Querying data in MongoDB is accomplished using the find() and findOne() methods. Unlike SQL's SELECT * FROM table WHERE condition, MongoDB queries are expressed as JSON/BSON filter documents using specialized query operators.
findOne() vs find()| Operator | Meaning | Example |
|---|---|---|
$eq | Equals | { status: { $eq: 'ACTIVE' } } (or shorthand { status: 'ACTIVE' }) |
$ne | Not equals | { role: { $ne: 'banned' } } |
$gt | Greater than | { price: { $gt: 500 } } |
$gte | Greater than or equal to | { age: { $gte: 18 } } |
$lt | Less than | { inventory: { $lt: 10 } } |
$lte | Less than or equal to | { rating: { $lte: 4.5 } } |
$in | Matches any value in array | { category: { $in: ['Web', 'AI', 'Cloud'] } } |
$nin | Matches none of the values | { status: { $nin: ['ARCHIVED', 'DELETED'] } } |
Use dot notation enclosed in quotes to query fields inside embedded subdocuments:
collection.find() return in the MongoDB Node.js driver?city located inside an embedded subdocument address in MongoDB?rating is strictly greater than 4.5?collection.findOne({ email: 'nonexistent@test.com' }) return?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.