Mongoskin Tutorial

Mongoskin is probably the most developer-friendly MongoDB driver for Node.js at the moment - it is asynchronous and has a clean looking structure. But its docs are not that developer-friendly (the effort is much appreciated though) - no good examples or explanation; probably because English isn't the author's first language.

After realizing that the Mongoskin docs may take a long time to improve, I decided to personally address the issue by writing a Mongoskin tutorial for all those who are looking for an easy way to integrate MongoDB with Node.js. This is going to be a beginner's tutorial which will guide you through the basics of Mongoskin and should be enough for all basic Node.js-MongoDB interaction.

To give credit where it is due, Mongoskin is built on top of MongoDB Native. The underlying magic behind Mongoskin is MongoDB Native, all Mongoskin does is make the magic wieldier.

If you are not that familiar with MongoDB, I would highly recommend that you first go through this MongoDB tutorial so that you know the basics of MongoDB, and what to expect from Mongoskin.

In the above mongo commands, we have made four entries to the collection named bands in the database named rockband.

Now it is time to include the Mongoskin module in your script, you do it this way:

var db = require('mongoskin').db('localhost:27017/rockband');

That creates an instance of a connection to MongoDB, by default it is connected to the database rockband. I have named the instance db, so as to keep it consistent with the MongoDB's commandline db object. Henceforth all instances of db is a reference to the instance we created here.

Now what if you need to authenticate to connect to the database? Authentication using Mongoskin is easy, just include the auth creds in the instantiation code like this:

The rest of the tutorial will show mongo commands and their equivalents in Mongoskin. If you don't know the basics of MongoDB, you will probably find it hard to follow the examples, so make sure you have gone through the MongoDB tutorial I wrote.

Notice how the Mongoskin code is quite similar to that of the mongo shell command. Instead of bands, we have collection('bands'), and we pass a callback function; rest of the it is the same. Most of the Mongoskin functions maintain the similarity with mongo commandline as closely as possible, that's the beauty of Mongoskin, if you are familiar with the commandline version, you can almost guess the Mongoskin version of a command. If there are parameters involved, just pass them as you would in the mongo commandline (works for most of the functions).

Now let's how we do some conditional selections in Mongoskin.

To get the band with the name "Road Crew", we'd do this in mongo shell:

Tip: you might get confused, was it remove() or delete()? Remember that delete is a JavaScript reserved keyword, it cannot be used as a function or variables name, hence remove() is the one!

In case you are using the default _id generated by MongoDB, and want to select a document by its _id; doing this way won't get you the result:

{_id: '4f5bc53f3d0b5eb764000002'}

This is the right way:

{_id: db.ObjectID.createFromHexString('4f5bc53f3d0b5eb764000002')}

That's it for today. There is more to Mongoskin than the examples I have shown in this tutorial. I will be covering the more technical aspects of Mongoskin in dedicated tutorials rather than including everything here and making it a boring read. Did I miss anything? Want to know anything? Ping me in the comments, if it is within to the scope of this tutorial I will update it from your feedback, else I'll cover them in a more technical Mongoskin tutorial.

but it will only response the first query result, and return the following message

events.js:72
throw er; // Unhandled 'error' event
^
Error: Can't render headers after they are sent to the client.
at ServerResponse.OutgoingMessage._renderHeaders (http.js:749:11)
at ServerResponse.res._renderHeaders (/home/jerrykuo/nodejs/portal/node_modules/express/node_modules/connect/lib/patch.js:69:27)
at ServerResponse.writeHead (http.js:1131:20)
at ServerResponse.res.writeHead (/home/jerrykuo/nodejs/portal/node_modules/express/node_modules/connect/lib/patch.js:75:22)
at response (/home/jerrykuo/nodejs/portal/service/firmwareinfo.js:13:7)
at /home/jerrykuo/nodejs/portal/service/firmwareinfo.js:51:6
at /home/jerrykuo/nodejs/portal/node_modules/mongodb/lib/mongodb/cursor.js:158:16
at commandHandler (/home/jerrykuo/nodejs/portal/node_modules/mongodb/lib/mongodb/cursor.js:651:16)
at /home/jerrykuo/nodejs/portal/node_modules/mongodb/lib/mongodb/db.js:1670:9
at Server.Base._callHandler (/home/jerrykuo/nodejs/portal/node_modules/mongodb/lib/mongodb/connection/base.js:382:41)

I know it's due to I return the result at the first query with response(), but if I put the response() behind the for loop , It will return nothing.
Could you give me some advice about this!

Very usefull tutorial.An humble request.I’m begginer to node.js and mongoskin.And i need to know how can i use mongoskin to upload image files.(i.e what are the changes i need to made in the above insert query).

HI,
I am a quit confused with the MongoSkin API used on this blog. On Mongoose client we have to define schema and it gives us flexibility to reference the ObjectId to the schema at the time of creating collection on MongoDB. So i am confused is it possible to reference/dereference using MongoSkin . For example consider the user collection. Suppose we want to add the new entity for user that controls the user behavior say for simplicity we introduce a group collection so what if i want to refer to the group ObjectId to the user at the time of user creation(I am aware of embedded documents but want to explore the possibility of referencing). I have been going through the documentation on MongoSkin but was not able to figure out how. If possible can you please help me on this case.