
- 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
Find current and previous documents in MongoDB
To get the current and previous documents, use sort(). Since we need only 2 documents, therefore, use LIMIT(2).
Let us create a collection with documents −
> db.demo360.insertOne({id:101,"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e56a10c54a481fef8ec7a15") } > db.demo360.insertOne({id:102,"Name":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e56a11254a481fef8ec7a16") } > db.demo360.insertOne({id:103,"Name":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e56a11a54a481fef8ec7a17") } > db.demo360.insertOne({id:104,"Name":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5e56a11f54a481fef8ec7a18") }
Display all documents from a collection with the help of find() method −
> db.demo360.find();
This will produce the following output −
{ "_id" : ObjectId("5e56a10c54a481fef8ec7a15"), "id" : 101, "Name" : "Chris" } { "_id" : ObjectId("5e56a11254a481fef8ec7a16"), "id" : 102, "Name" : "David" } { "_id" : ObjectId("5e56a11a54a481fef8ec7a17"), "id" : 103, "Name" : "Bob" } { "_id" : ObjectId("5e56a11f54a481fef8ec7a18"), "id" : 104, "Name" : "Mike" }
Following is the query to find current and previous documents in MongoDB −
> db.demo360.find({ id: { $lte: 102 } }, {id:1, Name: 1 }).sort({_id: -1}).limit(2);
This will produce the following output −
{ "_id" : ObjectId("5e56a11254a481fef8ec7a16"), "id" : 102, "Name" : "David" } { "_id" : ObjectId("5e56a10c54a481fef8ec7a15"), "id" : 101, "Name" : "Chris" }
- Related Articles
- Find in MongoDB documents with filled nested array and reshape the documents result
- Find documents that contains specific field in MongoDB?
- Find MongoDB documents that contains specific field?
- MongoDB query to find documents with specific FirstName and LastName
- Check for existing documents/embedded documents in MongoDB
- Find value above a specific value in MongoDB documents?
- Upsert many documents in MongoDB
- How to fire find query on sub-documents in MongoDB?
- MongoDB - Query embedded documents?
- How to find MongoDB documents with a specific string?
- Find a value in lowercase from a MongoDB collection with documents
- How to find latest entries in array over all MongoDB documents?
- Find MongoDB documents where all objects in array have specific value?
- Aggregation in MongoDB for nested documents?
- Search for multiple documents in MongoDB?

Advertisements