0% found this document useful (0 votes)
5K views

MongoDB (BDSL456B) Manual

Uploaded by

Aayeth Ramzi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5K views

MongoDB (BDSL456B) Manual

Uploaded by

Aayeth Ramzi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Mongodb

laboratory
(BDSL456B)

Labarotary Manual

Dept. of Computer Science and Engineering ( Data Science)

Adichunchanagiri Institute of Technology


(Affiliated to Visvesvaraya Technological University, Belagavi)
A.I.T Campus, Jyothinagar, Chikkamagaluru – 577102
www.aitckm.in
2023-24
MongoDB (BDSL456B)

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:

1. Understand basic MongoDB functions, operators and types of operations in MongoDB.


2. Demonstrate the use of Indexing, Advanced Indexing in MongoDB.
3. Apply the aggregation and Map Reduction in MongoDB.
4. Demonstrate text searching on collections in MongoDB.

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)

a. Demonstrate creation of different types of indexes on collection (unique, sparse,


8 compound and multikey indexes)
b. Demonstrate optimization of queries using indexes.
a. Develop a query to demonstrate Text search using catalog data collection for a given
9 word
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.

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

//to start mongo client


$ mongosh

//to list out database names


> show dbs

//to create database


> use student

//To know the Mongodb version


>db.version()

//to check in which database I am working


> db

//to drop database in which I am working


> db.dropDatabase()

//To create collection


> db.createCollection('details')

//to list out collection names


> show collections

Now drop the collection with the name mycollection


>db.details.drop()

//create collection by inserting document


> db.details.insert({rno:1,name:'Bhavana'})

// create student details like name, rno, email,location,address,marks

//Every row/document can be different than other


> db.details.insert({name:'Amit',rno:2})
> db.details.insert({rno:3, email_id:'[email protected]'})

// To display data from collection


> db.details.find()
{ "_id" : ObjectId("5d7d3daf315728b4998f522e"), "rno" : 1, "name" : "Bhavana" }
{ "_id" : ObjectId("5d7d3f28315728b4998f522f"), "name" : "Amit", "rno" : 2 }

DEPT. OF DATA SCIENCE, A I T, CKM 3


MongoDB (BDSL456B)

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" }

//findOne to show only first record


> db. details.findOne()
{
"_id" : ObjectId("5d83af5aa44331f62bcd8369"),
"rno" : 1,
"name" : "Ashiti"
}

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 } ] })

>db.details.find({$and: [{"location": "chennai"}, {"location": "delhi"}] })

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 } ] })

>db.details.find({$or: [{"location": "chennai"}, {"location": "delhi"}] })

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

DEPT. OF DATA SCIENCE, A I T, CKM 4


MongoDB (BDSL456B)

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 })

Every row/document can be different than other


> db.details.insert({name:'Amit',rno:2})
> db.details.insert({rno:3, email_id:'[email protected]'})

// To insert date use ISODate function


> db.emp.insert({rno:15, name:'Ravina', dob: ISODate("2019-09-14")})

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 }

//Insert multiple documents at once


> db.details.insert([{rno:7,name:'a'},{rno:8,name:'b'},{rno:8,name:'c'}])

// to insert multiple values for one key using []


> db.emp.insert({rno:10,name:'Ankit',hobbies:['singing','cricket','swimming',’music’],age:21})

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 } })

DEPT. OF DATA SCIENCE, A I T, CKM 5


MongoDB (BDSL456B)

Delete Operation: Use the deleteOne() method to delete a single document from a collection.
Syntax: db.collection.deleteOne({ field: value })

//It will remove record having rno as 4


> db. details.remove({rno:4})

//It will remove only one record having rno as 4


> db. details.remove({rno:4},1)

//It will remove all records


> db. details.remove({})

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.

Syntax: db.collection.find({}, { field1: 1, field2: 1, _id: 0 })

// Find command to show only names without condition


> db. details.find({},{name:1,_id:0})
{ "name" : "Ashiti" }
{ "name" : "Savita" }

DEPT. OF DATA SCIENCE, A I T, CKM 6


MongoDB (BDSL456B)

Program 2

a. Develop a MongoDB query to select certain fields and ignore some fields of the documents
from any collection.

