Node.js Tutorial

Node.js MongoDB Sorting – Full Stack Web Project

Node.js MongoDB Sorting - Full Stack Web Project
Menu

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:

To sort documents in ascending order, use:

{ name: 1 }

To sort documents in descending order, use:

{ name: -1 }

Sort in Ascending Order

Lets sort records in ascending order based on the name field.

Introduction to Applied AI:

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 (AZ) by name.

Sort in Descending Order

Next, lets 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 :

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 (ZA) by name.

Deep Learning Tutorial: 
Machine Learning Tutorial:
Complete Advance AI topics:- 
Complete Python Course with Advance topics:-


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

Source Code Available

Interested in This Project?

Get the complete source code for this project at a very affordable price — perfect for your portfolio, college submission, or learning. Message us on WhatsApp and we'll get back to you instantly!

Full source code included Step-by-step setup guide Instant delivery on WhatsApp Instant reply on WhatsApp
Chat on WhatsApp

We usually reply within a few minutes

Leave a Reply

Your email address will not be published. Required fields are marked *

Chat with us