
- 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
Sort MongoDB field which contains both integer and decimal values?
To sort, use sort() in MongoDB. Let us create a collection with documents −
> db.demo755.insertOne({"Value":10}); { "acknowledged" : true, "insertedId" : ObjectId("5eae9e72a930c785c834e572") } > db.demo755.insertOne({"Value":10.5}); { "acknowledged" : true, "insertedId" : ObjectId("5eae9e75a930c785c834e573") } > db.demo755.insertOne({"Value":9.5}); { "acknowledged" : true, "insertedId" : ObjectId("5eae9e79a930c785c834e574") } > db.demo755.insertOne({"Value":12.5}); { "acknowledged" : true, "insertedId" : ObjectId("5eae9e7fa930c785c834e575") } > db.demo755.insertOne({"Value":11.5}); { "acknowledged" : true, "insertedId" : ObjectId("5eae9e87a930c785c834e576") } > db.demo755.insertOne({"Value":6}); { "acknowledged" : true, "insertedId" : ObjectId("5eae9e97a930c785c834e577") }
Display all documents from a collection with the help of find() method −
> db.demo755.find();
This will produce the following output −
{ "_id" : ObjectId("5eae9e72a930c785c834e572"), "Value" : 10 } { "_id" : ObjectId("5eae9e75a930c785c834e573"), "Value" : 10.5 } { "_id" : ObjectId("5eae9e79a930c785c834e574"), "Value" : 9.5 } { "_id" : ObjectId("5eae9e7fa930c785c834e575"), "Value" : 12.5 } { "_id" : ObjectId("5eae9e87a930c785c834e576"), "Value" : 11.5 } { "_id" : ObjectId("5eae9e97a930c785c834e577"), "Value" : 6 }
Following is the query to sort MongoDB field which contains both integer and decimal −
> db.demo755.find().sort({Value:1});
This will produce the following output −
{ "_id" : ObjectId("5eae9e97a930c785c834e577"), "Value" : 6 } { "_id" : ObjectId("5eae9e79a930c785c834e574"), "Value" : 9.5 } { "_id" : ObjectId("5eae9e72a930c785c834e572"), "Value" : 10 } { "_id" : ObjectId("5eae9e75a930c785c834e573"), "Value" : 10.5 } { "_id" : ObjectId("5eae9e87a930c785c834e576"), "Value" : 11.5 } { "_id" : ObjectId("5eae9e7fa930c785c834e575"), "Value" : 12.5 }
- Related Articles
- Find MongoDB documents that contains specific field?
- Find documents that contains specific field in MongoDB?
- Fetch specific field values in MongoDB
- Check if a field contains a string in MongoDB?
- How to index and sort with pagination using custom field in MongoDB?
- How to compare field values in MongoDB?
- Find values group by another field in MongoDB?
- How to check if an array contains integer values in JavaScript ?
- How can I sort documents in MongoDB 4 and display only a single field?
- MongoDB query to select one field if the other is null and the first field if both are not null?
- Get the duplicate values of a field in MongoDB?
- List all values of a certain field in MongoDB?
- Querying object's field array values in MongoDB?
- Limit number of values in a field with MongoDB?
- Display the last two values from field with MongoDB

Advertisements