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.

Getting my Mongo on

11 thoughts on “Getting my Mongo on

  • 3. April 2011 at 13:29
    Permalink

    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

  • 3. April 2011 at 20:15
    Permalink

    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:

  • 4. April 2011 at 00:19
    Permalink

    Wow, thanks. That is really helpful. Again, thank you a lot! πŸ˜€

  • 4. April 2011 at 00:24
    Permalink

    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. April 2011 at 07:42
    Permalink

    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 πŸ™‚

  • 4. April 2011 at 17:54
    Permalink

    Uhm, this is very strange. Thanks for your suggestion–I can actually connect to mongohq with the shell (and I can find(), each(), etc.), but not with this code.

    http://pastie.org/pastes/1754779/text?key=qmorhogmzh4dr9rucssqog

    Do you see something wrong? Sorry for bothering you, but Google can’t find anything useful (this time). πŸ™

  • 4. April 2011 at 19:03
    Permalink

    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.

  • 4. April 2011 at 21:58
    Permalink

    Wow, you saved me. Thanks a lot, that was the problem! πŸ™‚

  • 5. April 2011 at 07:27
    Permalink

    No problem! Good to hear you got it sorted out πŸ™‚

  • 16. June 2011 at 21:54
    Permalink

    Thanks mookid, your advise about the race condition solved my problem.

  • 20. November 2011 at 16:22
    Permalink

    This is exactly what I was looking for. Thanks!

Comments are closed.