MongoDB (BDSL456B) Manual
MongoDB (BDSL456B) Manual
laboratory
(BDSL456B)
Labarotary Manual
MongoDB LABORATORY
SEMESTER – IV
Course Code: BDSL456B IA Marks: 50
Number of Lecture Hours/Week 0:2:0 Total Hours: 24
CREDITS – 01
Course Learning objectives:
Lab Programs:
Sl.
Name of Program
No.
a. Illustration of Where Clause, AND, OR operations in MongoDB.
1 b. Execute the Commands of MongoDB and operations in MongoDB: Insert, Query,
Update, Delete and Projection. (Note: use any collection).
a. Develop a MongoDB query to select certain fields and ignore some fields of the
documents from any collection.
2
b. Develop a MongoDB query to display the first 5 documents from the results obtained
in a. [use of limit and find]
a. Execute query selectors (comparison selectors, logical selectors) and list out the results
on any collection
3
b. Execute query selectors (Geospatial selectors, Bitwise selectors) and list out the results
on any collection
Create and demonstrate how projection operators ($, $elematch and $slice) would be
4
used in the MongoDB.
Execute Aggregation operations ($avg, $min, $max, $push, $addToSet etc.). Encourage
5
students to execute several queries to demonstrate various aggregation operators.
Execute Aggregation Pipeline and its operations (pipeline must contain $match, $group,
6 $sort, $project, $skip etc. students encourage to execute several queries to demonstrate
various aggregation operators)
a. Find all listings with listing_url, name, address, host_picture_url in the listings And
7 Reviews collection that have a host with a picture url.
b. Using E-commerce collection write a query to display reviews summary.
MongoDB (BDSL456B)
Laboratory outcomes:
At the end of the course students should be able to:
1. Make use of MongoDB commands and queries.
2. Illustrate the role of aggregate pipelines to extract data.
3. Demonstrate optimization of queries by creating indexes.
4. Develop aggregate pipelines for text search in collections.
50 marks lab component is split up into 30 marks for the conduction of the experiment and
preparation of laboratory record each program should be evaluated for 10 marks, and 20 marks for
the test to be conducted after the completion of all the laboratory sessions.
MongoDB (BDSL456B)
Commands
Program 1
a. Illustration of Where Clause, AND, OR operations in MongoDB.
Where Clause in MongoDB: In MongoDB, the find() method is used to query documents from a
collection. The find() method can accept a query document as a parameter which acts as a
"WHERE" clause.
Syntax: db.collection.find({ field: value })
> db.details.find()
{ "_id" : ObjectId("5d7d3daf315728b4998f522e"), "rno" : 1, "name" : "Bhavana" }
{ "_id" : ObjectId("5d7d3f28315728b4998f522f"), "name" : "Amit", "rno" : 2 }
{ "_id" : ObjectId("5d7d3f56315728b4998f5230"), "rno" : 3, "email_id" : "[email protected]" }
{ "_id" : 1, "rno" : 4, "name" : "Akash" }
AND Operation in MongoDB: MongoDB provides the $and operator to perform logical AND
operation between multiple conditions in a query.
Syntax: db.collection.find({ $and: [ { field1: value1 }, { field2: value2 } ] })
OR Operation in MongoDB: Similarly, MongoDB provides the $or operator to perform logical
OR operation between multiple conditions in a query.
Syntax: db.collection.find({ $or: [ { field1: value1 }, { field2: value2 } ] })
Syntax: db.collection.find({ $and: [ { field1: value1 }, { $or: [ { field2: value2 }, { field3: value3 }
] } ] })
In the combined example, we're using both $and and $or operators to filter documents based on
multiple conditions. Adjust the field names and values according to your specific use case.
b. Execute the Commands of MongoDB and operations in MongoDB: Insert, Query, Update, Delete
and Projection. (Note: use any collection).
Insert Operation: Use the insertOne() method to insert a single document into a collection.
Syntax: db.collection.insertOne({ field1: value1, field2: value2, field3: value3 })
db.details.insert({rno:6,dob:new Date(Date.now())})
db.details.insert([{rno:18,doj:Date()}])
db.details.insert({rno:7,dob:ISODate("1990-12-31")}) //yyyy-mm-dd
// trying to insert data with duplicate _id, it will not accept as _id is primary key field
> db.details.insert({_id:1,rno:5,name:"Reena"})
E11000 duplicate key error index: db1.emp.$_id_ dup key: { : 1.0 }
Query Operation: Use the find() method to query documents from a collection.
Syntax: db.collection.find({ field: value })
Update Operation: Use the updateOne() method to update a single document in a collection.
Syntax: db.collection.updateOne( { field: value }, { $set: { fieldToUpdate: newValue } })
Delete Operation: Use the deleteOne() method to delete a single document from a collection.
Syntax: db.collection.deleteOne({ field: value })
db. details.deleteMany({})
db. details.deleteMany( { location: "chennai" } )
db. details.deleteOne( { location: "chennai" } )
Projection Operation: Use the second parameter of the find() method to specify which fields to
include or exclude in the query result.
Program 2
a. Develop a MongoDB query to select certain fields and ignore some fields of the documents
from any collection.
//Find command with condition with giving name field only to show
> db. details.find({rno:5},{name:1})
{ "_id" : ObjectId("5d83af5aa44331f62bcd836d"), "name" : "Jivan" }
//Find command with condition with giving name field only to show and _id to hide
> db. details.find({rno:5},{name:1,_id:0})
{ "name" : "Jivan" }
b. Develop a MongoDB query to display the first 5 documents from the results obtained in a.
[use of limit and find]
Limit Operation: Used to restrict the number of documents returned by a query. This is particularly
useful when you're dealing with large datasets and you only need a subset of documents.
Program 3
a. Execute query selectors (comparison selectors, logical selectors) and list out the results on
any collection
Comparison Selectors: Comparison selectors are used to compare fields against specific values or
other fields. Here are some common comparison selectors:
$eq - Matches values that are equal to a specified value.
$ne - Matches all values that are not equal to a specified value.
$gt - Matches values that are greater than a specified value.
$gte - Matches values that are greater than or equal to a specified value.
$lt - Matches values that are less than a specified value.
$lte - Matches values that are less than or equal to a specified value.
$in - Matches any of the values specified in an array.
$nin - Matches none of the values specified in an array.
Logical Selectors: Logical selectors are used to combine multiple conditions in a query. Here are
some common logical selectors:
$and - Joins query clauses with a logical AND and requires that all conditions be true.
$or - Joins query clauses with a logical OR and requires that at least one condition be true.
$not - Inverts the effect of a query expression and returns documents that do not match the query
expression.
$nor - Joins query clauses with a logical NOR and requires that none of the conditions be true.
>db.collection.find({
$and: [
{ age: { $gte: 18 } }, // age greater than or equal to 18
{ age: { $lt: 30 } }, // age less than 30
{ gender: "male" }, // gender equals "male"
{ city: { $in: ["New York", "Los Angeles"] } } // city is either "New York" or "Los Angeles"
]
})
b. Execute query selectors (Geospatial selectors, Bitwise selectors) and list out the results on
any collection
Geospatial Selectors: MongoDB supports geospatial queries for geospatial data. It provides two
types of geospatial indexes: 2d indexes and 2d sphere indexes.
Example: Let's say we have a collection named "locations" with documents containing location data:
{
"_id": ObjectId("60cf9df572f71e01c86bf703"),
"name": "Store A",
"location": { "type": "Point", "coordinates": [40.7128, -74.0060] }
},
{
"_id": ObjectId("60cf9df572f71e01c86bf704"),
"name": "Store B",
"location": {"type": "Point", "coordinates": [34.0522, -118.2437] }
},
{
"_id": ObjectId("60cf9df572f71e01c86bf705"),
"name": "Store C",
"location": { "type": "Point", "coordinates": [51.5074, -0.1278] }
}
To perform a geospatial query to find stores within a certain radius of a given location, you can use
the $near operator:
>db.locations.find({
location: {
$near: {
$geometry: {
type: "Point",
coordinates: [40.7128, -74.0060] // Coordinates of New York City
},
$maxDistance: 10000 // Maximum distance in meters (10 km)
}
}
})
This query will return stores within 10 kilometers of New York City.
Bitwise Selectors:
MongoDB does not directly support bitwise operators for querying. However, you can still implement
bitwise operations in your application code to filter documents based on bitwise properties.
Example: Let's say we have a collection named "users" with documents containing user data:
{
"_id": ObjectId("60cf9df572f71e01c86bf706"),
"name": "User A",
"permissions": 5 // Permission: Read + Write
},
{
"_id": ObjectId("60cf9df572f71e01c86bf707"),
"name": "User B",
"permissions": 3 // Permission: Read
},
{
"_id": ObjectId("60cf9df572f71e01c86bf708"),
"name": "User C",
"permissions": 7 // Permission: Read + Write + Execute
}
To find users with write permission (bitwise AND with 2 equals 2), you can use the bitwise AND operator
$bit:
>db.users.find({
permissions: {
$bit: {
and: 2
}
}
})
Program 4
Create and demonstrate how projection operators ($, $elematch and $slice) would be used in the
MongoDB.
$elemMatch: The $elemMatch operator is used to match documents that contain an array field with at
least one element that matches all the specified query criteria.
$slice: The $slice projection operator is used within the projection document to limit the number of
elements returned from an array field.
db.student2.insert([
{ "_id": 1, "name": "Alice", "grades": [85, 90, 78] },
{ "_id": 2, "name": "Bob", "grades": [95, 88, 82] },
{ "_id": 3, "name": "Charlie", "grades": [72, 92, 80] },
{ "_id": 4, "name": "David", "grades": [80, 85, 90] },
{ "_id": 5, "name": "Emily", "grades": [75, 87, 89] }
]
)
// To project only the first element in the grades array that is greater than or equal to 85, we can use
the following query:
>db.student.find( { grades: { $gte: 85 } }, { "grades.$": 1 } )
//grades array contains at least one element that is greater than or equal to 85 and less than or equal
to 90.
>db.students.find( { grades: { $elemMatch: { $gte: 85, $lte: 90 } } }, { name: 1, grades: 1 } )
Program 5
Execute Aggregation operations ($avg, $min, $max, $push, $addToSet etc.) Encourage students to
execute several queries to demonstrate various aggregation operators.
$avg: Calculates the average value of the numeric values in a specified array.
Example: Suppose we have a collection named "sales" with documents containing sales data:
{
"_id": ObjectId("60cf9df572f71e01c86bf703"),
"product": "A",
"sales": [100, 150, 200]
},
{
"_id": ObjectId("60cf9df572f71e01c86bf704"),
"product": "B",
"sales": [120, 130, 140]
}
>db.sales.aggregate([ { $project: { product: 1, averageSales: { $avg: "$sales" } } } ])
Example: Suppose we want to push a new sale value to the existing sales array:
$addToSet: Adds a value to an array field only if the value is not already present.
Example: Suppose we want to add a new customer to the list of customers for each product:
Program 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)
Example: Suppose we have a collection named "students" with documents containing information
about students and their exam scores:
[
{
"_id": ObjectId("60cf9df572f71e01c86bf703"),
"name": "Alice",
"age": 18,
"examScores": [
{"subject": "Math", "score": 80},
{"subject": "Science", "score": 75}
]
},
{
"_id": ObjectId("60cf9df572f71e01c86bf704"),
"name": "Bob",
"age": 20,
"examScores": [
{"subject": "Math", "score": 90},
{"subject": "Science", "score": 85}
]
},
{
"_id": ObjectId("60cf9df572f71e01c86bf705"),
"name": "Charlie",
"age": 19,
"examScores": [
{"subject": "Math", "score": 85},
{"subject": "Science", "score": 80}
]
}
]
$group: Group documents by a specified key and perform aggregation operations on grouped data.
db.students.aggregate([
// Stage 1: Filter students who are at least 18 years old
{ $match: { age: { $gte: 18 } } },
// Stage 2: Group students by age and calculate average exam score for each age group
{
$group: {
_id: "$age",
averageScore: { $avg: { $avg: "$examScores.score" } }
}
},
Program 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.
db.listingsAndReviews.aggregate([
// Match documents where host_picture_url exists
{ $match: { "host.host_picture_url": { $exists: true, $ne: null } } },
// Project the required fields
{
$project: {
listing_url: 1,
name: 1,
address: 1,
host_picture_url: "$host.host_picture_url" }
}])
db.reviews.insert([{
"listing_url": "https://www.example.com/listing/12345",
"name": "Cozy Apartment in Downtown",
"address": "New York",
"host_picture_url": "https://www.example.com/pictures/johndoe.jpg"
},
{
"listing_url": "https://www.example.com/listing/12345",
"name": "Cozy Apartment in Downtown",
"address": "New York"
}])
b. Using E-commerce collection write a query to display reviews summary.
db.ecommerce.insert([
{
"product_id": "123",
"rating": 5,
"review": "Great product!"
},
{
"product_id": "123",
"rating": 4,
"review": "Good quality"
},
{
"product_id": "456",
"rating": 3,
"review": "Average product"
},
{
"product_id": "456",
"rating": 2,
"review": "Poor quality"
},
{
"product_id": "789",
"rating": 5,
"review": "Excellent product!"
}
])
>db.ecommerce.aggregate([
// Unwind the reviews array to de-normalize the data
{ $unwind: "$reviews" },
1. `$unwind`: This stage de-normalizes the data by splitting the `reviews` array into separate documents.
This allows us to work with each review individually in subsequent stages.
2. `$group`: This stage groups the documents by the `product_id` field and calculates summary statistics
for each product. We use `$avg` to calculate the average rating and `$sum` to count the total number of
reviews for each product.
3. `$project`: This stage reshapes the output document to include only the fields we want to display in the
final result. Here, we exclude the `_id` field generated by the `$group` stage and rename the grouped `_id`
field to `product_id` for clarity.
This query will give you a summary of reviews for each product, including the average rating and the total number of reviews.
You can further customize the pipeline to include additional summary statistics as needed.
Program 8
db.users.insert({
username: "John",
age: 30,
city:"Chennai",
"interests":["music","garden"],
Description:["good","avg","excellent"],
"hashedField": "hashedValue1",
"createdAt": ISODate("2023-01-01T00:00:00Z") })
Compass
Unique index:
Now, if I insert a new document that has name already exists in the collection, the MongoDB server
will throw an error:
Sparse Index:
Optional Fields: If a field in your collection is optional (i.e., not present in all documents), a sparse
index can be a good choice to optimize storage and write performance.
Null Values: If you anticipate many documents with null values for the indexed field, a sparse index
can help avoid storing unnecessary null entries in the index, saving space.
Compound Index:
Multikey Index:
db.users.createIndex({ "interests": 1 })
MongoDB provides text indexes to support text search queries on string content. text indexes can
include any field whose value is a string or an array of string elements. Remember that you can have
only one text index per collection so after creating one if you create another text index, you will get an
error.
Hashed Index: Indexes where MongoDB hashes the index keys to create a more even distribution of
keys.
They distribute data across shards based on the hash value of the indexed field, improving query
performance for filtering based on that field
TTL (Time-To-Live) Index: Indexes that automatically expire documents after a certain amount of
time.
Program 9
a. Develop a query to demonstrate Text search using catalog data collection for a given word
Suppose your `catalog_data` collection has a field named `description` where you want to perform the text
search.
db.catalog_data.insertMany([
{
item: "Laptop",
description: "High-performance laptop for gaming and productivity tasks",
category: "Electronics" ,
price:1200,
details:
{brand: " apple",size:14}
},
{
item: "Smartphone",
description: "Latest smartphone with advanced camera features and technology",
category: "Electronics" ,
price:3400,
details:
{brand: " lg",size:14}
},
{
item: "Book",
description: "Classic novel set in the Victorian era",
category: "Books" ,
price:6780
},
{
item: "Television",
description: "Ultra HD television with smart features",
category: "Electronics" ,
price:5460,
details:
First, you need to create a text index on the field where you want to perform the text search. In
this case, let's create a text index on the `description` field:
> db.catalog_data.createIndex({ description: "text" })
Now, you can perform a text search using the `$text` operator along with the `$search`
parameter:
> db.catalog_data.find({ $text: { $search: "given_word" } })
Replace `"given_word"` with the word you want to search for in the `description` field.
3. **Query Explain**:
As before, you can use the `explain()` method to get information about how MongoDB
executes the text search query:
> db.catalog_data.find({ $text: { $search: "given_word" } }).explain("executionStats")
This command provides execution statistics for the text search query.
MongoDB's text search also supports additional options such as case insensitivity, language-
specific stemming, and weighting of search terms. You can specify these options using the
`$text` operator.
>db.catalog_data.find({ $text: { $search: "given_word", $caseSensitive: false, $language:
"english" } })
b. Develop queries to illustrate excluding documents with certain words and phrases
The `$not` operator in conjunction with regular expressions or the `$regex` operator. Here's how you can
develop queries to illustrate excluding documents with certain words and phrases:
Suppose you have a collection named `documents` and you want to exclude documents containing specific
words or phrases from a field named `text`.
You can use regular expressions to match documents that do not contain certain words or phrases. For
example, to exclude documents containing the word "example", you can use the following query:
> db.documents.find({ text: { $not: /example/ } })
This query returns all documents from the `documents` collection where the `text` field does not contain
the word "example".
This query returns all documents from the `documents` collection where the `text` field does not contain
the phrase "example phrase".
3. **Query Explain**:
As always, you can use the `explain()` method to get information about how MongoDB executes the
query:
>db.documents.find({ text: { $not: /example/ } }).explain("executionStats")
This command provides execution statistics for the query.
By using the `$not` operator with regular expressions or the `$regex` operator, you can exclude documents
with certain words or phrases from your MongoDB queries. Adjust the regular expressions or regex
patterns according to your specific exclusion criteria.
Program 10
$match Stage:
1. This stage filters documents that contain the specified search term in any of the text fields indexed
for text search.
2. The $text operator enables text search, and the $search parameter specifies the search term.
3. MongoDB will use the text index (if available) to efficiently perform the search.
VIVA QUESTIONS
MongoDB supports field, range-based, string pattern matching type queries. for searching the data in the
database
MongoDB support primary and secondary index on any fields
MongoDB basically uses JavaScript objects in place of procedures
MongoDB uses a dynamic database schema
MongoDB is very easy to scale up or down
MongoDB has inbuilt support for data partitioning (Sharding).
You should use MongoDB when you are building internet and business applications that need to evolve
quickly and scale elegantly. MongoDB is popular with developers of all kinds who are building scalable
applications using agile methodologies. MongoDB is a great choice if one needs to:
MongoDB supports a wide range of data types as values in documents. Documents in MongoDB are
similar to objects in JavaScript. Along with JSON’s essential key/value–pair nature, MongoDB adds
support for a number of additional data types. The common data types in MongoDB are:
Null
{"x" : null}
Boolean
{"x" : true}
Number
{"x" : 4}
String
{"x" : "foobar"}
Date
{"x" : new Date()}
Regular expression
{"x" : /foobar/i}
Array
{"x" : ["a", "b", "c"]}
Embedded document
{"x" : {"foo" : "bar"}}
Object ID
{"x" : ObjectId()}
Binary Data
Binary data is a string of arbitrary bytes.
Code
{"x" : function() { /* ... */ }}
The find method is used to perform queries in MongoDB. Querying returns a subset of documents in a
collection, from no documents at all to the entire collection. Which documents get returned is determined
by the first argument to find, which is a document specifying the query criteria.
Example:
> db.users.find({"age" : 24})
The CRUD API in MongoDB provides deleteOne and deleteMany for this purpose. Both of these methods
take a filter document as their first parameter. The filter specifies a set of criteria to match against in
removing documents.
For example:
> db.books.deleteOne({"_id" : 3})
Once a document is stored in the database, it can be changed using one of several update
methods: updateOne, updateMany, and replaceOne. updateOne and updateMany each takes a filter
document as their first parameter and a modifier document, which describes changes to make, as the
second parameter. replaceOne also takes a filter as the first parameter, but as the second
parameter replaceOne expects a document with which it will replace the document matching the filter.
{
"_id" : ObjectId("4b2b9f67a1f631733d917a7a"),
"name" : "alice",
"friends" : 24,
"enemies" : 2
}
The basic method for adding data to MongoDB is “inserts”. To insert a single document, use the
collection’s insertOne method:
For inserting multiple documents into a collection, we use insertMany. This method enables passing an
array of documents to the database.
Indexing: It supports generic secondary indexes and provides unique, compound, geospatial, and full-text
indexing capabilities as well.
Aggregation: It provides an aggregation framework based on the concept of data processing pipelines.
Special collection and index types: It supports time-to-live (TTL) collections for data that should expire
at a certain time
File storage: It supports an easy-to-use protocol for storing large files and file metadata.
Sharding: Sharding is the process of splitting data up across machines.
The document-oriented data model of MongoDB makes it easier to split data across multiple servers.
Balancing and loading data across a cluster is done by MongoDB. It then redistributes documents
automatically.
The mongos acts as a query router, providing an interface between client applications and the sharded
cluster.
Config servers store metadata and configuration settings for the cluster. MongoDB uses the config servers
to manage distributed locks. Each sharded cluster must have its own config servers.
It is a JavaScript shell that allows interaction with a MongoDB instance from the command line. With that
one can perform administrative functions, inspecting an instance, or exploring MongoDB.
$ mongod
$ mongo
MongoDB shell version: 4.2.0
connecting to: test
>
The shell is a full-featured JavaScript interpreter, capable of running arbitrary JavaScript programs. Let’s
see how basic math works on this:
> x = 100;
200
> x / 5;
20
11. What are Databases in MongoDB?
MongoDB groups collections into databases. MongoDB can host several databases, each grouping
together collections.
Some reserved database names are as follows:
admin
local
config
A Document in MongoDB is an ordered set of keys with associated values. It is represented by a map,
hash, or dictionary. In JavaScript, documents are represented as objects:
{"greeting" : "Hello world!"}
The find method is used to perform queries in MongoDB. Querying returns a subset of documents in a
collection, from no documents at all to the entire collection. Which documents get returned is determined
by the first argument to find, which is a document specifying the query criteria.
For example: If we have a string we want to match, such as a "username" key with the value "alice", we
use that key/value pair instead:
If the value of a field does not yet exist, the "$set" sets the value. This can be useful for updating schemas
or adding user-defined keys.
Example:
> db.users.findOne()
{
"_id" : ObjectId("4b253b067525f35f94b60a31"),
"name" : "alice",
"age" : 23,
"sex" : "female",
"location" : "India"
}
> db.users.updateOne({"_id" :
ObjectId("4b253b067525f35f94b60a31")},
... {"$set" : {"favorite book" : "Start with Why"}})
Sharding is the process of splitting data up across machines. We also use the term “partitioning”
sometimes to describe this concept. We can store more data and handle more load without requiring larger
or more powerful machines, by putting a subset of data on each machine.
In the figure below, RS0 and RS1 are shards. MongoDB’s sharding allows you to create a cluster of many
machines (shards) and break up a collection across them, putting a subset of data on each shard. This
allows your application to grow beyond the resource limits of a standalone server or replica set.
MongoDB has two types of geospatial indexes: 2dsphere and 2d. 2dsphere indexes work with spherical
geometries that model the surface of the earth based on the WGS84 datum. This datum model the surface
of the earth as an oblate spheroid, meaning that there is some flattening at the poles. Distance calculations
using 2sphere indexes, therefore, take the shape of the earth into account and provide a more accurate
treatment of distance between, for example, two cities, than do 2d indexes. Use 2d indexes for points
stored on a two-dimensional plane.
2dsphere allows you to specify geometries for points, lines, and polygons in the GeoJSON format. A point
is given by a two-element array, representing [longitude, latitude]:
{
"name" : "New York City",
"loc" : {
"type" : "Point",
"coordinates" : [50, 2]
}
}
{
"name" : "Hudson River",
"loc" : {
"type" : "LineString",
"coordinates" : [[0,1], [0,2], [1,2]]
}
}
In MongoDB, indexes help in efficiently resolving queries. What an Index does is that it stores a small part
of the data set in a form that is easy to traverse. The index stores the value of the specific field or set of
fields, ordered by the value of the field as specified in the index.
MongoDB’s indexes work almost identically to typical relational database indexes.
Indexes look at an ordered list with references to the content. These in turn allow MongoDB to query
orders of magnitude faster. To create an index, use the createIndex collection method.
For example:
Here, executionStats mode helps us understand the effect of using an index to satisfy queries.