Syntax: db.collection.find({}, { field1: 1, field2: 1, _id: 0 })


 db.collection.find({}) is used to retrieve all documents from the collection.
 { field1: 1, field2: 1, _id: 0 } specifies the projection document where:
 field1: 1 and field2: 1 indicates that these fields will be included in the result.
 _id: 0 indicates that the _id field will be excluded from the result.

//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" }

// Find command to show only names without condition


> db. details.find({},{name:1,_id:0})
{ "name" : "Ashiti" }
{ "name" : "Savita" }

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.

Syntax: db.collection.find({}, { field1: 1, field2: 1, _id: 0 }).limit(5)


 Limit (5) limits the number of documents returned to 5.
// Limit use to show only some records from starting- following command shows only first 2
records from collection
> db. details.find().limit(2)
{ "_id" : ObjectId("5d83af5aa44331f62bcd8369"), "rno" : 1, "name" : "Ashiti" }
{ "_id" : ObjectId("5d83af5aa44331f62bcd836a"), "rno" : 2, "name" : "Savita" }

DEPT. OF DATA SCIENCE, A I T, CKM 7


MongoDB (BDSL456B)

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

DEPT. OF DATA SCIENCE, A I T, CKM 8


MongoDB (BDSL456B)

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.

DEPT. OF DATA SCIENCE, A I T, CKM 9


MongoDB (BDSL456B)

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
}
}
})

DEPT. OF DATA SCIENCE, A I T, CKM 10


MongoDB (BDSL456B)

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] }
]
)

>db.student.find( {}, { "grades": { $slice: 2 } } )


>db.student.find( {}, { "grades": { $slice: -3 } } )

// 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 } )

DEPT. OF DATA SCIENCE, A I T, CKM 11


MongoDB (BDSL456B)

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" } } } ])

$min: Finds the minimum value in a specified array.

Example: To find the minimum sales value for each product:

>db.sales.aggregate([ { $project: { product: 1, minSales: { $min: "$sales" } } } ])

$max: Finds the maximum value in a specified array.

Example: To find the maximum sales value for each product:

>db.sales.aggregate([ { $project: { product: 1, maxSales: { $max: "$sales" } } } ])

$push: Adds a value to an array field in each document.

Example: Suppose we want to push a new sale value to the existing sales array:

>db.sales.aggregate([ { $project: { product: 1, sales: { $push: 250 } } } ])

$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:

>db.sales.aggregate([ { $project: { product: 1, customers: { $addToSet: "New Customer" } } } ])

DEPT. OF DATA SCIENCE, A I T, CKM 12


MongoDB (BDSL456B)

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}
]
}
]

Let's create an aggregation pipeline to perform various operations:

$match: Filter documents based on specified criteria.

DEPT. OF DATA SCIENCE, A I T, CKM 13


MongoDB (BDSL456B)

$group: Group documents by a specified key and perform aggregation operations on grouped data.

$sort: Sort documents based on specified criteria.

$project: Reshape documents by including, excluding, or modifying fields.

$skip: Skip a specified number of documents.

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" } }
}
},

// Stage 3: Sort age groups by average score in descending order


{ $sort: { averageScore: -1 } },

// Stage 4: Project to include only age and averageScore fields


{
$project: {
_id: 0,
age: "$_id",
averageScore: 1
}
},

// Stage 5: Skip the first 1 document


{ $skip: 1 }
])
This aggregation pipeline will filter students who are at least 18 years old, group them by age, calculate the average exam score for each age group, sort
the age groups by average score in descending order, include only the age and averageScore fields in the output, and skip the first document.

DEPT. OF DATA SCIENCE, A I T, CKM 14


MongoDB (BDSL456B)

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" }
}])

This MongoDB aggregation pipeline:


1. Matches documents where the `host_picture_url` exists and is not null within the `host`
subdocument.
2. Projects the required fields (`listing_url`, `name`, `address`, and `host_picture_url`).
Assuming that your Listings And Reviews collection is named `listingsAndReviews` and the host's
picture URL is stored within a `host` subdocument, this query should retrieve the desired results.
Adjust the field names and collection name if they differ in your actual schema.

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"

DEPT. OF DATA SCIENCE, A I T, CKM 15


