
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
Get the top most document from a MongoDB collection
To get the topmost document, use find() along with limit(). To fetch only a single document, consider using limit(1). Let us create a collection with documents −
> db.demo681.insertOne({_id:101,Name:"Chris"}); { "acknowledged" : true, "insertedId" : 101 } > db.demo681.insertOne({_id:102,Name:"Bob"}); { "acknowledged" : true, "insertedId" : 102 } > db.demo681.insertOne({_id:103,Name:"David"}); { "acknowledged" : true, "insertedId" : 103 } > db.demo681.insertOne({_id:104,Name:"Bob"}); { "acknowledged" : true, "insertedId" : 104 } > db.demo681.insertOne({_id:105,Name:"Sam"}); { "acknowledged" : true, "insertedId" : 105 }
Display all documents from a collection with the help of find() method −
> db.demo681.find();
This will produce the following output −
{ "_id" : 101, "Name" : "Chris" } { "_id" : 102, "Name" : "Bob" } { "_id" : 103, "Name" : "David" } { "_id" : 104, "Name" : "Bob" } { "_id" : 105, "Name" : "Sam" }
Following is the query to get the topmost element −
> db.demo681.find().limit(1);
This will produce the following output −
{ "_id" : 101, "Name" : "Chris" }
- Related Articles
- MongoDB query to update a specific document from a collection
- Retrieving the first document in a MongoDB collection?
- How to get array from a MongoDB collection?
- How to delete document from a collection in MongoDB using deleteOne() method?
- Remove document whose value is matched with $eq from a MongoDB collection?
- How to remove all documents from a collection except a single document in MongoDB?
- How to get unique values from MongoDB collection?
- In MongoDB, what is the most efficient way to get the first and last document?
- Get the maximum mark records from a collection with documents in MongoDB
- Add new field to every document in a MongoDB collection?
- Get array items inside a MongoDB document?
- How to insert a document into a MongoDB collection using Java?
- Get the maximum mark records from a collection with documents in MongoDB query
- Adding new property to each document in a large MongoDB collection?
- MongoDB query to add a document in an already created collection

Advertisements