Nodejs MongoDB
This article describes,
- Sorting the Result
- Ascending/Descending Sort
- Limit the Results
You need to read the Nodejs MongoDB Query before this tutorial.
Sort the Result
Cursor.sort(sort) method can be used to sort the results in ascending or descending order. Cursor.sort(sort) method takes a single parameter of type document to define the sorting order.
The syntax of the method sort is
{ field : value }
Ascending/Descending Sort
We can specify in the sort parameter the field or fields to sort. They can be provided a value of 1 or -1 to represent the order of sorting (ascending order represented by 1 or descending order represented by -1).
For example sorting our collection having the name, dept, and salary fields in ascending order (according to the name alphabetically).
const MongoClient = require('mongodb').MongoClient; const url = "mongodb://localhost:27017/"; MongoClient.connect(url, function(error, database) { if (error) throw error; var dbo = database.db("testdb"); var srt={name:1};//Sorting the collection by field 'name' in ascending dbo.collection("employee").find({}).sort(srt).toArray(function(error, result) { if (error) throw error; console.log('Name \t Dept \t Salary'); for(doc of result) console.log(doc.name+'\t'+doc.dept+'\t'+doc.salary); database.close(); }); }); Output: Name Dept Salary Aarushi Marketing 10500 Akash HR 17500 Alex Sales 12500 Ankit Sales 13600 Deepika HR 13600 Jhony HR 10000 Krish Marketing 17500 Shiva Sales 15000 Vipin Marketing 10500 Viraj Marketing 20000
OR
Sorting the same in descending order.
const MongoClient = require('mongodb').MongoClient; const url = "mongodb://localhost:27017/"; MongoClient.connect(url, function(error, database) { if (error) throw error; var dbo = database.db("testdb"); var srt={name:-1};//Sorting the collection by field 'name' in descending dbo.collection("employee").find({}).sort(srt).toArray(function(error, result) { if (error) throw error; console.log('Name \t Dept \t Salary'); for(doc of result) console.log(doc.name+'\t'+doc.dept+'\t'+doc.salary); database.close(); }); }); Output: Name Dept Salary Viraj Marketing 20000 Vipin Marketing 10500 Shiva Sales 15000 Krish Marketing 17500 Jhony HR 10000 Deepika HR 13600 Ankit Sales 13600 Alex Sales 12500 Akash HR 17500 Aarushi Marketing 10500
OR
Sorting the same collection according to the salary field.
const MongoClient = require('mongodb').MongoClient; const url = "mongodb://localhost:27017/"; MongoClient.connect(url, function(error, database) { if (error) throw error; var dbo = database.db("testdb"); var srt={salary:1};//Sorting the collection by field 'salary' in ascending dbo.collection("employee").find({}).sort(srt).toArray(function(error, result) { if (error) throw error; console.log('Name \t Dept \t Salary'); for(doc of result) console.log(doc.name+'\t'+doc.dept+'\t'+doc.salary); database.close(); }); }); Output: Name Dept Salary Jhony HR 10000 Vipin Marketing 10500 Aarushi Marketing 10500 Alex Sales 12500 Ankit Sales 13600 Deepika HR 13600 Shiva Sales 15000 Akash HR 17500 Krish Marketing 17500 Viraj Marketing 20000
Limiting the result
We can use the limit(<number>) method to limit the number of documents returned by the cursor. Here, the number is the number of results.
This method can improve the performance of the query significantly. The limit() method is similar to the LIMIT statement in SQL.
The syntax of limit() method is,
For example,
const MongoClient = require('mongodb').MongoClient; const url = "mongodb://localhost:27017/"; MongoClient.connect(url, function(error, database) { if (error) throw error; var dbo = database.db("testdb"); var srt={salary:1,name:1};//Sorting the collection dbo.collection("employee").find({}).limit(5).sort(srt).toArray(function(error, result) { if (error) throw error; console.log('Name \t Dept \t Salary'); for(doc of result) console.log(doc.name+'\t'+doc.dept+'\t'+doc.salary); database.close(); }); }); Output: Name Dept Salary Jhony HR 10000 Aarushi Marketing 10500 Vipin Marketing 10500 Alex Sales 12500 Ankit Sales 13600