Finding and Querying

View saved

Find all and one

find returns a cursor; findOne returns a single document or null.

db.books.find()
db.books.findOne({ title: "Practical MongoDB" })

Filter with operators

Comparison operators live inside the query document.

db.books.find({ year: { $gte: 2024 } })
db.books.find({ tags: "nosql" })

Combine conditions

Multiple fields imply AND. Use $or for alternatives.

db.books.find({
  $or: [
    { year: 2025 },
    { tags: "database" }
  ]
})

Pretty print results

In mongosh, chaining .pretty() is often unnecessary; the shell formats documents for you. Limit results while exploring.

db.books.find().limit(5)

Comments

One comment per signed-in account. Comments are saved with this page’s URL.