MongoDB Manual
MongoDB Manual
MongoDB BDS456B
Manual
(Effective from the academic year 2023-2024)
Semester –IV
Prepared By:
Mrs. Indumathi K
Tutor
Department of CSE- Data Science ACSCE, Bengaluru
MongoDB BDS456B CSE-Data Science
C:\Users\Indu>mongosh
Connecting to:
mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&a
ppName=mongosh+2.2.4
------
------
switched to db products
customer_details
product_details
_id: 'ac7',
brand: 'ACME',
type: 'phone',
price: 320,
rating: 4,
warranty_years: 1,
available: false
_id: 'ac7',
brand: 'ACME',
type: 'phone',
price: 320,
rating: 4,
warranty_years: 1,
available: false
_id: 'ac3',
brand: 'ACME',
type: 'phone',
price: 200,
rating: 3.8,
warranty_years: 1,
available: true
},
_id: 'ac7',
brand: 'ACME',
type: 'phone',
price: 320,
rating: 4,
warranty_years: 1,
available: false
//Insert Query
products> db.product_details.insertOne({_id:'ac8',name:'AC8
Phone',brand:'ACME',type:'phone',price:2000,rating:4.0,warranty_years:2,available:true})
products> db.product_details.find({_id:'ac8'})
_id: 'ac8',
brand: 'ACME',
type: 'phone',
price: 2000,
rating: 4,
warranty_years: 2,
available: true
//Update Query
acknowledged: true,
insertedId: null,
matchedCount: 1,
modifiedCount: 1,
upsertedCount: 0
products> db.product_details.find({_id:'ac8'})
_id: 'ac8',
brand: 'ACME',
type: 'phone',
price: 4000,
rating: 4,
warranty_years: 2,
available: true
//Deletion
products> db.product_details.deleteOne({_id:'ac8'})
products> db.product_details.find({_id:'ac8'})
products>
//projection
products> db.product_details.find({type:'tv'})
_id: ObjectId('507d95d5719dbef170f15c01'),
type: 'tv',
monthly_price: 50,
rating: 3.9,
term_years: 2,
cancel_penalty: 25,
sales_tax: true,
additional_tarriffs: [
Experiment 2:
a. Develop a MongoDB query to select certain fields and ignore some fields of the
documents:
products> db.product_details.find({brand:'ACME'},{name:0,_id:0})
brand: 'ACME',
type: 'phone',
price: 200,
rating: 3.8,
warranty_years: 1,
available: true
},
brand: 'ACME',
type: 'phone',
price: 320,
rating: 4,
warranty_years: 1,
available: false
},
brand: 'ACME',
type: 'phone',
price: 2000,
rating: 4,
warranty_years: 2,
available: true
products> db.product_details.find().limit(5)
_id: 'ac3',
brand: 'ACME',
type: 'phone',
price: 200,
rating: 3.8,
warranty_years: 1,
available: true
},
_id: 'ac7',
brand: 'ACME',
type: 'phone',
price: 320,
rating: 4,
warranty_years: 1,
available: false
},
_id: ObjectId('507d95d5719dbef170f15bf9'),
price: 19,
rating: 2.8,
warranty_years: 0.25,
},
_id: ObjectId('507d95d5719dbef170f15bfa'),
color: 'green',
price: 12,
rating: 1,
warranty_years: 0
},
_id: ObjectId('507d95d5719dbef170f15bfb'),
type: 'warranty',
price: 38,
rating: 5,
warranty_years: 2,
Experiment 3:
products> db.product_details.find({price:{$gt:200}})
_id: 'ac7',
brand: 'ACME',
type: 'phone',
price: 320,
rating: 4,
warranty_years: 1,
available: false
},
_id: ObjectId('66290e738a958f3edf117b7b'),
brand: 'ACME',
type: 'phone',
price: 2000,
rating: 4,
warranty_years: 2,
available: true
Experiment 4:
Create and demonstrate how projection operators ($, $elematch and $slice) would be
used in the MongoDB.
products> db.product_details.find({},{"name.$":1})
_id: ObjectId('507d95d5719dbef170f15bf9'),
},
_id: ObjectId('507d95d5719dbef170f15bfb'),
},
_id: ObjectId('507d95d5719dbef170f15bfe'),
},
_id: ObjectId('507d95d5719dbef170f15bff'),
},
_id: ObjectId('507d95d5719dbef170f15c00'),
},
_id: ObjectId('507d95d5719dbef170f15c01'),
},
products>
db.product_details.find({},{additional_tarriffs:{$elemMatch:{amount:2.25}}})
{ _id: 'ac3' },
{ _id: 'ac7' },
{ _id: ObjectId('507d95d5719dbef170f15bf9') },
{ _id: ObjectId('507d95d5719dbef170f15bfa') },
{ _id: ObjectId('507d95d5719dbef170f15bfb') },
{ _id: ObjectId('507d95d5719dbef170f15bfc') },
{ _id: ObjectId('507d95d5719dbef170f15bfd') },
{ _id: ObjectId('507d95d5719dbef170f15bfe') },
{ _id: ObjectId('507d95d5719dbef170f15bff') },
{ _id: ObjectId('507d95d5719dbef170f15c00') },
_id: ObjectId('507d95d5719dbef170f15c01'),
},
{ _id: ObjectId('66290e738a958f3edf117b7b') }
products> db.product_details.find({},{name:1,brand:1,for:{$slice:1},_id:0})
//find $avg
products> db.product_details.aggregate([{$group:{_id:0,avgprice:{$avg:'$price'}}}])
//find $min
products> db.product_details.aggregate([{$group:{_id:0,minprice:{$min:'$price'}}}])
[ { _id: 0, minprice: 12 } ]
//find $max
products> db.product_details.aggregate([{$group:{_id:0,maxprice:{$max:'$price'}}}])
// $push
products> db.product_details.aggregate([{$group:{_id:0,allnames:{$push:'$name'}}}])
_id: 0,
allnames: [
'AC3 Phone',
'AC7 Phone',
'AC3 Phone'