Example sort in mongoDB with nodejs

Sort the Result

Use the sort() method to sort the result in ascending or descending order.

The sort() method takes one parameter, an object defining the sorting order.

Example: Sort the result alphabetically by name

 

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

var url = "mongodb://192.168.1.122:27017/";

 

MongoClient.connect(url, function(err, db) {

  if (err) throw err;

  var dbo = db.db("mydb");

  var mysort = { name: 1 };

  dbo.collection("customers").find().sort(mysort).toArray(function(err, result) {

    if (err) throw err;

    console.log(result);

    db.close();

  });

});

 

Run and result:

 

C:\Nodejs>node mongodbSortAscData.js

[ { _id: 5add95feece97026743f71db,

    name: 'name1',

    address: 'Highway 71' },

  { _id: 5add95feece97026743f71e4,

    name: 'name10',

    address: 'Yellow Garden 2' },

  { _id: 5add95feece97026743f71e5,

    name: 'name11',

    address: 'Park Lane 38' },

  { _id: 5add95feece97026743f71e6,

    name: 'name12',

    address: 'Central st 954' },

  { _id: 5add95feece97026743f71e7,

    name: 'name13',

    address: 'Main Road 989' },

  { _id: 5add95feece97026743f71e8,

    name: 'name14',

    address: 'Sideway 1633' },

  { _id: 5add95feece97026743f71dc,

    name: 'name2',

    address: 'Lowstreet 4' },

  { _id: 5add95feece97026743f71dd,

    name: 'name3',

    address: 'Apple st 652' },

  { _id: 5add95feece97026743f71de,

    name: 'name4',

    address: 'Mountain 21' },

  { _id: 5add95feece97026743f71df,

    name: 'name5',

    address: 'Valley 345' },

  { _id: 5add95feece97026743f71e0,

    name: 'name6',

    address: 'Ocean blvd 2' },

  { _id: 5add95feece97026743f71e1,

    name: 'name7',

    address: 'Green Grass 1' },

  { _id: 5add95feece97026743f71e2,

    name: 'name8',

    address: 'Sky st 331' },

  { _id: 5add95feece97026743f71e3,

    name: 'name9',

    address: 'One way 98' } ]

 

Sort Descending

Use the value -1 in the sort object to sort descending.

- { name: 1 } // ascending

- { name: -1 } // descending

Example: Sort the result reverse alphabetically by name

 

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

var url = "mongodb://192.168.1.122:27017/";

MongoClient.connect(url, function(err, db) {

  if (err) throw err;

  var dbo = db.db("mydb");

  var mysort = { name: -1 };

  dbo.collection("customers").find().sort(mysort).toArray(function(err, result) {

    if (err) throw err;

    console.log(result);

    db.close();

  });

});

 

Run and result:

 

C:\Nodejs>node mongodbSortDescData.js

[ { _id: 5add95feece97026743f71e3,

    name: 'name9',

    address: 'One way 98' },

  { _id: 5add95feece97026743f71e2,

    name: 'name8',

    address: 'Sky st 331' },

  { _id: 5add95feece97026743f71e1,

    name: 'name7',

    address: 'Green Grass 1' },

  { _id: 5add95feece97026743f71e0,

    name: 'name6',

    address: 'Ocean blvd 2' },

  { _id: 5add95feece97026743f71df,

    name: 'name5',

    address: 'Valley 345' },

  { _id: 5add95feece97026743f71de,

    name: 'name4',

    address: 'Mountain 21' },

  { _id: 5add95feece97026743f71dd,

    name: 'name3',

    address: 'Apple st 652' },

  { _id: 5add95feece97026743f71dc,

    name: 'name2',

    address: 'Lowstreet 4' },

  { _id: 5add95feece97026743f71e8,

    name: 'name14',

    address: 'Sideway 1633' },

  { _id: 5add95feece97026743f71e7,

    name: 'name13',

    address: 'Main Road 989' },

  { _id: 5add95feece97026743f71e6,

    name: 'name12',

    address: 'Central st 954' },

  { _id: 5add95feece97026743f71e5,

    name: 'name11',

    address: 'Park Lane 38' },

  { _id: 5add95feece97026743f71e4,

    name: 'name10',

    address: 'Yellow Garden 2' },

  { _id: 5add95feece97026743f71db,

    name: 'name1',

    address: 'Highway 71' } ]