0% found this document useful (0 votes)
11 views24 pages

MongoDb Programs

The MongoDB Lab Manual for the Department of AIML includes various exercises demonstrating MongoDB operations such as querying, inserting, updating, and deleting documents. It covers the use of query selectors, projection operators, and aggregation functions, providing example commands and expected outputs. The manual serves as a practical guide for students to learn and practice MongoDB functionalities.

Uploaded by

Prajval V
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)
11 views24 pages

MongoDb Programs

The MongoDB Lab Manual for the Department of AIML includes various exercises demonstrating MongoDB operations such as querying, inserting, updating, and deleting documents. It covers the use of query selectors, projection operators, and aggregation functions, providing example commands and expected outputs. The manual serves as a practical guide for students to learn and practice MongoDB functionalities.

Uploaded by

Prajval V
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/ 24

IV SEM MONGO DB LAB MANUAL DEPT Of AIML

Sl PROGRAMS
No
1 a. Illustration of Where Clause, AND,OR operations in MongoDB.

// Find documents where age is greater than 25 and gender is "male"

db.collection.find({ $and: [{ age: { $gt: 25 } }, { gender: "male" }] })

// Find documents where age is greater than 30 or gender is "female"

db.collection.find({ $or: [{ age: { $gt: 30 } }, { gender: "female" }] })

b. Execute the Commands of MongoDB and operations in MongoDB : Insert,


Query, Update, Delete and Projection. (Note: use any collection)

db.createcollection(“studentdetails”)

To insert one document:


db.studentdetails.insertOne({title: "Post Title 1", body: "Body of post.", category:
"News", likes: 1,tags: ["news", "events"], date: Date()})

To insert multiple documents:


db.posts.insertMany([
{
title: "Post Title 2",
body: "Body of post.",
category: "Event",
likes: 2,
tags: ["news", "events"],
date: Date()
},
{
title: "Post Title 3",
body: "Body of post.",
category: "Technology",
likes: 3,
tags: ["news", "events"],
date: Date()
},
{
title: "Post Title 4",
body: "Body of post.",
category: "Event",
likes: 4,
tags: ["news", "events"],
date: Date()
}
])
IV SEM MONGO DB LAB MANUAL DEPT Of AIML

Output:
{
acknowledged: true,
insertedIds: {
'0': ObjectId('6699fec7bb5b4fc21146b799'),
'1': ObjectId('6699fec7bb5b4fc21146b79a'),
'2': ObjectId('6699fec7bb5b4fc21146b79b')
}
}

db.posts.find( { title: "Post Title 1" } )


db.posts.updateOne( { title: "Post Title 1" }, { $set: { likes: 2 } } )
The updateMany() method will update all documents that match the provided query.
db.posts.updateMany({}, { $inc: { likes: 1 } })

Output:
{
acknowledged: true,
insertedId: null,
matchedCount: 0,
modifiedCount: 0,
upsertedCount: 0
}
Delete One Document

db.collectionName.deleteOne({ key: value })

db.student.deleteOne({Anna:101})

Output:
{ acknowledged: true, deletedCount: 0 }
posts> db.posts.find({}, {title: 1, date: 1})
IV SEM MONGO DB LAB MANUAL DEPT Of AIML

Output:
[
{
_id: ObjectId('6699fec7bb5b4fc21146b799'),
title: 'Post Title 2',
date: 'Fri Jul 19 2024 11:21:03 GMT+0530 (India Standard Time)'
},
{
_id: ObjectId('6699fec7bb5b4fc21146b79a'),
title: 'Post Title 3',
date: 'Fri Jul 19 2024 11:21:03 GMT+0530 (India Standard Time)'
},
{
_id: ObjectId('6699fec7bb5b4fc21146b79b'),
title: 'Post Title 4',
date: 'Fri Jul 19 2024 11:21:03 GMT+0530 (India Standard Time)'
}]

Delete Multiple Documents


To delete multiple documents that match a specified condition, use deleteMany.

db.collectionName.deleteOne({ key: value })

