
- 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
How do I sort natural in MongoDB?
Use $natural to sort natural in MongoDB. Let us create a collection with documents −
> db.demo684.insertOne({Value:10}); { "acknowledged" : true, "insertedId" : ObjectId("5ea530cea7e81adc6a0b3957") } > db.demo684.insertOne({Value:50}); { "acknowledged" : true, "insertedId" : ObjectId("5ea530d1a7e81adc6a0b3958") } > db.demo684.insertOne({Value:60}); { "acknowledged" : true, "insertedId" : ObjectId("5ea530d4a7e81adc6a0b3959") } > db.demo684.insertOne({Value:40}); { "acknowledged" : true, "insertedId" : ObjectId("5ea530d8a7e81adc6a0b395a") }
Display all documents from a collection with the help of find() method −
> db.demo684.find();
This will produce the following output −
{ "_id" : ObjectId("5ea530cea7e81adc6a0b3957"), "Value" : 10 } { "_id" : ObjectId("5ea530d1a7e81adc6a0b3958"), "Value" : 50 } { "_id" : ObjectId("5ea530d4a7e81adc6a0b3959"), "Value" : 60 } { "_id" : ObjectId("5ea530d8a7e81adc6a0b395a"), "Value" : 40 }
Following is the query to sort on the basis of _id −
> db.demo684.find(). sort({'_id': 1});
This will produce the following output −
{ "_id" : ObjectId("5ea530cea7e81adc6a0b3957"), "Value" : 10 } { "_id" : ObjectId("5ea530d1a7e81adc6a0b3958"), "Value" : 50 } { "_id" : ObjectId("5ea530d4a7e81adc6a0b3959"), "Value" : 60 } { "_id" : ObjectId("5ea530d8a7e81adc6a0b395a"), "Value" : 40 }
You can also use $natural −
> db.demo684.find().sort({$natural: 1});
This will produce the following output −
{ "_id" : ObjectId("5ea530cea7e81adc6a0b3957"), "Value" : 10 } { "_id" : ObjectId("5ea530d1a7e81adc6a0b3958"), "Value" : 50 } { "_id" : ObjectId("5ea530d4a7e81adc6a0b3959"), "Value" : 60 } { "_id" : ObjectId("5ea530d8a7e81adc6a0b395a"), "Value" : 40 }
- Related Articles
- How to do alphanumeric sort in MongoDB?
- How do I access subdocuments in MongoDB queries?
- How do I query a MongoDB collection?
- How do I sort a two-dimensional array in C#
- Natural Sort in JavaScript
- How to sort in MongoDB?
- How do I remove elements not matching conditions in MongoDB?
- How do I make case-insensitive queries on MongoDB?
- How do I display the indexes of a collection in MongoDB?
- How do I $set and $push in single update with MongoDB?
- How do I index “or” in MongoDB for indexing multiple fields?
- How do I push elements to an existing array in MongoDB?
- How do I add a field to existing record in MongoDB?
- How do I delete array value from a document in MongoDB?
- How can I sort documents in MongoDB 4 and display only a single field?

Advertisements