Getting my Mongo on

This is the sixth post in a small series on Node.js. In this post, I will take a look at how to use my MongoDB from a Node app.

First, let’s install a driver for MongoDB: node-mongodb-native:

NPM really make it easy to get going!

Now, in order to connect to MongoDB, do this:

This should be done only once when the application starts up.

Then, to access a collection, do this:

Inside the function above, coll gives you access to do all the usual stuff to a MongoDB collection. To name a few, there’s insert, find, findOne, ensureIndex, count, etc.

As the node-mongodb-native documentation is pretty sparse, I’ve found it useful to open up a Node.js shell and connect with the driver, allowing me to inspect the various objects’ properties and methods, e.g. by inspecting db.__proto__ like so:

and coll.__proto__ like so:

Of course this way of barfing functions all over the place is not pretty, but it makes it possible to get a glimpse of what kinds of operations that can be performed on the various objects.

11 thoughts on “Getting my Mongo on

  1. Hey, great article. It’s very useful to start playing with mongo through node.

    I have just one problem. I have one doc in a mongohq collection, but when I call:

    db.collection(“collection”, function(err, coll) {

    coll.find({}, {}, function(err, res) {

    console.log( res.items);

    });

    });

    res.items is an empty list. Is there something like “execute” that I need to call in order to get the items?

    Thank you!

    —Alberteddu

  2. When you call find, the second parameter in the supplied callback (what you call res) is a cursor!

    So in order to iterate through the results, you should do something like this:

  3. Oh, that’s what I saw before.

    “unauthorized db:*** lock type:-1 client:bla bla”

    Do you know the meaning of this message? :-O

  4. I don’t know the meaning of that exact message, except that I’d interpret that as some kind of authentication issue.

    You’re using MongoHq, right? Try clicking the port number in your list of databases – that will show you how to format a connection string to that database.

    Then make sure that <username> and <password> are set to those configured for that database (and not your MongoHq credentials).

    You also might want to try connecting with the Mongo Shell fromo your machine – MongoHq shows how to do that as well when you click the port number.

    Hope that helps you in diagnosing what’s wrong 🙂

  5. I think your issue can be a race condition between your initialization code and your query code.

    Try executing the db.collection stuff from within the callback given to authenticate.

    I solved the issue in a small Node sample I have at Github in app.js I open my HTTP server from the authentication callback, ensuring that I don’t do anything before the database connection is ready.

Leave a Reply to Alberteddu Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.