MongoDB (BDSL456B)

}])
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" },

DEPT. OF DATA SCIENCE, A I T, CKM 16


MongoDB (BDSL456B)

// Group by product and calculate summary statistics


{
$group: {
_id: "$product_id",
average_rating: { $avg: "$reviews.rating" },
total_reviews: { $sum: 1 },
// You can add more summary statistics as needed
}
},
// Optionally, you can project the output to shape it as needed
{
$project: {
_id: 0, // Exclude the group _id
product_id: "$_id",
average_rating: 1,
total_reviews: 1,
}
}
])

Explanation of the pipeline stages:

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.

DEPT. OF DATA SCIENCE, A I T, CKM 17


MongoDB (BDSL456B)

Program 8

a. Demonstrate creation of different types of indexes on collection (unique, sparse, compound


and multikey indexes)
b. Demonstrate optimization of queries using indexes.

db.users.insert({

username: "John",

age: 30,

city:"Chennai",

"interests":["music","garden"],

Description:["good","avg","excellent"],

"hashedField": "hashedValue1",

"location": { "type": "Point", "coordinates": [ -73.97, 40.77 ] },

"createdAt": ISODate("2023-01-01T00:00:00Z") })

Compass

(all column names in double quotes and no ISOdate )

Unique index:

db.users.createIndex({ "username": 1 }, { unique: true })

Now, if I insert a new document that has name already exists in the collection, the MongoDB server
will throw an error:

Sparse Index:

db.users.createIndex({ "city": 1 }, { sparse: true })

Use Cases for Sparse Indexes:

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.

DEPT. OF DATA SCIENCE, A I T, CKM 18


MongoDB (BDSL456B)

Compound Index:

db.users.createIndex({ "username": 1, "age": 1 })

Multikey Index:

db.users.createIndex({ "interests": 1 })

Assuming interests is an array field in the user documents.

db.users.createIndex( { description: "text" } )

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.

db. users.createIndex({ city: "hashed" })

Hashed indexes are beneficial for sharding collections in MongoDB.

They distribute data across shards based on the hash value of the indexed field, improving query
performance for filtering based on that field

Geo-spatial Index: Indexes used for geo-spatial queries.

db. users.createIndex({ location: "2dsphere" })

TTL (Time-To-Live) Index: Indexes that automatically expire documents after a certain amount of
time.

db. users.createIndex({ createdAt: 1 }, { expireAfterSeconds: 3600 })

db.products.createIndex({ "lastModified": { expireAfterSeconds: 3600 } })

DEPT. OF DATA SCIENCE, A I T, CKM 19


MongoDB (BDSL456B)

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:

DEPT. OF DATA SCIENCE, A I T, CKM 20


MongoDB (BDSL456B)

{brand: " lg",size:14}


},
{
item: "Headphones",
description: "Wireless headphones with noise-cancellation technology",
category: "Electronics" ,
price:8900,
details:
{brand: " boat",size:12}
},
{
item: "Smartphone",
description: "Latest smartphone with latest technology ",
category: "Electronics" ,
price:7632,
details:
{brand: " samsung",size:13}
}
])

1. **Creating Text Index**:

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" })

2. **Executing Text Search Query**:

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**:

DEPT. OF DATA SCIENCE, A I T, CKM 21


MongoDB (BDSL456B)

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.

4. **Additional Text Search Options**:

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" } })