db.student.deleteMany({ age: { $gt: 30 } })

Projection:
db.posts.find({}, {title: 1, date: 1})
IV SEM MONGO DB LAB MANUAL DEPT Of AIML

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

db.collection.insertMany([{name:"abc",age:25,gender:"female"},{name:"ghj",age
:55,gender:"male"},{name:"cvb",age:52,gender:"female"},{name:"yhn",age:80,g
ender:"female"},{name:"wsg",age:18,gender:"male"},{name:"jhc",age:29,gender:
"male"},{name:"jhc",age:29,gender:"male"}])

Output:
{
acknowledged: true,
insertedIds: {
'0': ObjectId('669a0268bb5b4fc21146b7a0'),
'1': ObjectId('669a0268bb5b4fc21146b7a1'),
'2': ObjectId('669a0268bb5b4fc21146b7a2'),
'3': ObjectId('669a0268bb5b4fc21146b7a3'),
'4': ObjectId('669a0268bb5b4fc21146b7a4'),
'5': ObjectId('669a0268bb5b4fc21146b7a5'),
'6': ObjectId('669a0268bb5b4fc21146b7a6')
}}

// Select certain fields and ignore some fields

db.collection.find({}, { name: 1, age: 1, _id: 0 })

Output:
[
{},
{},
{},
{ name: 'abc', age: 25 },
{ name: 'abc', age: 25 },
{ name: 'ghj', age: 55 },
{ name: 'cvb', age: 52 },
{ name: 'yhn', age: 80 },
{ name: 'wsg', age: 18 },
{ name: 'jhc', age: 29 },
{ name: 'jhc', age: 29 }
]
IV SEM MONGO DB LAB MANUAL DEPT Of AIML

b. Develop a MongoDB query to display the first 5 documents from the results
obtained in a. [use of limit and find]

// Display the first 5 documents


Query: db.collection.find().limit(5)
Output:
[
{
_id: ObjectId('669a0084bb5b4fc21146b79c'),
title: 'Post Title 2',
body: 'Body of post.',
category: 'Event',
likes: 2,
tags: [ 'news', 'events' ],
date: 'Fri Jul 19 2024 11:28:28 GMT+0530 (India
Standard Time)'
},
{
_id: ObjectId('669a0084bb5b4fc21146b79d'),
title: 'Post Title 3',
body: 'Body of post.',
category: 'Technology',
likes: 3,
tags: [ 'news', 'events' ],
date: 'Fri Jul 19 2024 11:28:28 GMT+0530 (India
Standard Time)'
},
{
_id: ObjectId('669a0084bb5b4fc21146b79e'),
title: 'Post Title 4',
body: 'Body of post.',
category: 'Event',
likes: 4,
tags: [ 'news', 'events' ],
IV SEM MONGO DB LAB MANUAL DEPT Of AIML

date: 'Fri Jul 19 2024 11:28:28 GMT+0530 (India


Standard Time)'
},
{
_id: ObjectId('669a012fbb5b4fc21146b79f'),
name: 'abc',
age: 25,
gender: 'female'
},
{
_id: ObjectId('669a0268bb5b4fc21146b7a0'),
name: 'abc',
age: 25,
gender: 'female'
}
]
IV SEM MONGO DB LAB MANUAL DEPT Of AIML

3 a. Execute query selectors (comparison selectors, logical selectors ) and list


out the results on any collection

A.1 comparison selectors

Query: db.collection.find({ age: { $gt: 30 } })

Output:
[
{
_id: ObjectId('669a0268bb5b4fc21146b7a1'),
name: 'ghj',
age: 55,
gender: 'male'
},
{
_id: ObjectId('669a0268bb5b4fc21146b7a2'),
name: 'cvb',
age: 52,
gender: 'female'
},
{
_id: ObjectId('669a0268bb5b4fc21146b7a3'),
name: 'yhn',
age: 80,
gender: 'female'
}
]

Logical selectors

Query: db.collection.find({ $and: [{ age: { $gt: 25 } }, { gender: "male" }] })


