nodejs
  1. nodejs-mongodb-sorting

Node.js MongoDB Sorting

Sorting is the process of placing records in a specific order based on one or more column values. In Node.js, sorting can be done in MongoDB by using the sort() method. In this tutorial, we'll discuss how to use the sort() method in MongoDB with Node.js.

Syntax

The syntax for using the sort() method in MongoDB is as follows:

db.collection.find().sort({ field: sortOrder })

The sort() method takes an object as its parameter, where the keys represent the fields to sort by, and the values represent the sort order (either 1 for ascending or -1 for descending).

Example

Let's say we have a MongoDB collection called "employees" with the following records:

{ "_id": 1, "name": "John", "age": 30, "salary": 1000 }
{ "_id": 2, "name": "Jane", "age": 25, "salary": 2000 }
{ "_id": 3, "name": "Bob", "age": 35, "salary": 1500 }

To sort the records by the "name" field in ascending order, we can use the following code in Node.js:

const MongoClient = require('mongodb').MongoClient;

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

This will output the following:

[
  { "_id": 3, "name": "Bob", "age": 35, "salary": 1500 },
  { "_id": 2, "name": "Jane", "age": 25, "salary": 2000 },
  { "_id": 1, "name": "John", "age": 30, "salary": 1000 }
]

Explanation

In the example above, we used the sort() method in MongoDB to sort the records in the "employees" collection by the "name" field in ascending order. We passed an empty object as the query parameter to retrieve all the records in the collection, and we passed an object with "name" as the key and 1 as the value to sort the records in ascending order. Finally, we used the toArray() method to retrieve the sorted records and output them to the console.

Use

Sorting is useful when you want to retrieve records from a database in a specific order. You can sort records based on one or more fields, and in either ascending or descending order.

Important Points

  • The sort() method is applied to the find() method of a MongoDB collection to sort the matching records based on the specified fields.
  • The sort() method takes an object as its parameter, where the keys represent the fields to sort by, and the values represent the sort order (either 1 for ascending or -1 for descending).
  • Multiple fields can be specified in the sort() object to sort records by multiple criteria.

Summary

In this tutorial, we discussed how to use the sort() method in MongoDB with Node.js to sort records in a collection based on specific fields and in either ascending or descending order. We covered the syntax, example, output, explanation, use, and important points of sorting in MongoDB with Node.js. With this knowledge, you can now sort and retrieve records from a MongoDB collection based on specific criteria in your Node.js applications.

Published on: