Example find database in mongoDB with nodejs

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

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

    if (err) throw err;

    console.log(result);

    db.close();

  });

});

 

Run and result:

 

C:\Nodejs>node mongodbFindAllData.js

[ { _id: 5add95feece97026743f71db,

    name: 'name1',

    address: 'Highway 71' },

  { _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' },

  { _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' } ]

 

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 the fields "name" and "address" of all documents in the customers collection

 

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

  dbo.collection("customers").find({}, { _id: 0, name: 1, address: 1 }).toArray(function(err, result) {

    if (err) throw err;

    console.log(result);

    db.close();

  });

});

 

Run and result:

 

C:\Nodejs>node mongodbFindSomeData.js

[ { name: 'name1',

    address: 'Highway 71' },

  { name: 'name2',

    address: 'Lowstreet 4' },

  { name: 'name3',

    address: 'Apple st 652' },

  { name: 'name4',

    address: 'Mountain 21' },

  { name: 'name5',

    address: 'Valley 345' },

  { name: 'name6',

    address: 'Ocean blvd 2' },

  { name: 'name7',

    address: 'Green Grass 1' },

  { name: 'name8',

    address: 'Sky st 331' },

  { name: 'name9',

    address: 'One way 98' },

  { name: 'name10',

    address: 'Yellow Garden 2' },

  { name: 'name11',

    address: 'Park Lane 38' },

  { name: 'name12',

    address: 'Central st 954' },

  { name: 'name13',

    address: 'Main Road 989' },

  { name: 'name14',

    address: 'Sideway 1633' } ]

 

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