Example find database in mongodb with nodejs use mongoose

Find All

To select data from a table in MongoDB, we can also use the find() method.

The find() method returns all occurrences in the selection.

The first parameter of the find() method is a query object. In this example we use an empty query object, which selects all documents in the collection.

Note: No parameters in the find() method gives you the same result as SELECT * in MySQL.

Example: Find all documents in the customers collection

 

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);

    customerObject.find({}, 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 mongooseFindAllData.js

db connect

[ { _id: 5adf11285075b4199c20efb8,

    name: 'name1',

    address: 'Highway12',

   __v: 0 },

  { _id: 5ae2eb89d4731310781c448b,

    name: 'name2',

    address: 'Lowstreet 4',

    __v: 0 },

  { _id: 5ae2eb89d4731310781c448c,

    name: 'name3',

    address: 'Apple st 652',

    __v: 0 },

  { _id: 5ae2eb89d4731310781c448d,

    name: 'name4',

    address: 'Mountain 21',

    __v: 0 },

  { _id: 5ae2eb89d4731310781c448e,

    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 },

  { _id: 5ae2eb89d4731310781c4493,

    name: 'name10',

    address: 'Yellow Garden 2',

    __v: 0 },

  { _id: 5ae2eb89d4731310781c4494,

    name: 'name11',

    address: 'Park Lane 38',

    __v: 0 },

  { _id: 5ae2eb89d4731310781c4495,

    name: 'name12',

    address: 'Central st 954',

    __v: 0 },

  { _id: 5ae2eb89d4731310781c4496,

    name: 'name13',

    address: 'Main Road 989',

    __v: 0 },

  { _id: 5ae2eb89d4731310781c4497,

    name: 'name14',

    address: 'Sideway 1633',

    __v: 0 } ]

 

Find Some

The second parameter of the find() method is an object describing which fields to include in the result.

This parameter is optional, and if omitted, all fields will be included in the result.

Example: Return only the fields "name" of all documents in the customers collection

 

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);

    customerObject.find({},{_id:0,name:1}, 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 mongooseFindSomeData.js

db connect

[ { name: 'name1' },

  { name: 'name2' },

  { name: 'name3' },

  { name: 'name4' },

  { name: 'name5' },

  { name: 'name6' },

  { name: 'name7' },

  { name: 'name8' },

  { name: 'name9' },

  { name: 'name10' },

  { name: 'name11' },

  { name: 'name12' },

  { name: 'name13' },

  { name: 'name14' }]

 

Note: You are not allowed to specify both 0 and 1 values in the same object (except if one of the fields is the _id field). If you specify a field with the value 0, all other fields get the value 1, and vice versa