Get the top most document from a MongoDB collection

To get the topmost document from a MongoDB collection, use the find() method along with limit(1). This retrieves the first document based on the natural insertion order or specified sorting criteria.

Syntax

db.collection.find().limit(1);

Sample Data

db.demo681.insertMany([
    {_id: 101, Name: "Chris"},
    {_id: 102, Name: "Bob"},
    {_id: 103, Name: "David"},
    {_id: 104, Name: "Bob"},
    {_id: 105, Name: "Sam"}
]);
{
  "acknowledged": true,
  "insertedIds": [101, 102, 103, 104, 105]
}

Display all documents from the collection ?

db.demo681.find();
{ "_id": 101, "Name": "Chris" }
{ "_id": 102, "Name": "Bob" }
{ "_id": 103, "Name": "David" }
{ "_id": 104, "Name": "Bob" }
{ "_id": 105, "Name": "Sam" }

Example: Get Top Document

Retrieve the topmost document from the collection ?

db.demo681.find().limit(1);
{ "_id": 101, "Name": "Chris" }

Key Points

  • limit(1) restricts the result to only the first document.
  • Without sorting, it returns the first document in natural order (insertion order).
  • Use sort() before limit(1) to get the top document based on specific criteria.

Conclusion

The find().limit(1) method efficiently retrieves the topmost document from a MongoDB collection. Combine with sort() for retrieving the top document based on custom ordering criteria.

Updated on: 2026-03-15T03:29:12+05:30

258 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements