Example sort in mongodb with nodejs use mongoose

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 mongoose = require('mongoose');

var db = mongoose.connection;

var Schema = mongoose.Schema;

db.on('error', console.error);

db.once('open', function () {

    console.log("db connect");

    var customers = new Schema({

        name: {type: String, required: true},

        address: {type: String, required: true}

    });

    var customerObject = db.model('customers', customers);

   

    var mysort = {name: 1};

    customerObject.find({}, function (err, result) {

        if (err) {

            console.log("error query");

        } else {

            console.log(result);

        }

    }).sort(mysort);

});

mongoose.connect('mongodb://192.168.1.122:27017/codewr');

 

Run and result:

 

C:\Nodejs>node mongooseSortAscData.js

db connect

[ { _id: 5adf11285075b4199c20efb8,

    name: 'name1',

    address: 'Highway12',

    __v: 0 },

  { _id: 5adf13268d58740ac846a6f1,

    name: 'name10',

    address: 'Yellow Garden 2',

    __v: 0 },

  { _id: 5adf13268d58740ac846a6f2,

    name: 'name11',

    address: 'Park Lane 38',

    __v: 0 },

  { _id: 5adf13268d58740ac846a6f3,

    name: 'name12',

    address: 'Central st 954',

    __v: 0 },

  { _id: 5adf13268d58740ac846a6f4,

    name: 'name13',

    address: 'Main Road 989',

    __v: 0 },

 

  { _id: 5adf13268d58740ac846a6f5,

    name: 'name14',

    address: 'Sideway 1633',

    __v: 0 },

  { _id: 5adf13268d58740ac846a6e9,

    name: 'name2',

    address: 'Lowstreet 4',

    __v: 0 },

  { _id: 5adf13268d58740ac846a6ea,

    name: 'name3',

    address: 'Apple st 652',

    __v: 0 },

  { _id: 5adf13268d58740ac846a6eb,

    name: 'name4',

    address: 'Mountain 21',

    __v: 0 },

  { _id: 5adf13268d58740ac846a6ec,

    name: 'name5',

    address: 'Valley 345',

    __v: 0 },

  { _id: 5ae2eb89d4731310781c448f,

    name: 'name6',

    address: 'Ocean blvd 2',

    __v: 0 },

  { _id: 5ae2eb89d4731310781c4490,

    name: 'name7',

    address: 'Green Grass 1',

    __v: 0 },

  { _id: 5ae2eb89d4731310781c4491,

    name: 'name8',

    address: 'Sky st 331',

    __v: 0 },

  { _id: 5ae2eb89d4731310781c4492,

    name: 'name9',

    address: 'One way 98',

    __v: 0 } ]

 

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 and address start “S”

 

var mongoose = require('mongoose');

var db = mongoose.connection;

var Schema = mongoose.Schema;

db.on('error', console.error);

db.once('open', function () {

    console.log("db connect");

    var customers = new Schema({

        name: {type: String, required: true},

        address: {type: String, required: true}

    });

    var customerObject = db.model('customers', customers);

    var query = {address: /^S/};

    var mysort = {name: -1};

    customerObject.find(query, function (err, result) {

        if (err) {

            console.log("error query");

        } else {

            console.log(result);

        }

    }).sort(mysort);

});

mongoose.connect('mongodb://192.168.1.122:27017/codewr');

 

Run and result:

 

C:\Nodejs>node mongooseSortDescData.js

db connect

[ { _id: 5adf13268d58740ac846a6ef,

    name: 'name8',

    address: 'Sky st 331',

    __v: 0 },

  { _id: 5ae2eb89d4731310781c4497,

    name: 'name14',

    address: 'Sideway 1633',

    __v: 0 } ]