0% found this document useful (0 votes)
549 views25 pages

Mongo DB Final Lab Manual

manual

Uploaded by

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

Mongo DB Final Lab Manual

manual

Uploaded by

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

MongoDB(BDSL456B)

MongoDB

Subject code: BDSL456B IA Marks:50


Hour/week:02 Total Hours:20

Course Outcomes:

At the end of the course, the student will be able to:

1. Make use of MangoDB 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.

Sl No. List of Experiment CO’s Page


No
1 a. Illustration of Where Clause, AND,OR operations in MongoDB. CO1 3-6
b. Execute the Commands of MongoDB and operations in MongoDB : Insert, Query, Update,
Delete and Projection. (Note: use any collection)
[Refer: Book 1 chapter 4].
2 a. Develop a MongoDB query to select certain fields and ignore some fields of the documents from CO1 7-8
any collection.
b. Develop a MongoDB query to display the first 5 documents from the results obtained in a. [use
of limit and find]
[Refe: Book1 Chapter 4, book 2: chapter 5]
3 a. Execute query selectors (comparison selectors, logical selectors ) and list out the results on any CO1
collection
b. Execute query selectors (Geospatial selectors, Bitwise selectors ) and list out the results on any
collection
[Refer: Book 3 Chapter 13]
4 Create and demonstrate how projection operators ($, $elematch and $slice) would be used in the CO1
MondoDB.
[Refer: Book 3 Chapter 14]
5 Execute Aggregation operations ($avg, $min,$max, $push, $addToSet etc.). students encourage to CO2
execute several queries to demonstrate various aggregation operators)
[Refer: Book 3 Chapter 15]
6 Execute Aggregation Pipeline and its operations (pipeline must contain $match, $group, $sort, CO4
$project, $skip etc. students encourage to execute several queries to demonstrate various
aggregation operators)
[Refer book 2: chapter 6 ]
7 a. Find all listings with listing_url, name, address, host_picture_url in the listings and Reviews CO3
collection that have a host with a picture url
b. Using E-commerce collection write a query to display reviews summary.
[Refer Book2: chapter 6]
8 a. Demonstrate creation of different types of indexes on collection (unique, sparse, compound and CO3
multikey indexes) b. Demonstrate optimization of queries using indexes.
[Refer: Book 2: Chapter 8 and Book 3: Chapter 12]
9 a. Develop a query to demonstrate Text search using catalog data collection for a given word CO4
b. Develop queries to illustrate excluding documents with certain words and phrases
[Refer: Book 2: Chapter 9]
10 Develop an aggregation pipeline to illustrate Text search on Catalog data collection. CO4
[Refer: Book 2 :Chapter 9]

Dept. of Computer Science & Engineering (AI & ML) PageNo: 1


MongoDB(BDSL456B)

CONTENT BEYOND SYLLABUS


Sl List of Experiment CO’s Page
No. no

1. Illustration of basic datatypes in MongoDB. CO1

2. Illustration of basic Arthemetic operation. CO1

3. Illustration of String Expression operator. CO1

1 b. Execute the Commands of MongoDB and operations in MongoDB : Insert, Query, Update, Delete and
Dept. of Computer Science & Engineering (AI & ML) PageNo: 2
MongoDB(BDSL456B)

Projection. (Note: use any collection)

SOLUTION:
Step 1 : Create the database .
test> use Students
switched to db Students

Step 2: Insert the value into database using insertone method()


Students> db.student.insertOne({name:"ankit", age:22, usn:01, phno:7899330231, address:'banglore'})
{
acknowledged: true,
insertedId: ObjectId('6626198aff34ca682116c9b5')
}

Step 3: Insert the value into database using insertMany method()


