Mongo DB Final Lab Manual
Mongo DB Final Lab Manual
MongoDB
Course Outcomes:
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)
SOLUTION:
Step 1 : Create the database .
test> use Students
switched to db Students
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'
}
]
{
_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'
}
]
2 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]
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:
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.
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') }
}
[
{
_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 }
]
}
]
]
}
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>
No output because there is no quantity which is greater than 30 in the array.
[
{
_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)
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 } }
])
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.eCommerce.aggregate([
{ $group: { _id: "$product_id", avgRating: { $avg: "$rating" }, totalReviews: {$sum: 1 } } }])
// Unique index
db.collection.createIndex({ username: 1 }, { unique: true })
// Sparse index
db.collection.createIndex({ city: 1 }, { sparse: true })
// Compound index
db.collection.createIndex({ age: 1, city: -1 })
// Multikey index
db.collection.createIndex({ tags: 1 })
// Using index
db.collection.find({ username: "john" }).hint({ username: 1 })
9.a. Develop a query to demonstrate Text search using catalog data collection for a given word:
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'
},
{
_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
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
}
}
])
1. String:
2. Integer:
3. Double:
4. Boolean:
1. $divide operator
2. $add operator:
3. $log operator:
Step 1:
Step 2:
4. $sqrt operator: