
- 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
Easiest way to sort an array in MongoDB
The easiest way to sort an array in MongoDB, use $sort. Let us create a collection with documents −
> db.demo242.insertOne( ... ... {"details2": ... [ ... {"ShipingDate":new ISODate("2019-10-11"),"Price":1400}, ... {"ShipingDate":new ISODate("2019-10-01"),"Price":1600 } ... ] ... } ... ...); { "acknowledged" : true, "insertedId" : ObjectId("5e4429229af932883c61ea44") }
Display all documents from a collection with the help of find() method −
> db.demo242.find();
This will produce the following output −
{ "_id" : ObjectId("5e4429229af932883c61ea44"), "details2" : [ { "ShipingDate" : ISODate("2019-10-11T00:00:00Z"), "Price" : 1400 }, { "ShipingDate" : ISODate("2019-10-01T00:00:00Z"), "Price" : 1600 } ] }
Following is the query to sort an array in MongoDB −
> db.demo242.aggregate([ {$unwind: "$details2"}, {$sort: {"details2.ShipingDate":1}}, {$group: {_id:"$_id", details2: {$push:"$details2.ShipingDate"}}} ]);
This will produce the following output −
{ "_id" : ObjectId("5e4429229af932883c61ea44"), "details2" : [ ISODate("2019-10-01T00:00:00Z"), ISODate("2019-10-11T00:00:00Z") ] }
- Related Articles
- The easiest way to concatenate two arrays in PHP?
- The easiest way to insert date records in MySQL?
- How to sort inner array in MongoDB?
- MongoDB query to sort nested array?
- What is the easiest way to store date in MySQL database?
- What is the easiest way to reverse a String in Java?
- Easiest way to get number of rows in a MySQL table?
- Best way to sum array size fields in MongoDB?
- What is the easiest way to convert int to string in C++
- What is the easiest way to lose weight without exercise?
- How to express the number16000 in exponential form in the easiest way?
- What is the easiest way to transfer files from phone to PC?
- Easiest way to copy values of one column to a new table in MySQL?
- What is the easiest way to convert a List to a Set in Java?
- Sort MongoDB Collection by Array value?

Advertisements