How do you find a document in the DB?
To find a document in MongoDB, use findOne() and find().
Finding one document:
js
db.users.findOne({ name: "Alex" })Returns the first document found that matches the condition.
Finding several documents:
js
db.users.find({ age: 25 })Returns a cursor, a set of matching documents (you can iterate over it or print it).
How this works:
- the parentheses take a filter, search conditions as an object,
{ field: value } findOne(), for a single resultfind(), for multiple results
So: searching in MongoDB is done via findOne() or find() with a filter on the fields you need.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.