DATABASE ENIGINEERING
LAB-8
Name- Abhinav Giri
Roll No.- R2142221345
SAP ID- 500110031
Course- B.Tech CSE(FSAI)
Semester- 6
Batch- 3
SCHOOL OF COMPUTER SCIENCE
UPES, DEHRADUN
Lab Exercise 5 - Using Element Operators to
Query Data in MongoDB (mongosh Console)
1. Launch the mongosh Console
2. Set Up a Database and Collection
Switch to a database:
Insert sample data into a collection:
db.inventory.insertMany([ { item: "laptop", qty: 10, price: 800, inStock: true }, { item: "mouse",
qty: 50, price: 20, color: "black" }, { item: "keyboard", qty: 25, price: 45, inStock: false },
{ item: "monitor", qty: 15, price: 150 }, { item: "speaker", qty: 30, dimensions: { length: 10,
width: 5, height: 7 } } ])
1. Query Using $exists
Find documents with the inStock field:
db.inventory.find({ inStock: { $exists: true } })
Find documents without the inStock field:
db.inventory.find({ inStock: { $exists: false } })
Query Using $type
Find documents where the price field is a number:
db.inventory.find({ price: { $type: "number" } }
Find documents where the dimensions field is an object:
db.inventory.find({ dimensions: { $type: "object" } })
Find documents where the color field is a string:
db.inventory.find({ color: { $type: "string" } })