
- 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
How to do alphanumeric sort in MongoDB?
You need to set numericOrdering: true for alphanumeric sort. Let us first create a collection with documents −
> db.alphanumericSortDemo.insertOne({"StudentId":"STU1010"}); { "acknowledged" : true, "insertedId" : ObjectId("5ccf149adceb9a92e6aa194c") } > db.alphanumericSortDemo.insertOne({"StudentId":"STU1101"}); { "acknowledged" : true, "insertedId" : ObjectId("5ccf14a2dceb9a92e6aa194d") } > db.alphanumericSortDemo.insertOne({"StudentId":"STU1901"}); { "acknowledged" : true, "insertedId" : ObjectId("5ccf14a9dceb9a92e6aa194e") } > db.alphanumericSortDemo.insertOne({"StudentId":"STU908"}); { "acknowledged" : true, "insertedId" : ObjectId("5ccf14aedceb9a92e6aa194f") } > db.alphanumericSortDemo.insertOne({"StudentId":"STU101"}); { "acknowledged" : true, "insertedId" : ObjectId("5ccf14b2dceb9a92e6aa1950") }
Following is the query to display all documents from a collection with the help of find() method −
> db.alphanumericSortDemo.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5ccf149adceb9a92e6aa194c"), "StudentId" : "STU1010" } { "_id" : ObjectId("5ccf14a2dceb9a92e6aa194d"), "StudentId" : "STU1101" } { "_id" : ObjectId("5ccf14a9dceb9a92e6aa194e"), "StudentId" : "STU1901" } { "_id" : ObjectId("5ccf14aedceb9a92e6aa194f"), "StudentId" : "STU908" } { "_id" : ObjectId("5ccf14b2dceb9a92e6aa1950"), "StudentId" : "STU101" }
Case 1 − When you want the result in ascending order.
Here is the query to do alphanumeric sort in MongoDB −
> db.alphanumericSortDemo.find({}).sort({"StudentId" : 1}).collation( { locale: "en_US", numericOrdering: true });
This will produce the following output −
{ "_id" : ObjectId("5ccf14b2dceb9a92e6aa1950"), "StudentId" : "STU101" } { "_id" : ObjectId("5ccf14aedceb9a92e6aa194f"), "StudentId" : "STU908" } { "_id" : ObjectId("5ccf149adceb9a92e6aa194c"), "StudentId" : "STU1010" } { "_id" : ObjectId("5ccf14a2dceb9a92e6aa194d"), "StudentId" : "STU1101" } { "_id" : ObjectId("5ccf14a9dceb9a92e6aa194e"), "StudentId" : "STU1901" }
Case 2 − When you want the result in descending order.
Here is the query to do alphanumeric sort in MongoDB −
> db.alphanumericSortDemo.find({}).sort({"StudentId" : -1}).collation( { locale: "en_US", numericOrdering: true });
This will produce the following output −
{ "_id" : ObjectId("5ccf14a9dceb9a92e6aa194e"), "StudentId" : "STU1901" } { "_id" : ObjectId("5ccf14a2dceb9a92e6aa194d"), "StudentId" : "STU1101" } { "_id" : ObjectId("5ccf149adceb9a92e6aa194c"), "StudentId" : "STU1010" } { "_id" : ObjectId("5ccf14aedceb9a92e6aa194f"), "StudentId" : "STU908" } { "_id" : ObjectId("5ccf14b2dceb9a92e6aa1950"), "StudentId" : "STU101" }
- Related Articles
- How to sort an alphanumeric column in MySQL?
- How to sort mixed numeric/alphanumeric array in JavaScript
- How do I sort natural in MongoDB?
- Sort strings in Alphanumeric sequence
- How to sort an alphanumeric column with different lengths in MySQL?
- How to sort in MongoDB?
- Sort only numbers from alphanumeric string in MySQL?
- How to sort inner array in MongoDB?
- How to use MongoDB Aggregate to sort?
- How to perform ascending order sort in MongoDB?
- How to perform descending order sort in MongoDB?
- How to sort, select and query subdocument in MongoDB?
- How to do conditional update in MongoDB?
- MongoDB query to sort subdocuments
- MongoDB aggregate query to sort

Advertisements