Students> db.student.insertMany([{name:"ankit", age:22, usn:01, phno:7899330231, address:'banglore'},
{name:"jiya", age:25, usn:02, phno:7844330231, address:'bihar'}, {name:"pooja", age:22, usn:03,
phno:8299330231, address:'delhi'}, {name:"priya", age:29, usn:04, phno:8899330231, address:'chenai'},
{name:"anand", age:30, usn:05, phno:7899330232, address:'mumbai'}])
{
acknowledged: true,
insertedIds: {
'0': ObjectId('66261befff34ca682116c9b6'),
'1': ObjectId('66261befff34ca682116c9b7'),
'2': ObjectId('66261befff34ca682116c9b8'),
'3': ObjectId('66261befff34ca682116c9b9'),
'4': ObjectId('66261befff34ca682116c9ba')
}
}
Students> db.student.find()
[
{
_id: ObjectId('6626198aff34ca682116c9b5'),
name: 'ankit',
age: 22,
usn: 1,
phno: 7899330231,
address: 'banglore'
},
{
_id: ObjectId('66261befff34ca682116c9b6'),
name: 'ankit',
age: 22,
usn: 1,
phno: 7899330231,
address: 'banglore'
},
{
_id: ObjectId('66261befff34ca682116c9b7'),
name: 'jiya',
age: 25,
usn: 2,
Dept. of Computer Science & Engineering (AI & ML) PageNo: 3
MongoDB(BDSL456B)

phno: 7844330231,
address: 'bihar'
},
{
_id: ObjectId('66261befff34ca682116c9b8'),
name: 'pooja',
age: 22,
usn: 3,
phno: 8299330231,
address: 'delhi'
},
{
_id: ObjectId('66261befff34ca682116c9b9'),
name: 'priya',
age: 29,
usn: 4,
phno: 8899330231,
address: 'chenai'
},
{
_id: ObjectId('66261befff34ca682116c9ba'),
name: 'anand',
age: 30,
usn: 5,
phno: 7899330232,
address: 'mumbai'
}
]

Step 4: Update the values by using updateone method


Students> db.student.updateOne({address:'banglore'}, {$set:{address:'delhi'}})
{
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
}
Students> db.student.find()
[
{
_id: ObjectId('6626198aff34ca682116c9b5'),
name: 'ankit',
age: 22,
usn: 1,
phno: 7899330231,
address: 'delhi'
},

{
_id: ObjectId('66261befff34ca682116c9b6'),
Dept. of Computer Science & Engineering (AI & ML) PageNo: 4
MongoDB(BDSL456B)

name: 'ankit',
age: 22,
usn: 1,
phno: 7899330231,
address: 'banglore'
},
{
_id: ObjectId('66261befff34ca682116c9b7'),
name: 'jiya',
age: 25,
usn: 2,
phno: 7844330231,
address: 'bihar'
},
{
_id: ObjectId('66261befff34ca682116c9b8'),
name: 'pooja',
age: 22,
usn: 3,
phno: 8299330231,
address: 'delhi'
},
{
_id: ObjectId('66261befff34ca682116c9b9'),
name: 'priya',
age: 29,
usn: 4,
phno: 8899330231,
address: 'chenai'
},
{
_id: ObjectId('66261befff34ca682116c9ba'),
name: 'anand',
age: 30,
usn: 5,
phno: 7899330232,
address: 'mumbai'
}
]

Step 5: Demonstrate Projection

Students> db.student.find({}, {age:1}).pretty()


[
{ _id: ObjectId('6626198aff34ca682116c9b5'), age: 22 },
{ _id: ObjectId('66261befff34ca682116c9b6'), age: 22 },
{ _id: ObjectId('66261befff34ca682116c9b7'), age: 25 },
{ _id: ObjectId('66261befff34ca682116c9b8'), age: 22 },
{ _id: ObjectId('66261befff34ca682116c9b9'), age: 29 },
{ _id: ObjectId('66261befff34ca682116c9ba'), age: 30 }
]
1 a. Illustration of Where Clause, AND,OR operations in MongoDB.
Dept. of Computer Science & Engineering (AI & ML) PageNo: 5
MongoDB(BDSL456B)

Step 1: Demonstrate the and clause:


Students> db.students.find({$and:[{ age:22 },{ address:'banglore'}]})
Step 2: Demonstrate the or clause:
Students> db.student.find({$or:[{address:'banglore' },{ age:22}]})
[
{
_id: ObjectId('6626198aff34ca682116c9b5'),
name: 'ankit',
age: 22,
usn: 1,
phno: 7899330231,
address: 'delhi'
},
{
_id: ObjectId('66261befff34ca682116c9b6'),
name: 'ankit',
age: 22,
usn: 1,
phno: 7899330231,
address: 'banglore'
},
{
_id: ObjectId('66261befff34ca682116c9b8'),
name: 'pooja',
age: 22,
usn: 3,
phno: 8299330231,
address: 'delhi'
}
]

Dept. of Computer Science & Engineering (AI & ML) PageNo: 6


MongoDB(BDSL456B)

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

