Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to use ORDERBY in MongoDB if there are possible null values?
When sorting documents containing null values in MongoDB, use the sort() method. MongoDB treats null values as the smallest value, so they appear first in ascending order and last in descending order.
Note − Starting in MongoDB v3.2, the $orderby operator is deprecated in the mongo shell. Use cursor.sort() instead.
Syntax
db.collection.find().sort({ "fieldName": 1 }); // Ascending (nulls first)
db.collection.find().sort({ "fieldName": -1 }); // Descending (nulls last)
db.collection.aggregate([
{ $match: { "fieldName": { $exists: true } } },
{ $sort: { "fieldName": 1 } }
]);
Sample Data
db.demo530.insertMany([
{ "Name": "Chris" },
{ "Name": null },
{ "Name": "David" },
{ "Name": "Adam" },
{ "Name": null },
{ "Name": "Carol" }
]);
Display all documents from the collection ?
db.demo530.find();
{ "_id" : ObjectId("5e8b2990ef4dcbee04fbbbec"), "Name" : "Chris" }
{ "_id" : ObjectId("5e8b2991ef4dcbee04fbbbed"), "Name" : null }
{ "_id" : ObjectId("5e8b2992ef4dcbee04fbbbee"), "Name" : "David" }
{ "_id" : ObjectId("5e8b2995ef4dcbee04fbbbef"), "Name" : "Adam" }
{ "_id" : ObjectId("5e8b2999ef4dcbee04fbbbf0"), "Name" : null }
{ "_id" : ObjectId("5e8b299eef4dcbee04fbbbf1"), "Name" : "Carol" }
Method 1: Basic Sorting (Includes Null Values)
Sort all documents including null values using sort() ?
db.demo530.find().sort({ "Name": 1 });
{ "_id" : ObjectId("5e8b2991ef4dcbee04fbbbed"), "Name" : null }
{ "_id" : ObjectId("5e8b2999ef4dcbee04fbbbf0"), "Name" : null }
{ "_id" : ObjectId("5e8b2995ef4dcbee04fbbbef"), "Name" : "Adam" }
{ "_id" : ObjectId("5e8b299eef4dcbee04fbbbf1"), "Name" : "Carol" }
{ "_id" : ObjectId("5e8b2990ef4dcbee04fbbbec"), "Name" : "Chris" }
{ "_id" : ObjectId("5e8b2992ef4dcbee04fbbbee"), "Name" : "David" }
Method 2: Using Aggregation Pipeline
Use aggregation to sort documents with null values included ?
db.demo530.aggregate([
{ $match: { "Name": { $exists: true } } },
{ $sort: { "Name": 1 } }
]);
{ "_id" : ObjectId("5e8b2991ef4dcbee04fbbbed"), "Name" : null }
{ "_id" : ObjectId("5e8b2999ef4dcbee04fbbbf0"), "Name" : null }
{ "_id" : ObjectId("5e8b2995ef4dcbee04fbbbef"), "Name" : "Adam" }
{ "_id" : ObjectId("5e8b299eef4dcbee04fbbbf1"), "Name" : "Carol" }
{ "_id" : ObjectId("5e8b2990ef4dcbee04fbbbec"), "Name" : "Chris" }
{ "_id" : ObjectId("5e8b2992ef4dcbee04fbbbee"), "Name" : "David" }
Key Points
- Null values are treated as the smallest value in MongoDB sorting
- In ascending order (1): null values appear first
- In descending order (-1): null values appear last
- Use
{ $exists: true }to include documents with null values
Conclusion
MongoDB's sort() method handles null values by treating them as the smallest value. Use aggregation with $exists: true to explicitly include null values in your sorted results.
Advertisements
