MongoDB Query Documents using db.collection.find()
MongoDB query documents operations are commonly done with the db.collection.find() method in mongosh. The method reads documents from a MongoDB Collection and returns a cursor for the matching MongoDB Documents.
You can use find() with an empty query document to return all documents, with a filter document to match selected records, and with a projection document to return only specific fields.
MongoDB find() method syntax for querying documents
The basic syntax of the MongoDB find() method is shown below.
db.collection.find(query, projection)
Here, query is the filter condition. The optional projection document controls which fields are included or excluded in the result.
| Parameter | Purpose in db.collection.find() | Example |
|---|---|---|
query | Specifies which documents should match. | { place: "New York" } |
projection | Specifies which fields should be returned. | { name: 1, _id: 0 } |
Examples covered in this tutorial:
- Query all documents in a MongoDB collection
- Query MongoDB documents based on a criteria
- Query documents using MongoDB comparison operators
- Return selected fields using MongoDB find projection
- Difference between find() and findOne()
Example 1 – Query all documents in a MongoDB collection
To query all documents in a collection, use the find() method with an empty query document.
Following is the syntax of find command to query all documents
db.<collection>.find( {} )
<collection> is the name of the MongoDB Collection.
Following command queries all documents in people collection present intutorialkart database
> use tutorialkart
switched to db tutorialkart
> db.people.find({})
{ "_id" : ObjectId("59eeba4e5f82df4555f2bfb2"), "name" : "Midhuna", "age" : 23, "place" : "Amaravati" }
{ "_id" : ObjectId("59eeba4e5f82df4555f2bfb3"), "name" : "Akhil", "age" : 24, "place" : "New York", "bonus" : 250 }
{ "_id" : ObjectId("59eeba4e5f82df4555f2bfb4"), "name" : "Honey", "age" : 27, "profession" : "Docter" }
{ "_id" : ObjectId("59eeba4e5f82df4555f2bfb5"), "name" : "Manju", "age" : 28, "place" : "Vizag", "profession" : "Driver" }
{ "_id" : ObjectId("59eeba4e5f82df4555f2bfb6"), "name" : "Bharat", "age" : 24, "place" : "New York", "profession" : "Programmer", "bonus" : 250 }
{ "_id" : ObjectId("59eeba4e5f82df4555f2bfb7"), "name" : "Arya", "age" : 25, "profession" : "Teacher" }
In mongosh, find() returns a cursor. When the cursor is printed in the shell, you see the documents returned by the query. In an application program, you normally iterate the cursor or convert it to an array, depending on the driver and use case.
Example 2 – Query MongoDB documents based on a criteria
Following example demonstrates to query those documents of a Collection that respect a criteria.
Select the database in which the collection is, using USE command.
> use tutorialkart
switched to db tutorialkart
Prepare a criteria to select documents
> criteria={place:"New York"}
{ "place" : "New York" }
Use the criteria with find() method to select those documents that obey the criteria.
> criteria={name:"Manju"}
{ "name" : "Manju" }
Run find(criteria) method
> db.people.find(criteria)
{ "_id" : ObjectId("59eeba4e5f82df4555f2bfb3"), "name" : "Akhil", "age" : 24, "place" : "New York", "bonus" : 250 }
{ "_id" : ObjectId("59eeba4e5f82df4555f2bfb6"), "name" : "Bharat", "age" : 24, "place" : "New York", "profession" : "Programmer", "bonus" : 250 }
The result shown above corresponds to the criteria { place: "New York" }. A filter document works like a condition: MongoDB returns only the documents where the field values satisfy that condition.
A clearer way to write the same query is to pass the filter document directly to find().
db.people.find({ place: "New York" })
{ "_id" : ObjectId("59eeba4e5f82df4555f2bfb3"), "name" : "Akhil", "age" : 24, "place" : "New York", "bonus" : 250 }
{ "_id" : ObjectId("59eeba4e5f82df4555f2bfb6"), "name" : "Bharat", "age" : 24, "place" : "New York", "profession" : "Programmer", "bonus" : 250 }
Query MongoDB documents using comparison operators
MongoDB query documents can also use query operators. Operators are written with a dollar sign, such as $gt, $gte, $lt, $lte, $ne, and $in.
The following query returns people whose age is greater than or equal to 25.
db.people.find({ age: { $gte: 25 } })
{ "_id" : ObjectId("59eeba4e5f82df4555f2bfb4"), "name" : "Honey", "age" : 27, "profession" : "Docter" }
{ "_id" : ObjectId("59eeba4e5f82df4555f2bfb5"), "name" : "Manju", "age" : 28, "place" : "Vizag", "profession" : "Driver" }
{ "_id" : ObjectId("59eeba4e5f82df4555f2bfb7"), "name" : "Arya", "age" : 25, "profession" : "Teacher" }
The following query returns people whose place is either New York or Vizag.
db.people.find({ place: { $in: ["New York", "Vizag"] } })
Query MongoDB documents with multiple conditions
When you include multiple field conditions in the same query document, MongoDB applies an AND match. The document must satisfy all the specified conditions.
db.people.find({ place: "New York", age: 24 })
The query above returns only documents where place is New York and age is 24.
To match any one of multiple conditions, use $or.
db.people.find({
$or: [
{ place: "New York" },
{ profession: "Teacher" }
]
})
This query returns documents where the person is from New York or has the profession Teacher.
Return selected fields using MongoDB find projection
The second argument of find() is projection. Projection is useful when you do not need the full document. For example, to return only the name and place fields, use the following command.
db.people.find(
{ place: "New York" },
{ name: 1, place: 1 }
)
By default, MongoDB includes the _id field in the result. To hide _id, set it to 0 in the projection document.
db.people.find(
{ place: "New York" },
{ _id: 0, name: 1, place: 1 }
)
{ "name" : "Akhil", "place" : "New York" }
{ "name" : "Bharat", "place" : "New York" }
In the same projection document, you normally include fields with 1 or exclude fields with 0. The _id field is the common exception because it can be excluded while other fields are included.
Format MongoDB find() results with pretty()
For easier reading in the shell, append .pretty() to the cursor returned by find().
db.people.find({ place: "New York" }).pretty()
This does not change the documents stored in the collection. It only prints the query result in a more readable format.
Difference between MongoDB find() and findOne()
find() returns a cursor for all documents that match the query. findOne() returns only the first matching document, or null if there is no match.
| Method | Returns | When to use |
|---|---|---|
db.people.find({ age: 24 }) | A cursor with all matching documents | Use when many documents may match. |
db.people.findOne({ age: 24 }) | One document or null | Use when only one matching document is needed. |
Difference between MongoDB find() and aggregate()
find() is used for direct document retrieval with filters, projections, sorting, and limits. aggregate() is used when the result needs pipeline stages such as grouping, reshaping, joining with $lookup, calculating fields, or returning summarized results.
For example, use find() to get people from New York. Use aggregate() when you want to count people by place, group records, or transform the output structure.
Common mistakes while querying MongoDB documents with find()
- Do not write field names as variables unless they are actually JavaScript variables. In most shell examples, use field names directly inside the query document.
- Use quotes around string values, such as
"New York". - Remember that
find({})returns all documents in the collection. - Remember that
_idis included by default unless projection excludes it. - Use
findOne()only when one matching document is enough.
MongoDB query documents FAQ
How do you query documents in MongoDB using find?
Use db.collection.find(query). For example, db.people.find({ place: "New York" }) returns documents from the people collection where the place field is New York.
Which command is used to find all documents in a MongoDB collection?
Use db.collection.find({}) to return all documents in a collection. For example, db.people.find({}) returns every document in the people collection.
What is the difference between find() and findOne() in MongoDB?
find() returns a cursor for all matching documents. findOne() returns the first matching document only, or null when no document matches.
How do you return only one field for all MongoDB documents?
Use projection as the second argument of find(). For example, db.people.find({}, { _id: 0, name: 1 }) returns only the name field and hides _id.
When should aggregate() be used instead of find() in MongoDB?
Use aggregate() when you need pipeline operations such as grouping, calculating fields, joining collections, or reshaping results. Use find() for simpler document retrieval with filters and projections.
Editorial QA checklist for MongoDB find() tutorial
- Confirm every
find()example uses a valid MongoDB query document. - Check that output examples match the filter used in the command.
- Verify projection examples clearly explain why
_idappears unless excluded. - Keep shell output separate from syntax examples so readers can copy commands safely.
- Use
find(),findOne(), andaggregate()only for the use cases each method actually supports.
Conclusion: using db.collection.find() to query MongoDB documents
In this MongoDB Tutorial, we learned how to use db.collection.find() to query all MongoDB documents, filter documents with criteria, use comparison operators, and return selected fields with projection. For simple read queries, find() is the usual method. For a single matching document, use findOne(). For grouped or transformed results, use aggregate().
TutorialKart.com