Introduction
Querying helps you find the exact information you need from a large collection of data quickly and easily.
Jump into concepts and practice - no test required
Querying helps you find the exact information you need from a large collection of data quickly and easily.
db.collection.find(query, projection)
query specifies the conditions to match documents.
projection controls which fields to show in the results.
db.users.find({ city: "New York" })db.products.find({ stock: 0 })db.orders.find({ date: { $gte: ISODate("2024-06-01T00:00:00Z") } })This example adds three users and then finds those who live in New York.
db.users.insertMany([
{ name: "Alice", city: "New York" },
{ name: "Bob", city: "Chicago" },
{ name: "Carol", city: "New York" }
])
const results = db.users.find({ city: "New York" }).toArray()
resultsQuerying saves time by filtering only the data you want.
Using projections can make queries faster by returning fewer fields.
MongoDB queries are case-sensitive by default.
Querying helps you find specific data quickly.
You use queries to filter and control what data you get back.
Learning to query well makes working with databases easier and faster.
users?find().find() is valid; others like get(), search(), and select() are invalid in MongoDB.products with documents: {"name": "Pen", "price": 5}{"name": "Notebook", "price": 15}{"name": "Eraser", "price": 3}db.products.find({ price: { $gt: 4 } }) return?price is greater than 4.db.users.find({ age: > 25 })age: > 25, which is invalid syntax in MongoDB.$gt inside an object: { age: { $gt: 25 } }.orders with documents containing status and total. How would you write a query to find all orders with status "shipped" and total greater than 100?status equals "shipped" AND total is greater than 100.{ status: "shipped", total: { $gt: 100 } }.