Students> db.student.insertMany([{name:"ankit",age:22, usn:01, phno:7899330231, address:'banglore'},


{name:"jiya", age:25, usn:02, phno:7844330231, address:'bihar'}, {name:"pooja", age:22, usn:03,
phno:8299330231, address:'delhi'}, {name:"priya", age:29, usn:04, phno:8899330231, address:'chenai'},
{name:"anand", age:30, usn:05, phno:7899330232, address:'mumbai'}])
{
acknowledged: true,
insertedIds: {
'0': ObjectId('66261befff34ca682116c9b6'),
'1': ObjectId('66261befff34ca682116c9b7'),
'2': ObjectId('66261befff34ca682116c9b8'),
'3': ObjectId('66261befff34ca682116c9b9'),
'4': ObjectId('66261befff34ca682116c9ba')
}
}

Students> db.student.find({}, { name: 1, age: 1 })


[
{ _id: ObjectId('6626198aff34ca682116c9b5'), name: 'ankit', age: 22 },
{ _id: ObjectId('66261befff34ca682116c9b6'), name: 'ankit', age: 22 },
{ _id: ObjectId('66261befff34ca682116c9b7'), name: 'jiya', age: 25 },
{ _id: ObjectId('66261befff34ca682116c9b8'), name: 'pooja', age: 22 },
{ _id: ObjectId('66261befff34ca682116c9b9'), name: 'priya', age: 29 },
{ _id: ObjectId('66261befff34ca682116c9ba'), name: 'anand', age: 30 }
]

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

