Node.js MongoDB Sorting

Node.js MongoDB Sorting

In MongoDB, the sort() method is used to arrange query results either in ascending or descending order. This method accepts a parameter that defines the sorting order for the fields you want to sort by.

Data Science Tutorial:–Click Here

To sort documents in ascending order, use:

{ name: 1 }

To sort documents in descending order, use:

{ name: -1 }

Sort in Ascending Order

Let’s sort records in ascending order based on the name field.

Introduction to Applied AI:–Click Here

Create a JavaScript file named sortasc.js with the following code:

var http = require('http');  
var MongoClient = require('mongodb').MongoClient;  
var url = "mongodb://localhost:27017/MongoDatabase";  

MongoClient.connect(url, function(err, db) {  
  if (err) throw err;  
  var mysort = { name: 1 };  
  db.collection("employees").find().sort(mysort).toArray(function(err, result) {  
    if (err) throw err;  
    console.log(result);  
    db.close();  
  });  
});  

Now open your command terminal and run the file using:

Download New Real Time Projects :–Click here

node sortasc.js

This will display all employee records sorted alphabetically (A–Z) by name.

Sort in Descending Order

Next, let’s sort the records in descending order according to the name field.

Create a JavaScript file named sortdsc.js and add the following code:

SQL Tutorial :–Click Here

var http = require('http');  
var MongoClient = require('mongodb').MongoClient;  
var url = "mongodb://localhost:27017/MongoDatabase";  

MongoClient.connect(url, function(err, db) {  
  if (err) throw err;  
  var mysort = { name: -1 };  
  db.collection("employees").find().sort(mysort).toArray(function(err, result) {  
    if (err) throw err;  
    console.log(result);  
    db.close();  
  });  
});  

Run the file in the command terminal using:

node sortdsc.js

This will return all employee records sorted in reverse alphabetical order (Z–A) by name.

Deep Learning Tutorial:– Click Here
Machine Learning Tutorial:–Click Here
Complete Advance AI topics:- CLICK HERE
Complete Python Course with Advance topics:-Click Here


mongodb sort descending mongodb sort array of objects mongodb sort by date mongodb sort aggregate mongodb sort by _id mongodb sort by date descending mongodb sort by multiple fields mongodb sort query node js mongodb sorting example node js mongodb sorting python node js mongodb sorting w3schools node js mongodb sorting json nodejs mongo db sorting backend, mongodb sorting, nodejs mongodb, nodejs mongodb skip, nodejs mongodb search sort pagination, sorting in mongodb, nodejs mongodb search, nodejs mongodb pagination, nodejs mongodb limit, mongodb sorting tutorial, nodejs mongodb api tutorial, mongodb sorting not working with skip and limit, nodejs mongodb batch write, nodejs mongodb pagination backend, nodejs mongodb advance crud, nodejs mongodb batch update, nodejs mongodb api search pagination, nodejs mongodb full backend api

Share this content:

Post Comment