Working with MongoDB find()

The find() method in MongoDB selects documents in a collection or view and returns a cursor to the selected documents. It is the primary method for querying data from MongoDB collections.

Syntax

db.collection.find(query, projection)

Parameters:

  • query (optional): Specifies selection criteria using query operators
  • projection (optional): Specifies which fields to return

Sample Data

Let us create a collection with sample documents ?

db.demo405.insertMany([
    {"StudentInfo": {"Name": "Chris"}},
    {"StudentInfo": {"Name": "David"}},
    {"StudentInfo": {"Name": "Bob"}},
    {"StudentInfo": {"Name": "John"}}
]);
{
    "acknowledged": true,
    "insertedIds": [
        ObjectId("5e6f9134fac4d418a0178595"),
        ObjectId("5e6f9138fac4d418a0178596"),
        ObjectId("5e6f913cfac4d418a0178597"),
        ObjectId("5e6f9140fac4d418a0178598")
    ]
}

Example: Find All Documents

The find() method with no parameters returns all documents from a collection ?

db.demo405.find();
{ "_id": ObjectId("5e6f9134fac4d418a0178595"), "StudentInfo": { "Name": "Chris" } }
{ "_id": ObjectId("5e6f9138fac4d418a0178596"), "StudentInfo": { "Name": "David" } }
{ "_id": ObjectId("5e6f913cfac4d418a0178597"), "StudentInfo": { "Name": "Bob" } }
{ "_id": ObjectId("5e6f9140fac4d418a0178598"), "StudentInfo": { "Name": "John" } }

Key Points

  • The find() method returns a cursor object, not the actual documents
  • Without parameters, it retrieves all documents with all fields
  • Use query conditions to filter specific documents
  • Use projection to limit returned fields for better performance

Conclusion

The find() method is essential for querying MongoDB collections. It provides flexible document selection with optional query filters and field projections to retrieve exactly the data you need.

Updated on: 2026-03-15T02:52:42+05:30

226 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements