Example query in mongodb with nodejs use mongoose

Filter the Result

When finding documents in a collection, you can filter the result by using a query object.

The first argument of the find() method is a query object, and is used to limit the search.

Example: Find documents with the name is  "name14"

 

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 = {name: "name14"};

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

        if (err) {

            console.log("error query");

        } else {

            console.log(result);

        }

    });

});

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

 

Run and result:

 

C:\Nodejs>node mongooseFindWithQuery.js

db connect

[ { _id: 5adf13268d58740ac846a6f5,

    name: 'name14',

    address: 'Sideway 1633',

    __v: 0 },

  { _id: 5ae2eb89d4731310781c4497,

    name: 'name14',

    address: 'Sideway 1733',

    __v: 0 } ]

 

Filter with first result

When use find() method return many object. You can use  findOne() for return a first object

Example:

 

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 = {name: "name14"};

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

        if (err) {

            console.log("error query");

        } else {

            console.log(result);

        }

    });

});

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

 

Run and result:

 

C:\Nodejs>node mongooseFindOne.js

db connect

{ _id: 5adf13268d58740ac846a6f5,

  name: 'name14',

  address: 'Sideway 1633',

  __v: 0 }

 

Filter With Regular Expressions

You can write regular expressions to find exactly what you are searching for.

Regular expressions can only be used to query strings.

To find only the documents where the "address" field starts with the letter "S", use the regular expression /^S/

Example: Find documents where the address starts with the letter "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/ };

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

        if (err) {

            console.log("error query");

        } else {

            console.log(result);

        }

    });

});

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

 

Run and result:

 

C:\Nodejs>node mongooseQueryString.js

db connect

[ { _id: 5adf13268d58740ac846a6ef,

    name: 'name8',

    address: 'Sky st 331',

    __v: 0 },

  { _id: 5adf13268d58740ac846a6f5,

    name: 'name14',

    address: 'Sideway 1633',

    __v: 0 },

  { _id: 5ae2eb89d4731310781c4491,

    name: 'name8',

    address: 'Sky st 331',

    __v: 0 },

  { _id: 5ae2eb89d4731310781c4497,

    name: 'name14',

    address: 'Sideway 1633',

    __v: 0 } ]