Skip to content
  • SiteMap
  • Our Services
  • Frequently Asked Questions (FAQ)
  • Support
  • About Us

UpdateGadh

Update Your Skills.

  • Home
  • Projects
    •  Blockchain projects
    • Python Project
    • Data Science
    •  Ai projects
    • Machine Learning
    • PHP Project
    • React Projects
    • Java Project
    • SpringBoot
    • JSP Projects
    • Java Script Projects
    • Code Snippet
    • Free Projects
  • Tutorials
    • Ai
    • Machine Learning
    • Advance Python
    • Advance SQL
    • DBMS Tutorial
    • Data Analyst
    • Deep Learning Tutorial
    • Data Science
    • Nodejs Tutorial
  • Blog
  • Contact us
  • Toggle search form
Node.js MongoDB Sorting

Node.js MongoDB Sorting – Full Stack Web Project

Posted on November 5, 2025March 15, 2026 By Rishabh saini No Comments on Node.js MongoDB Sorting – Full Stack Web Project

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

    Post Views: 223
    Node.js Tutorial Tags:mongodb sorting, mongodb sorting not working with skip and limit, mongodb sorting tutorial, nodejs mongo db sorting backend, nodejs mongodb, nodejs mongodb advance crud, nodejs mongodb api search pagination, nodejs mongodb api tutorial, nodejs mongodb batch update, nodejs mongodb batch write, nodejs mongodb limit, nodejs mongodb pagination, nodejs mongodb pagination backend, nodejs mongodb search, nodejs mongodb search sort pagination, nodejs mongodb skip, sorting in mongodb

    Post navigation

    Previous Post: Best Pharmacy Management Application – Full Stack MERN Project
    Next Post: Best File Sharing System Using Full Stack MERN Web Application

    More Related Articles

    Node.js process.env Property Node.js process.env Property – Full Stack Web Project Node.js Tutorial
    Node.js vs AngularJS Node.js vs AngularJS – Full Stack Web Project Node.js Tutorial
    Node.js Package ManagerNode.js Package Manager Node.js Package Manager Node.js Tutorial

    Leave a Reply Cancel reply

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

    You may also like

    1. Node.js MongoDB Create Collection
    2. Node.js MongoDB Select Record
    3. Node.js MongoDB Remove – Full Stack Web Project
    4. Install Nodejs on Windows
    5. Node.js Net
    6. Node.js ZLIB

    Most Viewed Posts

    1. Top Large Language Models in 2025
    2. Online Shopping System using PHP, MySQL with Free Source Code
    3. login form in php and mysql , Step-by-Step with Free Source Code
    4. Flipkart Clone using PHP And MYSQL Free Source Code
    5. News Portal Project in PHP and MySql Free Source Code
    6. User Login & Registration System Using PHP and MySQL Free Code
    7. Top 10 Final Year Project Ideas in Python
    8. Online Bike Rental Management System Using PHP and MySQL
    9. E learning Website in php with Free source code
    10. E-Commerce Website Project in Java Servlets (JSP)
    • AI
    • ASP.NET
    • Blockchain
    • ChatCPT
    • code Snippets
    • Collage Projects
    • Data Science Project
    • Data Science Tutorial
    • DBMS Tutorial
    • Deep Learning Tutorial
    • Final Year Projects
    • Free Projects
    • How to
    • html
    • Interview Question
    • Java Notes
    • Java Project
    • Java Script Notes
    • JAVASCRIPT
    • Javascript Project
    • JSP JAVA(J2EE)
    • Machine Learning Project
    • Machine Learning Tutorial
    • MySQL Tutorial
    • Node.js Tutorial
    • PHP Project
    • Portfolio
    • Python
    • Python Interview Question
    • Python Projects
    • PythonFreeProject
    • React Free Project
    • React Projects
    • Spring boot
    • SQL Tutorial
    • TOP 10
    • Uncategorized
    • Online Examination System in PHP with Source Code
    • AI Chatbot for College and Hospital
    • Job Portal Web Application in PHP MySQL
    • Online Tutorial Portal Site in PHP MySQL — Full Project with Source Code
    • Online Job Portal System in JSP Servlet MySQL

    Most Viewed Posts

    • Top Large Language Models in 2025 (8,612)
    • Online Shopping System using PHP, MySQL with Free Source Code (5,209)
    • login form in php and mysql , Step-by-Step with Free Source Code (4,857)

    Copyright © 2026 UpdateGadh.

    Powered by PressBook Green WordPress theme