db.collection.find().limit(5)
Students> db.student.find().limit(2)
[
{
_id: ObjectId('6626198aff34ca682116c9b5'),
name: 'ankit',
age: 22,
usn: 1,
phno: 7899330231,
address: 'delhi'
},
{
_id: ObjectId('66261befff34ca682116c9b6'),
name: 'ankit',
age: 22,
usn: 1,
phno: 7899330231,
address: 'banglore'
}

3 a. Execute query selectors (comparison selectors, logical selectors ) and list out the results on any
Dept. of Computer Science & Engineering (AI & ML) PageNo: 7
MongoDB(BDSL456B)

collection .

COMPARSION SELECTOR

Students> db.student.find({age:{$gt:22}})
[
{
_id: ObjectId('66261befff34ca682116c9b7'),
name: 'jiya',
age: 25,
usn: 2,
phno: 7844330231,
address: 'bihar'
},
{
_id: ObjectId('66261befff34ca682116c9b9'),
name: 'priya',
age: 29,
usn: 4,
phno: 8899330231,
address: 'chenai'
},
{
_id: ObjectId('66261befff34ca682116c9ba'),
name: 'anand',
age: 30,
usn: 5,
phno: 7899330232,
address: 'mumbai'
}
]

LOGICAL SELECTORS:

Students> db.student.find({$and:[{age:{$lte:40}}, {address:"banglore"}]})


[
{
_id: ObjectId('66261befff34ca682116c9b6'),
name: 'ankit',
age: 22,
usn: 1,
phno: 7899330231,
address: 'banglore'
}
]

Dept. of Computer Science & Engineering (AI & ML) PageNo: 8


MongoDB(BDSL456B)

Students> db.student.find({$nor:[{age:{$lte:40}}, {address:"banglore"}]})


No output because nor does not match the clauses.

3 b. Execute query selectors (Geospatial selectors, Bitwise selectors ) and list out the results on any
collection.

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

4. Create and demonstrate how projection operators ($, $elematch and $slice) would be used in the
Dept. of Computer Science & Engineering (AI & ML) PageNo: 9
MongoDB(BDSL456B)

MondoDB.

Step 1: insertmany values to the biodata collection.


employee> db.biodata.insertMany([{ name: 'radha', age: 26, hobbies: ['walk', 'cricket'], experience:
[{ company: 'IBM', duration: 1.5 }, { company: 'wipro', duration: 7 }, { company: 'ABC', duration: 5 }] }] )
{
acknowledged: true,
insertedIds: { '0': ObjectId('662c889e003795f1bc16c9b6') }
}

employee> db.biodata.insertMany([{ name: 'priya', age: 39, hobbies: ['walk', 'cricket'], experience: [{ company:
'techmahindra', duration: 1.5 }, { company: 'amazon', duration: 13 }, { company: 'XYZ', duration: 5.2 }] }] )
{
acknowledged: true,
insertedIds: { '0': ObjectId('662c88ef003795f1bc16c9b7') }
}

Step 2 : employee> db.biodata.find()


[
{
_id: ObjectId('662c8554003795f1bc16c9b5'),
name: 'jiya',
age: 22,
hobbies: [ 'walk', 'cricket' ],
experience: [ { company: 'TCS', duration: 6 }, { company: 'LT', duration: 5 } ]
},
{
_id: ObjectId('662c889e003795f1bc16c9b6'),
name: 'radha',
age: 26,
hobbies: [ 'walk', 'cricket' ],
experience: [
{ company: 'IBM', duration: 1.5 },
{ company: 'wipro', duration: 7 },
{ company: 'ABC', duration: 5 }
]
},
{
_id: ObjectId('662c88ef003795f1bc16c9b7'),
name: 'priya',
age: 39,
hobbies: [ 'walk', 'cricket' ],
experience: [
{ company: 'techmahindra', duration: 1.5 },
{ company: 'amazon', duration: 13 },
{ company: 'XYZ', duration: 5.2 }
]
}
]

Step 3: employee> db.biodata.find({hobbies:'cricket'})


Dept. of Computer Science & Engineering (AI & ML) PageNo: 10
MongoDB(BDSL456B)

[
{
_id: ObjectId('662c8554003795f1bc16c9b5'),
name: 'jiya',
age: 22,
hobbies: [ 'walk', 'cricket' ],
experience: [ { company: 'TCS', duration: 6 }, { company: 'LT', duration: 5 } ]
},
{
_id: ObjectId('662c889e003795f1bc16c9b6'),
name: 'radha',
age: 26,
hobbies: [ 'walk', 'cricket' ],
experience: [
{ company: 'IBM', duration: 1.5 },
{ company: 'wipro', duration: 7 },
{ company: 'ABC', duration: 5 }
]
},
{
_id: ObjectId('662c88ef003795f1bc16c9b7'),
name: 'priya',
age: 39,
hobbies: [ 'walk', 'cricket' ],
experience: [
{ company: 'techmahindra', duration: 1.5 },
{ company: 'amazon', duration: 13 },
{ company: 'XYZ', duration: 5.2 }
]
}
]

Step 4: Demonstration of elemMatch.


products> use product2
switched to db product2
product2> db.product1.insertMany([{product_details: [{ name: 'TV', quantity: 11 }, { name: 'fridge', quantity:
25 }, { name: 'car', quantity: 5 }] }] )
{
acknowledged: true,
insertedIds: { '0': ObjectId('663074191ebd416a0516c9b7') }
}
product2> db.product1.find()
[
{
_id: ObjectId('663074191ebd416a0516c9b7'),
product_details: [
{ name: 'TV', quantity: 11 },
{ name: 'fridge', quantity: 25 },
{ name: 'car', quantity: 5 }

]
}
Dept. of Computer Science & Engineering (AI & ML) PageNo: 11
MongoDB(BDSL456B)

product2>db.product1.find({$and:[{"product_details.name":'TV'},{"product_details.quantity":
{$gt:10}}]})
[
{
_id: ObjectId('663074191ebd416a0516c9b7'),
product_details: [
{ name: 'TV', quantity: 11 },
{ name: 'fridge', quantity: 25 },
{ name: 'car', quantity: 5 }
]
}
]

product2> db.product1.find({product_details:{$elemMatch:{quantity:{$gt:22}, name:'fridge'}}})


[
{
_id: ObjectId('663074191ebd416a0516c9b7'),
product_details: [
{ name: 'TV', quantity: 11 },
{ name: 'fridge', quantity: 25 },
{ name: 'car', quantity: 5 }
]
}
]
product2> db.product1.find({product_details:{$elemMatch:{quantity:{$gt:30}, name:'fridge'}}})

product2>
No output because there is no quantity which is greater than 30 in the array.

Step 5 : Demonstration of $slice method

test> use junkfood


switched to db junkfood
junkfood> db.junk.insertMany([{ name: 'radha', age: 26, junkfoods: ['maagi', 'aloo tiki','burger','shevpuri']},
{ name: 'priya', age: 29, junkfoods: ['samosa', 'pizza','noodles', 'panipuri']}, { name: 'anu', age: 32, junkfoods:
['gobi', 'pav bhaji','sandwich','french fries']}, { name: 'ram', age: 44, junkfoods: ['puff',
'chips','coldrinks','chats'] }] )
{
acknowledged: true,
insertedIds: {
'0': ObjectId('6630912d4618e0e6f116c9b5'),
'1': ObjectId('6630912d4618e0e6f116c9b6'),
'2': ObjectId('6630912d4618e0e6f116c9b7'),
'3': ObjectId('6630912d4618e0e6f116c9b8')
}
}

junkfood> db.junk.find({}, {'junkfoods':{$slice:2}})


Dept. of Computer Science & Engineering (AI & ML) PageNo: 12
MongoDB(BDSL456B)

[
{
_id: ObjectId('6630912d4618e0e6f116c9b5'),
name: 'radha',
age: 26,
junkfoods: [ 'maagi', 'aloo tiki' ]
},
{
_id: ObjectId('6630912d4618e0e6f116c9b6'),
name: 'priya',
age: 29,
junkfoods: [ 'samosa', 'pizza' ]
},
{
_id: ObjectId('6630912d4618e0e6f116c9b7'),
name: 'anu',
age: 32,
junkfoods: [ 'gobi', 'pav bhaji' ]
},
{
_id: ObjectId('6630912d4618e0e6f116c9b8'),
name: 'ram',
age: 44,
junkfoods: [ 'puff', 'chips' ]
}
]

5. Execute Aggregation operations ($avg, $min,$max, $push, $addToSet etc.). students encourage to
Dept. of Computer Science & Engineering (AI & ML) PageNo: 13
MongoDB(BDSL456B)

execute several queries to demonstrate various aggregation operators)

Step 1: To find the average of the student age:

aggregation>db.student1.insertMany([{name:'jiya',age:22,address:'delhi',marks:87.2},{name:'priya',age:25,
address:'bihar',marks:98.2},{name:'aditya',age:33,address:'pune',marks:54.2},
{name:'ram',age:29,address:'banglore',marks:76.2},{name:'sita',age:20,address:'mumbai',marks:33.2}])
{
acknowledged: true,
insertedIds: {
'0': ObjectId('66371c9554e283dc6e16c9b5'),
'1': ObjectId('66371c9554e283dc6e16c9b6'),
'2': ObjectId('66371c9554e283dc6e16c9b7'),
'3': ObjectId('66371c9554e283dc6e16c9b8'),
'4': ObjectId('66371c9554e283dc6e16c9b9')
}
}

aggregation> db.student1.aggregate([{$group:{_id:null,avgAge:{$avg:"$age"}}}])
Output:
[ { _id: null, avgAge: 25.8 } ]

aggregation> db.student1.aggregate([{$group:{_id:null,minAge:{$min:"$age"}}}])
Output:
[ { _id: null, minAge: 20 } ]

aggregation> db.student1.aggregate([{$group:{_id:null,maxAge:{$max:"$age"}}}])
Output:
[ { _id: null, maxAge: 33 } ]

aggregation> db.student1.aggregate([{$group:{_id:null,allNames:{$push:"$name"}}}])
[
{ _id: null, allNames: [ 'jiya', 'priya', 'aditya', 'ram', 'sita' ] } ]

aggregation>db.student1.aggregate([{$group:{_id:null,uniqueNames:{$addToSet:"$name"}}}])
[
{
_id: null,
uniqueNames: [ 'jiya', 'aditya', 'ram', 'priya', 'sita' ]
}
]

6. Execute Aggregation Pipeline and its operations (pipeline must contain $match, $group, $sort,
Dept. of Computer Science & Engineering (AI & ML) PageNo: 14
MongoDB(BDSL456B)

$project, $skip etc. students encourage to execute several queries to demonstrate various aggregation
operators)

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

Dept. of Computer Science & Engineering (AI & ML) PageNo: 15


MongoDB(BDSL456B)

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.find({}, { listing_url: 1, name: 1, address: 1, host_picture_url: 1 })

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

db.eCommerce.aggregate([
{ $group: { _id: "$product_id", avgRating: { $avg: "$rating" }, totalReviews: {$sum: 1 } } }])

Dept. of Computer Science & Engineering (AI & ML) PageNo: 16


MongoDB(BDSL456B)

8.a. Demonstrate creation of different types of indexes on collection:

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

8. b. Demonstrate optimization of queries using indexes:

// Using index
db.collection.find({ username: "john" }).hint({ username: 1 })

// Explain query execution plan


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

Dept. of Computer Science & Engineering (AI & ML) PageNo: 17


MongoDB(BDSL456B)

9.a. Develop a query to demonstrate Text search using catalog data collection for a given word:

test> use content1


switched to db content1

content1> db.content.insertMany([{name:'priya', line:'i love dog'}, {name:'jiya', line:' i love food'}, {ram:'i
love travelling'}, {name:'pooja', line:'i love sleeping'}])
{
acknowledged: true,
insertedIds: {
'0': ObjectId('663c6fe73789f982e716c9b5'),
'1': ObjectId('663c6fe73789f982e716c9b6'),
'2': ObjectId('663c6fe73789f982e716c9b7'),
'3': ObjectId('663c6fe73789f982e716c9b8')
}
}
content1> db.content.find()
[
{
_id: ObjectId('663c6fe73789f982e716c9b5'),
name: 'priya',
line: 'i love dog'
},
{
_id: ObjectId('663c6fe73789f982e716c9b6'),
name: 'jiya',
line: ' i love food'
},
{
_id: ObjectId('663c6fe73789f982e716c9b7'),
ram: 'i love travelling'
},
{
_id: ObjectId('663c6fe73789f982e716c9b8'),
name: 'pooja',
line: 'i love sleeping'
}
]

content1> db.content.createIndex({name:"text",line:"text"})
name_text_line_text
content1> db.content.find({$text:{$search:"love"}})
[
{
_id: ObjectId('663c6fe73789f982e716c9b8'),
name: 'pooja',
line: 'i love sleeping'
},

Dept. of Computer Science & Engineering (AI & ML) PageNo: 18


MongoDB(BDSL456B)

{
_id: ObjectId('663c6fe73789f982e716c9b6'),
name: 'jiya',
line: ' i love food'
},
{
_id: ObjectId('663c6fe73789f982e716c9b5'),
name: 'priya',
line: 'i love dog'
}
]

content1> db.content.find({$text:{$search:"dog"}})
[
{
_id: ObjectId('663c6fe73789f982e716c9b5'),
name: 'priya',
line: 'i love dog'
}
]

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

content1> db.content2.insertMany([{name:'priya', line:'i love dog and cats'}, {name:'jiya', line:' i love cat and
dog'}, {ram:'i love cow and peacock but i like birds'}])

{
acknowledged: true,
insertedIds: {
'0': ObjectId('663c73503789f982e716c9b9'),
'1': ObjectId('663c73503789f982e716c9ba'),
'2': ObjectId('663c73503789f982e716c9bb')
}
}
content1> db.content2.createIndex({name:"text",line:"text"})
name_text_line_text

content1> db.content2.find({$text:{$search:"dog -cow"}})


[
{
_id: ObjectId('663c73503789f982e716c9ba'),
name: 'jiya',
line: ' i love cat and dog'
},
{
_id: ObjectId('663c73503789f982e716c9b9'),
name: 'priya',
line: 'i love dog and cats'
}
]
Dept. of Computer Science & Engineering (AI & ML) PageNo: 19
MongoDB(BDSL456B)

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

db.Catalog.aggregate([
{
$search: {
index: 'default', // The name of the search index to use
text: {
query: 'your_search_query_here', // The text to search for
path: 'description' // The field in the documents to search
}
}
},
{
$project: {
_id: 0, // Exclude the _id field from the output
productName: 1, // Include the productName field in the output
description: 1, // Include the description field in the output
score: { $meta: 'searchScore' } // Include the search score in the output
}
}
])

Dept. of Computer Science & Engineering (AI & ML) PageNo: 20


MongoDB(BDSL456B)

CONTENT BEYOND SYLLABUS

1. Illustration of different datatypes in MongoDB

1. String:

2. Integer:

Dept. of Computer Science & Engineering (AI & ML) PageNo: 21


MongoDB(BDSL456B)

3. Double:

4. Boolean:

Dept. of Computer Science & Engineering (AI & ML) PageNo: 22


MongoDB(BDSL456B)

2. Illustration of basic arthemetic operations in MongoDB.

1. $divide operator

2. $add operator:

3. $log operator:

Step 1:

Dept. of Computer Science & Engineering (AI & ML) PageNo: 23


MongoDB(BDSL456B)

Step 2:

4. $sqrt operator:

Dept. of Computer Science & Engineering (AI & ML) PageNo: 24


MongoDB(BDSL456B)

Dept. of Computer Science & Engineering (AI & ML) PageNo: 25

You might also like