Simple Query
Populate inventory collection with the data to start using find query.
db.inventory.insertMany([
{ item: "journal", qty: 25, status: "L" },
{ item: "notebook", qty: 50, status: "A" },
{ item: "paper", qty: 100, status: "H" },
{ item: "planner", qty: 75, status: "A" },
{ item: "postcard", qty: 45, status: "L" },
{ item: "bluebook", qty: 100, status: "H" },
{ item: "dbbook", qty: 75, status: "A" },
{ item: "bookmark", qty: 45, status: "L" }
]);
Find all
Get all records from inventory
- Node.js
- Python
- MongoDB
const cursor = db.collection('inventory').find({});
cursor = db.inventory.find({})
db.inventory.find({})
Equality condition
Get records from inventory with the status equals "D"
- Node.js
- Python
- MongoDB
const cursor = db.collection('inventory').find({ status: 'D' });
cursor = db.inventory.find({"status": "D"})
db.inventory.find({status: "D"})
AND condition
Get records from inventory with the status equals "D" AND qty less than 30.
- Node.js
- Python
- MongoDB
const cursor = db.collection('inventory').find({
status: 'A',
qty: { $lt: 30 },
});
cursor = db.inventory.find({"status": "A", "qty": {"$lt": 30}})
db.inventory.find({ status: "A", qty: { $lt: 30 }})
OR condition
Get records from inventory with the status equals "A" OR qty less than 30.
- Node.js
- Python
- MongoDB
const cursor = db.collection('inventory').find({
status: 'A',
qty: { $lt: 30 },
});
cursor = db.inventory.find({"status": "A", "qty": {"$lt": 30}})
db.inventory.find({ status: "A", qty: { $lt: 30 }})