Manual Mango
Manual Mango
MongoDB
(BDSL456B)
(As per Visvesvaraya Technological University Syllabus)
2 a. Develop a MongoDB query to select certain fields and ignore some fields of the
documents from any collection.
b. Develop a MongoDB query to display the first 5 documents from the results
obtained in a. [use of limit and find]
3 a. Execute query selectors (comparison selectors, logical selectors ) and list
out the results on any collection
b. Execute query selectors (Geospatial selectors, Bitwise selectors ) and list out
the results on any collection
4 Create and demonstrate how projection operators ($, $elematch and $slice) would
be used in the MondoDB.
5 Execute Aggregation operations ($avg, $min,$max, $push, $addToSet etc.).
students encourage to execute several queries to demonstrate various aggregation
operators)
6 Execute Aggregation Pipeline and its operations (pipeline must contain $match,
$group, $sort, $project, $skip etc. students encourage to execute several queries to
demonstrate various aggregation operators)
7 a. Find all listings with listing_url, name, address, host_picture_url in the listings
And Reviews collection that have a host with a picture url
b. Develop queries to illustrate excluding documents with certain words and phrases
10 Develop an aggregation pipeline to illustrate Text search on Catalog data collection.
1
DATABASE
2
RDBMS VS DOCUMENT DATABASE
1. Data Model:
o RDBMS: RDBMS uses a structured data model based on tables
with rows and columns. Data is organized into tables with
predefined schemas, and relationships between tables are
established using foreign keys.
o Document Database: Document databases use a semi-
structured data model where data is stored in documents,
typically in formats like JSON or BSON. Each document can
have its own structure, and there is no fixed schema across the
entire database.
2. Schema:
o RDBMS: RDBMS typically requires a predefined schema, where
the structure of each table and the relationships between tables
must be specified before inserting data. Changes to the schema
can be complex and often require downtime.
o Document Database: Document databases are schema-less or
schema-flexible. Documents within the same collection
(equivalent to a table in RDBMS) can have different structures,
and new fields can be added dynamically without requiring a
predefined schema.
3. Query Language:
o RDBMS: RDBMS typically uses SQL (Structured Query
Language) for querying and manipulating data. SQL is powerful
and standardized across most relational databases.
o Document Database: Document databases often use query
languages specific to the database, such as MongoDB's query
language or Couchbase's N1QL. These languages are designed
to work with semi-structured data and allow for querying nested
fields within documents.
4. Scaling:
o RDBMS: Scaling RDBMS horizontally (across multiple servers)
can be challenging, especially for large-scale applications.
Vertical scaling (increasing the resources of a single server) is a
common approach for RDBMS.
o Document Database: Document databases are often designed to
scale horizontally with ease. They can distribute data across
3
multiple nodes and handle large volumes of data and high
read/write loads efficiently.
5. Use Cases:
o RDBMS: RDBMS are well-suited for applications where data has
a clearly defined structure, and ACID (Atomicity, Consistency,
Isolation, Durability) transactions are required. Common use
cases include traditional transactional systems, such as
financial applications and e-commerce platforms.
o Document Database: Document databases are ideal for
applications with rapidly evolving schemas, unstructured or
semi-structured data, and where high performance on read and
write operations is crucial. Use cases include content
management systems, real-time analytics, and applications
requiring flexible data models.
Introduction to MongoDB:
Features:
4
4. High Availability: Supports replication and automated failover,
ensuring data availability in case of hardware failure or network
partition.
5. Horizontal Scalability: MongoDB allows for horizontal scaling
through sharding, distributing data across multiple servers to handle
large volumes of data and traffic.
6. Rich Query Language: MongoDB supports a rich query language with
features like secondary indexes, aggregations, text search, and
geospatial queries.
7. Aggregation Framework: Provides an aggregation pipeline for
processing and transforming documents in the database.
8. Indexing: Supports various types of indexes including compound
indexes, geospatial indexes, text indexes, and hashed indexes to
improve query performance.
9. Security: Offers authentication, authorization, encryption, and
auditing features to ensure data security and compliance.
10. Ad Hoc Queries: Allows ad hoc queries on documents using a
query language similar to SQL.
Components:
Use Cases:
5
Catalog and Product Data: MongoDB can handle complex product
catalogs with varying attributes and structures efficiently.
Mobile and Social Infrastructure: MongoDB is well-suited for mobile
and social applications due to its ability to handle large volumes of
unstructured data.
IoT Data Storage: MongoDB can efficiently store and analyze large
volumes of data generated by IoT devices.
6
ability to handle high volumes of data with low latency makes it
suitable for applications that require real-time decision-making.
6. Real-Time Collaboration Tools: MongoDB is used in collaboration
tools such as project management platforms, document editing tools,
and messaging applications. It allows multiple users to collaborate in
real-time by storing and synchronizing data efficiently.
7. Content Management Systems (CMS): MongoDB is used in CMS
applications for storing and serving content in real-time. Its flexible
schema allows content creators to store various types of content such
as text, images, videos, and metadata.
8. E-commerce: MongoDB is used in e-commerce applications for
product catalog management, inventory tracking, and real-time order
processing. Its ability to handle complex product data and high traffic
volumes makes it suitable for e-commerce platforms.
1. Basic Queries:
o Find: The find() method retrieves documents from a collection. It
takes a query object as a parameter to specify selection criteria.
Projection:
Query Operators:
7
o Element Operators ($exists, $type)
o Array Operators ($in, $all, $elemMatch)
Example: db.users.find({ age: { $gt: 25, $lt: 40 } }) retrieves users with
ages between 25 and 40.
``Sorting:
The sort() method sorts documents in the result set based on specified
criteria.
Limiting Results:
Skipping Results:
db.collection_name.find().skip(number)
Example: db.users.find().skip(5) skips the first 5 users.
Indexing:
db.collection_name.createIndex({ field: 1 })
Example: db.users.createIndex({ name: 1 }) creates an index on the
name field.
Aggregation:
8
{ $match: { status: "delivered" } },
{ $group: { _id: "$product", total: { $sum: "$quantity" } } }
])
Text Search:
Geospatial Queries:
db.collection_name.find({
location: {
$near: {
$geometry: { type: "Point", coordinates: [longitude, latitude] },
$maxDistance: distance_in_meters
}
}
})
Example:
db.places.find({
location: {
$near: {
$geometry: { type: "Point", coordinates: [-73.97, 40.77] },
$maxDistance: 1000
}
}
9
MongoDB | Amarnath Patil
Experiments
Experiment 1:
// Insert document
db.collection.insertOne({ name: "John", age: 35, gender: "male" })
// Query document
db.collection.find({ age: { $gt: 30 } })
// Update document
db.collection.updateOne({ name: "John" }, { $set: { age: 40 } })
// Delete document
db.collection.deleteOne({ name: "John" })
// Projection
db.collection.find({}, { name: 1, age: 1 })
Experiment 2:
Experiment 3:
// Comparison selectors
db.collection.find({ age: { $gt: 30 } })
// Logical selectors
db.collection.find({ $and: [{ age: { $gt: 25 } }, { gender: "male" }] })
// Geospatial selectors
db.collection.find({ location: { $near: { $geometry: { type: "Point", coordinates:
[ -73.9667, 40.78 ] }, $maxDistance: 1000 } } })
// Bitwise selectors
db.collection.find({ flags: { $bitsAllSet: 4 } })
Experiment 4:
// $ projection operator
db.collection.find({}, { "grades.$": 1 })
Experiment 5:
// $avg
11
db.collection.aggregate([{ $group: { _id: null, avgAge: { $avg: "$age" } } }])
// $min
db.collection.aggregate([{ $group: { _id: null, minAge: { $min: "$age" } } }])
// $max
db.collection.aggregate([{ $group: { _id: null, maxAge: { $max: "$age" } } }])
// $push
db.collection.aggregate([{ $group: { _id: null, allNames: { $push: "$name" } }
}])
// $addToSet
db.collection.aggregate([{ $group: { _id: null, uniqueNames: { $addToSet:
"$name" } } }])
Experiment 6:
// Aggregation Pipeline
db.collection.aggregate([
{ $match: { age: { $gt: 30 } } },
{ $group: { _id: "$gender", count: { $sum: 1 } } },
{ $sort: { count: -1 } },
{ $skip: 2 },
{ $project: { _id: 0, gender: "$_id", count: 1 } }
])
Experiment 7:
db.eCommerce.aggregate([
12
{ $group: { _id: "$product_id", avgRating: { $avg: "$rating" }, totalReviews: {
$sum: 1 } } }
])
Experiment 8:
// Unique index
db.collection.createIndex({ username: 1 }, { unique: true })
// Sparse index
db.collection.createIndex({ city: 1 }, { sparse: true })
// Compound index
db.collection.createIndex({ age: 1, city: -1 })
// Multikey index
db.collection.createIndex({ tags: 1 })
// Using index
db.collection.find({ username: "john" }).hint({ username: 1 })
Experiment 9:
13
Experiment10:
Develop an aggregation pipeline to illustrate Text search on Catalog data
collection
db.Catalog.aggregate([
$search: {
text: {
},
$project: {
])
14
15