
- 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 array in MongoDB query and project all fields?
To sort array, use $sort. For projection, use $project in MongoBD aggregate(). Let us create a collection with documents −
> db.demo252.insertOne( ... {"Values" : [ { "v1" : 20, "v2" :30 }, { "v1" : 20, "v2" : 20 }, { "v1" : 10, "v2" : 7 } ] } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e46c2761627c0c63e7dba78") }
Display all documents from a collection with the help of find() method −
> db.demo252.find();
This will produce the following output −
{ "_id" : ObjectId("5e46c2761627c0c63e7dba78"), "Values" : [ { "v1" : 20, "v2" : 30 }, { "v1" : 20, "v2" : 20 }, { "v1" : 10, "v2" : 7 } ] }
Following is the query to sort array and project all fields with aggregate() −
> db.demo252.aggregate([ ... { "$unwind": "$Values"}, ... { "$sort": {"Values.v2":1, "Values.v1": 1}}, ... { "$group": { ... "_id": { ... "_id": "$_id" ... ... }, ... "st": { "$push":"$Values"} ... }}, ... { "$project": { ... "_id": "$_id._id", ... "Values": "$st" ... }} ...]);
This will produce the following output −
{ "_id" : ObjectId("5e46c2761627c0c63e7dba78"), "Values" : [ { "v1" : 10, "v2" : 7 }, { "v1" : 20, "v2" : 20 }, { "v1" : 20, "v2" : 30 } ] }
- Related Articles
- MongoDB query to sort nested array?
- MongoDB query with $all in array
- MongoDB query to return specific fields from an array?
- MongoDB query to display all the fields value, except _id
- MongoDB query to get only specific fields in nested array documents?
- MongoDB query to concatenate values of array with other fields
- How to project specific fields from a document inside an array in Mongodb?
- How to display a specific field in array using $project in MongoDB and ignore other fields?
- How to query a document in MongoDB comparing fields from an array?
- MongoDB query for fields in embedded document?
- Count unique items in array-based fields across all MongoDB documents?
- MongoDB query to sum specific fields
- MongoDB query to update selected fields
- Sort and Group in one MongoDB aggregation query?
- Include all existing fields and add new fields to document in MongoDB?

Advertisements