Node.JS & MongoDB – Looping through Collection using core MongoDB library

Technology Discussions by Ajay Singh

As we all know that MongoDB is very good companion to Node.JS because of the linkage with JSON which is common to both the platforms. JSON is an integral part of Node.JS as it is JavaScript and MongoDB stores data in Binary JSON format.

Now the question is how to perform some simple operations like looping through the objects in collection. This article assumes that you have already installed the core MongoDB library using Node Package Manager (NPM).

sudo npm install mongodb

Now the very first step is to declare the variables pointing to the ‘MyDB’ database in MongoDB:


// MongoDB Connection
var Db = require('mongodb').Db,
    Connection = require('mongodb').Connection,
    Server = require('mongodb').Server,
    BSON = require('mongodb').BSONNative;

var host = process.env['MONGO_NODE_DRIVER_HOST'] != null ? process.env['MONGO_NODE_DRIVER_HOST'] : 'localhost';var port = process.env['MONGO_NODE_DRIVER_PORT'] != null ? process.env['MONGO_NODE_DRIVER_PORT']…

View original post 97 more words


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s