Output:
[
{
_id: ObjectId('669a0268bb5b4fc21146b7a1'),
name: 'ghj',
age: 55,
gender: 'male'
},
{
_id: ObjectId('669a0268bb5b4fc21146b7a5'),
name: 'jhc',
age: 29,
IV SEM MONGO DB LAB MANUAL DEPT Of AIML

gender: 'male'
},
{
_id: ObjectId('669a0268bb5b4fc21146b7a6'),
name: 'jhc',
age: 29,
gender: 'male'
}
]

B. Execute query selectors (Geospatial selectors, Bitwise selectors ) and list outthe
results on any collection

B.1 Geospatial selectors:


1. Create a Database and Collection:

use geospatialDB
db.createCollection("locations")

2. Create a Geospatial Index:

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


3. Insert Geospatial Data:

db.locations.insertMany([
{
name: "Location A",
location: {
type: "Point",
coordinates: [ -73.97, 40.77 ]
}
}
{
name: "Location B",
location: {
type: "Point",
coordinates: [ -73.88, 40.78 ]
}
}
])

Find Nearest Locations:


db.locations.find({
location: {
$near: {
$geometry: {
type: "Point",
coordinates: [ -73.95, 40.75 ] // Replace with your coordinates
},
$maxDistance: 5000 // Optional: max distance in meters
}}})
IV SEM MONGO DB LAB MANUAL DEPT Of AIML

Output:
[
{
_id: ObjectId('66575a5442058b31e6cdcdf8'),
name: 'location A',
location: { type: 'Point', coordinates: [ -73.97, 40.77 ] }
}
]

Bitwise selectors:
B.2
db.collection.insertMany([
{ "_id": 1, "field": 5 }, // binary: 0101
{ "_id": 2, "field": 9 }, // binary: 1001
{ "_id": 3, "field": 12 } // binary: 1100
])

1. db.collection.find({ field: { $bitsAllSet: [0, 2] } })


2. db.collection.find({ field: { $bitsAnySet: [1] } })
3. db.collection.find({ field: { $bitsAllClear: [0, 2] } })
4. db.collection.find({ field: { $bitsAnyClear: [0, 2] } })
IV SEM MONGO DB LAB MANUAL DEPT Of AIML

4 Create and demonstrate how projection operators ($, $elematch and $slice) would be
used in the MondoDB.

db.marklist.insertMany([
{ "_id": 1, "semester": 1, "grades": [70, 87, 90] },
{ "_id": 2, "semester": 1, "grades": [90, 88, 92] },
{ "_id": 3, "semester": 1, "grades": [85, 100, 90] },
{ "_id": 4, "semester": 2, "grades": [79, 85, 80] },
{ "_id": 5, "semester": 2, "grades": [88, 88, 92] },
{ "_id": 6, "semester": 2, "grades": [95, 90, 96] }])

Query:

(1) db.marklist.find( { semester: 1, grades: { $gte: 85 } }, { "grades.$": 1 } )


Output:
[
{ _id: 1, grades: [ 87 ] },
{ _id: 2, grades: [ 90 ] },
{ _id: 3, grades: [ 85 ] }
]

(2) db.marklist.find({ semester: 2, grades: { $gte: 85 } }, { "grades.$": 1 })


Output:
[
{ _id: 4, grades: [ 85 ] },
{ _id: 5, grades: [ 88 ] },
{ _id: 6, grades: [ 95 ] }
]

(3) db.marklist.find({"grades": {"$elemMatch": {"$gte": 95} }})


Output:

[
{ _id: 3, semester: 1, grades: [ 85, 100, 90 ] },
{ _id: 6, semester: 2, grades: [ 95, 90, 96 ] }
]

4) db.marklist.find({},{"grades": {"$slice": 1 } })
output:
{ _id: 1, semester: 1, grades: [ 70 ] },
{ _id: 2, semester: 1, grades: [ 90 ] },
{ _id: 3, semester: 1, grades: [ 85 ] },
{ _id: 4, semester: 2, grades: [ 79 ] },
{ _id: 5, semester: 2, grades: [ 88 ] },
{ _id: 6, semester: 2, grades: [ 95 ] }
IV SEM MONGO DB LAB MANUAL DEPT Of AIML

