
- 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
Sorting field value (FirstName) for MongoDB?
To sort values, use sort() in MongoDB. Let us first create a collection with documents −
> db.demo365.insertOne({"FirstName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e57d5b6d0ada61456dc936f") } > db.demo365.insertOne({"FirstName":"Adam"}); { "acknowledged" : true, "insertedId" : ObjectId("5e57d5bad0ada61456dc9370") } > db.demo365.insertOne({"FirstName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5e57d5bed0ada61456dc9371") } > db.demo365.insertOne({"FirstName":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e57d5c0d0ada61456dc9372") }
Display all documents from a collection with the help of find() method −
> db.demo365.find();
This will produce the following output −
{ "_id" : ObjectId("5e57d5b6d0ada61456dc936f"), "FirstName" : "Chris" } { "_id" : ObjectId("5e57d5bad0ada61456dc9370"), "FirstName" : "Adam" } { "_id" : ObjectId("5e57d5bed0ada61456dc9371"), "FirstName" : "John" } { "_id" : ObjectId("5e57d5c0d0ada61456dc9372"), "FirstName" : "Bob" }
Following is the query for sorting −
> db.demo365.find().sort({"FirstName":1});
This will produce the following output −
{ "_id" : ObjectId("5e57d5bad0ada61456dc9370"), "FirstName" : "Adam" } { "_id" : ObjectId("5e57d5c0d0ada61456dc9372"), "FirstName" : "Bob" } { "_id" : ObjectId("5e57d5b6d0ada61456dc936f"), "FirstName" : "Chris" } { "_id" : ObjectId("5e57d5bed0ada61456dc9371"), "FirstName" : "John" }
- Related Articles
- MongoDB query to find on field combination of FirstName and LastName?
- Looping MongoDB collection for sorting
- Update MongoDB field using value of another field?
- Check if value exists for a field in a MongoDB document?
- MongoDB query to get distinct FirstName values from documents
- Delete a field and value in MongoDB?
- Replace an array field value with MongoDB?
- MongoDB query for a single field
- MongoDB query to find documents with specific FirstName and LastName
- Sorting varchar field numerically in MySQL?
- Set MongoDB compound index with a fixed value field
- How to exclude array type field value in MongoDB?
- MongoDB aggregation to fetch documents with specific field value?
- MongoDB Query for boolean field as “not true”
- MongoDB Aggregate JSON array field for the matching field of other collection?

Advertisements