
- 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
MongoDB query to sort by words
To sort by words, use $addField along with $cond. Let us create a collection with documents −
> db.demo62.insertOne({"Subject":"MySQL"}); { "acknowledged" : true, "insertedId" : ObjectId("5e287084cfb11e5c34d8992f") } > db.demo62.insertOne({"Subject":"MongoDB"}); { "acknowledged" : true, "insertedId" : ObjectId("5e287085cfb11e5c34d89930") } > db.demo62.insertOne({"Subject":"Java"}); { "acknowledged" : true, "insertedId" : ObjectId("5e287086cfb11e5c34d89931") }
Display all documents from a collection with the help of find() method −
> db.demo62.find();
This will produce the following output −
{ "_id" : ObjectId("5e287084cfb11e5c34d8992f"), "Subject" : "MySQL" } { "_id" : ObjectId("5e287085cfb11e5c34d89930"), "Subject" : "MongoDB" } { "_id" : ObjectId("5e287086cfb11e5c34d89931"), "Subject" : "Java" }
Following is the query to sort by words −
> db.demo62.aggregate([ ... { ... $addFields: { ... sortByWords: { ... $cond: [ ... { $eq: ["$Subject", "MongoDB"] }, ... 0, ... { $cond: [{ $eq: ["$Subject", "MySQL"] }, 1, 2] } ... ] ... } ... } ... }, ... ... { ... $sort: { ... sortByWords: 1 ... } ... } ... ]);
This will produce the following output −
{ "_id" : ObjectId("5e287085cfb11e5c34d89930"), "Subject" : "MongoDB", "sortByWords" : 0 } { "_id" : ObjectId("5e287084cfb11e5c34d8992f"), "Subject" : "MySQL", "sortByWords" : 1 } { "_id" : ObjectId("5e287086cfb11e5c34d89931"), "Subject" : "Java", "sortByWords" : 2 }
- Related Articles
- MongoDB query to sort subdocuments
- MongoDB aggregate query to sort
- MongoDB query to sort nested array?
- MongoDB query to sort by the sum of specified object inside inner array?
- MongoDB to sort by subdocument match?
- How to sort, select and query subdocument in MongoDB?
- MongoDB query to group by _id
- Sort and Group in one MongoDB aggregation query?
- Sort by subdocument in MongoDB
- MongoDB query by sub-field?
- Sort array in MongoDB query and project all fields?
- Sort MongoDB Collection by Array value?
- MySQL query to sort by both timestamp and enum?
- MySQL query to sort by certain last string character?
- MongoDB query to filter by several array elements?

Advertisements