Adjust the options (`$caseSensitive`, `$language`, etc.) according to your requirements.

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`.

1. **Using Regular Expressions**:

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".

2. **Using the $regex Operator**:


Alternatively, you can use the `$regex` operator to achieve the same result. For example, to exclude
documents containing the phrase "example phrase", you can use the following query:

DEPT. OF DATA SCIENCE, A I T, CKM 22


MongoDB (BDSL456B)

> db.documents.find({ text: { $not: { $regex: "example phrase" } } })

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.

DEPT. OF DATA SCIENCE, A I T, CKM 23


MongoDB (BDSL456B)

Program 10

Develop an aggregation pipeline to illustrate Text search on Catalog data collection


db.catalog_data.aggregate([
// Stage 1: Match documents containing the search term
{
$match: {
$text: {
$search: "search_term"
}
}
},
// Stage 2: Project only required fields
{
$project: {
_id: 0,
productName: 1,
description: 1,
// Add more fields as needed
}
}
])

$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.

DEPT. OF DATA SCIENCE, A I T, CKM 24


MongoDB (BDSL456B)

VIVA QUESTIONS

1. What are some of the advantages of MongoDB?

Some advantages of MongoDB are as follows:

 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).

2. When to use MongoDB?

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:

 Support a rapid iterative development.


 Scale to high levels of read and write traffic - MongoDB supports horizontal scaling through Sharding,
distributing data across several machines, and facilitating high throughput operations with large sets of
data.
 Scale your data repository to a massive size.
 Evolve the type of deployment as the business changes.
 Store, manage and search data with text, geospatial, or time-series dimensions.

3. What are the data types in MongoDB?

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"}}

DEPT. OF DATA SCIENCE, A I T, CKM 25


MongoDB (BDSL456B)

 Object ID
{"x" : ObjectId()}
 Binary Data
Binary data is a string of arbitrary bytes.
 Code
{"x" : function() { /* ... */ }}

4. How to perform queries in MongoDB?

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})

5. How do you Delete a Document?

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})

6. How do you Update a Document?

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.

For example, in order to replace a document:

{
"_id" : ObjectId("4b2b9f67a1f631733d917a7a"),
"name" : "alice",
"friends" : 24,
"enemies" : 2
}

7. How to add data in MongoDB?

The basic method for adding data to MongoDB is “inserts”. To insert a single document, use the
collection’s insertOne method:

> db.books.insertOne({"title" : "Start With Why"})

For inserting multiple documents into a collection, we use insertMany. This method enables passing an
array of documents to the database.

DEPT. OF DATA SCIENCE, A I T, CKM 26


MongoDB (BDSL456B)

8. What are some features of MongoDB?

 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.

9. How does Scale-Out occur in MongoDB?

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.

10. What is the Mongo Shell?

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.

To start the shell, run the mongo executable:

$ mongod
$ mongo
MongoDB shell version: 4.2.0
connecting to: test
>

DEPT. OF DATA SCIENCE, A I T, CKM 27


MongoDB (BDSL456B)

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

12. What is a Collection in MongoDB?

A collection in MongoDB is a group of documents. If a document is the MongoDB analog of a row in a


relational database, then a collection can be thought of as the analog to a table.
Documents within a single collection can have any number of different “shapes.”, i.e. collections have
dynamic schemas.
For example, both of the following documents could be stored in a single collection:

{"greeting" : "Hello world!", "views": 3}


{"signoff": "Good bye"}

13. What is a Document in MongoDB?

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!"}

Complex documents will contain multiple key/value pairs:


{"greeting" : "Hello world!", "views" : 3}

14. How is Querying done in MongoDB?

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:

> db.users.find({"username" : "alice"})

15. Explain the SET Modifier in MongoDB?

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.

DEPT. OF DATA SCIENCE, A I T, CKM 28


MongoDB (BDSL456B)

Example:

> db.users.findOne()
{
"_id" : ObjectId("4b253b067525f35f94b60a31"),
"name" : "alice",
"age" : 23,
"sex" : "female",
"location" : "India"
}

To add a field to this, we use “$set”:

> db.users.updateOne({"_id" :
ObjectId("4b253b067525f35f94b60a31")},
... {"$set" : {"favorite book" : "Start with Why"}})

16. Explain the process of Sharding.

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.

17. What are Geospatial Indexes in MongoDB?

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]:

DEPT. OF DATA SCIENCE, A I T, CKM 29


MongoDB (BDSL456B)

{
"name" : "New York City",
"loc" : {
"type" : "Point",
"coordinates" : [50, 2]
}
}

A line is given by an array of points:

{
"name" : "Hudson River",
"loc" : {
"type" : "LineString",
"coordinates" : [[0,1], [0,2], [1,2]]
}
}

18. Explain the term “Indexing” in MongoDB.

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:

> db.users.find({"username": "user101"}).explain("executionStats")

Here, executionStats mode helps us understand the effect of using an index to satisfy queries.

DEPT. OF DATA SCIENCE, A I T, CKM 30

You might also like