5 Execute Aggregation operations ($avg, $min,$max, $push, $addToSet etc.).


students encourage to execute several queries to demonstrate various aggregation
operators)

db.sales.insertMany([
{ "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-01-
01T08:00:00Z") }
{ "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : ISODate("2014-02-
03T09:00:00Z") }
{ "_id" : 3, "item" : "xyz", "price" : 5, "quantity" : 5, "date" : ISODate("2014-02-
03T09:05:00Z") }
{ "_id" : 4, "item" : "abc", "price" : 10, "quantity" : 10, "date" : ISODate("2014-02-
15T08:00:00Z") }
{ "_id" : 5, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2014-02-
15T09:05:00Z") }
{ "_id" : 6, "item" : "xyz", "price" : 5, "quantity" : 5, "date" : ISODate("2014-02-
15T12:05:10Z") }
{ "_id" : 7, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2014-02-
15T14:12:12Z") }])

1.$avg:

db.sales.aggregate([
{
$group : {
_id : null,
averageQuantity: { $avg: "$quantity" },
count: { $sum: 1 }
}
}
])

Output:

[ { _id: null, averageQuantity: 7.875, count: 8 } ]

2.$min.

db.sales.aggregate([{ $group: { _id: null, minqty: { $min: "$quantity" } } }])

output:
[ { _id: null, minqty: 1 } ]
IV SEM MONGO DB LAB MANUAL DEPT Of AIML

3.$max:

db.sales.aggregate([{ $group: { _id: null, maxqty: { $max: "$quantity" } } }])

Output:
[ { _id: null, maxqty: 20 } ]

4.$push:

db.sales.aggregate(
[
{
$group:
{
_id: { day: { $dayOfYear: "$date"}, year: { $year: "$date" } },
itemsSold: { $push: { item: "$item", quantity: "$quantity" } }
}
}
]

output:

"_id" : { "day" : 46, "year" : 2014 },


"itemsSold" : [
{ "item" : "abc", "quantity" : 10 },
{ "item" : "xyz", "quantity" : 10 },
{ "item" : "xyz", "quantity" : 5 },
{ "item" : "xyz", "quantity" : 10 }
]
}

{
"_id" : { "day" : 34, "year" : 2014 },
"itemsSold" : [
{ "item" : "jkl", "quantity" : 1 },
{ "item" : "xyz", "quantity" : 5 }
]
}
{
"_id" : { "day" : 1, "year" : 2014 },
"itemsSold" : [ { "item" : "abc", "quantity" : 2 } ]
}
IV SEM MONGO DB LAB MANUAL DEPT Of AIML

5. )$addToSet

db.sales.aggregate(
[
{
$group:
{
_id: { day: { $dayOfYear: "$date"}, year: { $year: "$date" } },
itemsSold: { $addToSet: "$item" }
}
}
]
)

output:

{ "_id" : { "day" : 46, "year" : 2014 }, "itemsSold" : [ "xyz", "abc" ] }


{ "_id" : { "day" : 34, "year" : 2014 }, "itemsSold" : [ "xyz", "jkl" ] }
{ "_id" : { "day" : 1, "year" : 2014 }, "itemsSold" : [ "abc" ] }
IV SEM MONGO DB LAB MANUAL DEPT Of AIML

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)

db.orders.insertMany( [
{ _id: 0, name: "Pepperoni", size: "small", price: 19, quantity: 10, date: ISODate(
"2021-03-13T08:14:30Z" ) },
{ _id: 1, name: "Pepperoni", size: "medium", price: 20, quantity: 20, date : ISODate(
"2021-03-13T09:13:24Z" ) },
{ _id: 2, name: "Pepperoni", size: "large", price: 21, quantity: 30, date : ISODate(
"2021-03-17T09:22:12Z" ) },
{ _id: 3, name: "Cheese", size: "small", price: 12, quantity: 15, date : ISODate( "2021-
03-13T11:21:39:36Z" ) },
{ _id: 4, name: "Cheese", size: "medium", price: 13, quantity:50, date : ISODate(
"2022-01-12T21:23:13.331Z" ) },
{ _id: 5, name: "Cheese", size: "large", price: 14, quantity: 10, date : ISODate( "2022-
01-12T05:08:13Z" ) },
{ _id: 6, name: "Vegan", size: "small", price: 17, quantity: 10, date : ISODate( "2021-
01-13T05:08:13Z" ) },
{ _id: 7, name: "Vegan", size: "medium", price: 18, quantity: 10, date : ISODate(
"2021-01-13T05:10:13Z" ) }] )

$match & $group


db.orders.aggregate( [
// Stage 1: Filter pizza order documents by pizza size
{
$match: { size: "medium" }
},
// Stage 2: Group remaining documents by pizza name and calculate total quantity
{
$group: { _id: "$name", totalQuantity: { $sum: "$quantity" } }
}
])

$sort:
db.restaurants.insertMany( [
{ "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan"},
{ "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens"},
{ "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn"},
{ "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan"},
{ "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn"},
])

db.restaurants.aggregate( [ { $sort : { borough : 1 } } ] )


IV SEM MONGO DB LAB MANUAL DEPT Of AIML

Output:

{ "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn" }


{ "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn" }
{ "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan" }
{ "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan" }
{ "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens" }

$project:
db.restaurants.aggregate([{$project:{name:1}}])

output:
[
{ _id: 1, name: 'Central Park Cafe' },
{ _id: 2, name: 'Rock A Feller Bar and Grill' },
{ _id: 3, name: 'Empire State Pub' },
{ _id: 4, name: "Stan's Pizzaria" },
{ _id: 5, name: "Jane's Deli" }
]

Skip:
db.restaurants.aggregate([{ $skip : 2 }]);

output:
[
{ _id: 3, name: 'Empire State Pub', borough: 'Brooklyn' },
{ _id: 4, name: "Stan's Pizzaria", borough: 'Manhattan' },
{ _id: 5, name: "Jane's Deli", borough: 'Brooklyn' }
]
IV SEM MONGO DB LAB MANUAL DEPT Of AIML

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

Inserting values:
db.listings_and_reviews.insertMany([
{
listing_url: "https://example.com/listing/2",
name: "Cozy Cabin",
address: "456 Forest Rd, City, Country",
host: {
picture_url: "https://example.com/host/2.jpg"
}
},

{
listing_url: "https://example.com/listing/3",
name: "Modern Loft",
address: "789 Skyline Dr, City, Country",
host: {
picture_url: "https://example.com/host/3.jpg"
}
}
])

QUERY:
db.listings_and_reviews.find(
{ "host.picture_url": { $exists: true, $ne: null, $ne: "" } },
{
"listing_url": 1,
"name": 1,
"address": 1,
"host.picture_url": 1
}
)
IV SEM MONGO DB LAB MANUAL DEPT Of AIML

Output:
[
{
_id: ObjectId('665ff9827f1e6a6c00cdcdf6'),
listing_url: 'https://example.com/listing/2',
name: 'Cozy Cabin',
address: '456 Forest Rd, City, Country',
host: { picture_url: 'https://example.com/host/2.jpg' }
},
{
_id: ObjectId('665ff9827f1e6a6c00cdcdf7'),
listing_url: 'https://example.com/listing/3',
name: 'Modern Loft',
address: '789 Skyline Dr, City, Country',
host: { picture_url: 'https://example.com/host/3.jpg' }
},
{
_id: ObjectId('6660027c22c200c983cdcdf6'),
listing_url: 'https://example.com/listing/2',
name: 'Cozy Cabin',
address: '456 Forest Rd, City, Country',
host: { picture_url: 'https://example.com/host/2.jpg' }
},
{
_id: ObjectId('6660027c22c200c983cdcdf7'),
listing_url: 'https://example.com/listing/3',
name: 'Modern Loft',
address: '789 Skyline Dr, City, Country',
host: { picture_url: 'https://example.com/host/3.jpg' }
}
]
IV SEM MONGO DB LAB MANUAL DEPT Of AIML

b. Using E-commerce collection write a query to display reviews summary.

Insert the values:


db.ecommerce.insertMany([
{
product_name: "Product A",
reviews: [
{
rating: 3,
comment: "Average product.",
date: new Date("2023-01-15")
},
{
rating: 2,
comment: "Not satisfied with the quality.",
date: new Date("2023-07-05")
}
]
},

{
product_name: "Product B",
reviews: [
{
rating: 3,
comment: "Average product.",
date: new Date("2023-02-20")
},
{
rating: 2,
comment: "Not satisfied with the quality.",
date: new Date("2023-04-05")
}
]
},
{
product_name: "Product C",
reviews: [
{
rating: 5,
comment: "Highly recommend!",
date: new Date("2023-05-18")
}
]
}
])
IV SEM MONGO DB LAB MANUAL DEPT Of AIML

Query:
db.ecommerce.aggregate([
{ $unwind: "$reviews" }, // Deconstruct the reviews array
{
$group: {
_id: null,
totalReviews: { $sum: 1 }, // Count total number of reviews
averageRating: { $avg: "$reviews.rating" }, // Calculate average rating
latestReview: { $max: "$reviews.date" }, // Find the most recent review date
oldestReview: { $min: "$reviews.date" } // Find the oldest review date
}
},
{
$project: {
_id: 0, // Exclude the _id field from the results
totalReviews: 1,
averageRating: 1,
latestReview: 1,
oldestReview: 1
}
}
])

Output:
[
{
totalReviews: 5,
averageRating: 3,
latestReview: ISODate('2023-07-05T00:00:00.000Z'),
oldestReview: ISODate('2023-01-15T00:00:00.000Z')
}
]
IV SEM MONGO DB LAB MANUAL DEPT Of AIML

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


sparse, compound and multikey indexes)
1. Unique Index
A unique index ensures that the indexed field does not contain duplicate values.

synatax:
db.exampleCollection.createIndex({ "fieldName": 1 }, { unique: true })

Query:
db.exampleCollection.insertMany([
{ _id: 1, username: "alice", age: 25 },
{ _id: 2, username: "bob", age: 30 }
]);

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


// This ensures that the "username" field contains unique values.

2. Sparse Index
A sparse index only indexes the documents that contain the indexed field,
skipping documents that do not have the field.

synatax:
db.exampleCollection.createIndex({ "fieldName": 1 }, { sparse: true })

Query:
db.exampleCollection.insertMany([
{ _id: 3, username: "carol" },
{ _id: 4, username: "dave", email: "[email protected]" }
]);

db.exampleCollection.createIndex({ "email": 1 }, { sparse: true });


// This indexes only documents where "email" exists.
3. Compound Index
A compound index indexes multiple fields within a collection. The order of the
fields in the index is significant.

synatax:
db.exampleCollection.createIndex({ "field1": 1, "field2": -1 })

Query:
db.exampleCollection.insertMany([
{ _id: 5, firstName: "eve", lastName: "johnson" },
{ _id: 6, firstName: "frank", lastName: "smith" }
IV SEM MONGO DB LAB MANUAL DEPT Of AIML

]);

db.exampleCollection.createIndex({ "firstName": 1, "lastName": -1 });


// This creates an index on both "firstName" and "lastName" fields.
4. Multikey Index
A multikey index is created on an array field, allowing MongoDB to index the
individual elements within the array.

synatax:
db.exampleCollection.createIndex({ "arrayField": 1 })

Query:
db.exampleCollection.insertMany([
{ _id: 7, tags: ["mongodb", "database", "nosql"] },
{ _id: 8, tags: ["indexing", "performance"] }
]);

db.exampleCollection.createIndex({ "tags": 1 });


// This creates an index on the elements of the "tags" array.

b. Demonstrate optimization of queries using indexes.

Unique Index:
db.optimization.createIndex({ "name": 1 }, { unique: true });

db.optimization.find({ age: { $gt: 30 } }).explain("executionStats");

Compound Index:
db.exampleCollection.createIndex({ name: 1, age: 1 });

db.exampleCollection.find({ name: "Alice", age: 25 }).explain("executionStats");

Multiple Index:
db.exampleCollection.createIndex({ tags: 1 });

db.exampleCollection.find({ tags: "mongodb" }).explain("executionStats");

Output:

{ acknowledged: true, insertedIds: { '0': 3, '1': 4 } }


exampleCollection> db.exampleCollection.createIndex({ "email": 1 }, { sparse: true
});

Output:

email_1
IV SEM MONGO DB LAB MANUAL DEPT Of AIML

9 a. Develop a query to demonstrate Text search using catalog data collection


for agiven word

use catalogDB

db.catalog.insertMany
([ { title: "Data Science for Beginners", description: "An introductory book on data
science." },
{ title: "Advanced Machine Learning", description: "A deep dive into machine learning
algorithms." },
{ title: "MongoDB in Action", description: "A comprehensive guide to MongoDB." }, {
title: "Data Analysis with Python", description: "Learn data analysis techniques using
Python." },
{ title: "Introduction to Big Data", description: "Basics of big data technologies." } ])

# Create text index


db.catalog.createIndex({ title: "text", description: "text" })

# Perform text search


db.catalog.find({ $text: { $search: "data" } })

Output:

[
{
_id: ObjectId('667fb027b7d7b34e21cdcdfa'),
title: 'Introduction to Big Data',
description: 'Basics of big data technologies.'
},
{
_id: ObjectId('667fb027b7d7b34e21cdcdf6'),
title: 'Data Science for Beginners',
description: 'An introductory book on data science.'
},
{
_id: ObjectId('667fb027b7d7b34e21cdcdf9'),
title: 'Data Analysis with Python',
description: 'Learn data analysis techniques using Python.'
}
]
IV SEM MONGO DB LAB MANUAL DEPT Of AIML

b. Develop queries to illustrate excluding documents with certain words and


phrases

Method:1
db.catalog.aggregate([
{ $match: { $text: { $search: "data" } } },
{ $match: { title: { $not: /Python/ }, description: { $not: /Python/ } } }
])

Method:2

db.catalog.aggregate([
{ $match: { $text: { $search: "data" } } },
{ $match: {
$and: [
{ title: { $not: /Python/ } },
{ description: { $not: /Python/ } }
]
}}
])

Output:

[
{
_id: ObjectId('667fb027b7d7b34e21cdcdfa'),
title: 'Introduction to Big Data',
description: 'Basics of big data technologies.'
},
{
_id: ObjectId('667fb027b7d7b34e21cdcdf6'),
title: 'Data Science for Beginners',
description: 'An introductory book on data science.'
}
]
10 Develop an aggregation pipeline to illustrate Text search on Catalog data collection.

db.catalog.aggregate([
{
$match: {
$text: { $search: "data" }
}
},
{
$match: {
$and: [
{ title: { $not: /Python/ } },
IV SEM MONGO DB LAB MANUAL DEPT Of AIML

{ description: { $not: /Python/ } }


]
}
},
{
$project: {
title: 1,
description: 1,
score: { $meta: "textScore" }
}
},
{
$sort: {
score: { $meta: "textScore" }
}
}
])

Output:

[
{
_id: ObjectId('667fb027b7d7b34e21cdcdfa'),
title: 'Introduction to Big Data',
description: 'Basics of big data technologies.',
score: 1.2916666666666665
},
{
_id: ObjectId('667fb027b7d7b34e21cdcdf6'),
title: 'Data Science for Beginners',
description: 'An introductory book on data science.',
score: 1.2916666666666665
}
]

You might also like