Nodejs and MongoDB, A Beginner’s Approach
by ksetyadi on October 20, 2011
This is not a book and I didn’t try to sell a book to you.
The term “A Beginner’s Approach” reflects my self when finding a hard way out to connect Nodejs to MongoDB. There are lots of libraries available to use when connecting Nodejs to MongoDB. If you were trying to make your feet wet, and that’s what I’m doing until today, you probably want to try this approach. I can’t promise anything but at least, you will not get a headache.
First, read about Nodejs. After that, MongoDB. If you’re already familiar with it, skip it and install both on your system. There maybe vary depending on your system. If you use Mac and Homebrew (or MacPorts), you’re lucky. Just do this:
$ brew install node
to install Nodejs, and:
$ brew install mongodb
or
$ sudo port install mongodb
to install MongoDB.
Next, you should familiar on how Nodejs and MongoDB works. Playing a whole day is probably enough to grasp the idea if you are a Javascript-ers and you are familiar with web server programming. If you aren’t, you should have no problem learning them in a short time, probably a week or two, if you willing to.
Okay, here’s the fun part. Oh wait, we need to install one more additional driver to connect from Nodejs to MongoDB. I’ve picked node-mongodb-native by christkv and I recommend you to install it too. It’s easy.
$ npm install mongodb
Now we’re ready to go.
To prepare a connection to MongoDB, you can create variables that define the database and its server, like this one:
and then instantiate a database client:
'test' is your database name. This is usually available from the first time you install MongoDB. The IP address is obvious. 27017 refers to the specific port of your database.
Next, you may want to define the action for insert, remove, update, and show the data. Here’s mine.
Now, the final step is to open the connection and do what you want with your already-defined action above.
You’ve noticed that there is a 'test_insert' parameter. 'test_insert' is your collection name (you can go with different name though). You can think a collection just like a table in SQL database, but without a relation (that’s why we often call it NoSQL because it has no relationship like SQL does).
These codes are far from perfect. I simplify it for the sake of, surprise surprise, simplicity and easy to understand. I uploaded the source code to my Github too. So if you want to try a little bit further, you can do whatever you like, based on my code, to explore more about Nodejs and MongoDB.
Any comments or questions are definitely welcome.
Update: I forgot to mention that for the sake of simplicity, I’m not including a callback following some operations above. Christkv, the author of the native driver I’ve used here, has give us a clue on his comment below so I just want to say that if you want (and ready) to know more about callback, please read here.
24 comments
Nice tutorial but I think you have missed one thing here: the callback parameter. Every actions you mentioned above need a callback to handle the operation results. I expect for more article on this.
Anyway, this is a good start for a beginner to learn about node and mongodb.
by CK on October 22, 2011 at 6:56 pm. #
You’re right but I think if I included the callback, it would confuse those who want to learn Nodejs from the scratch. I’ll follow this post if necessary.
by ksetyadi on October 29, 2011 at 2:36 pm. #
[...] radical movement. From Nodejs and MongoDB to Octave and [...]
by Octave and Gnuplot in Mac | Ksetyadi's [B]Log on October 24, 2011 at 2:32 am. #
Thanks so much for this!
by Wayne on October 31, 2011 at 6:30 pm. #
try this module. Its excellent
npm install mongous
by koti on October 31, 2011 at 6:59 pm. #
Did you mean
npm install mongoose?by ksetyadi on October 31, 2011 at 7:53 pm. #
There’s a good example with callbacks here
by BooUrns on October 31, 2011 at 8:31 pm. #
No, I’m quite sure he meant https://github.com/amark/mongous
by citizen428 on October 31, 2011 at 8:37 pm. #
Noted. Thanks.
by ksetyadi on October 31, 2011 at 10:10 pm. #
Wow… that looks like a pretty useful find.
by tylerh on January 7, 2013 at 11:05 am. #
it’s good :). you only real the callback if you are using a “safe” insert as otherwise it’s not actually fetching the state of the operation.
so
collection.insert(doc, {safe:true}, function(err, result) {});
by Christian Amor Kvalheim on October 31, 2011 at 10:53 pm. #
Well noted. I’ve updated the post so there’s a “caution” near the end of this post about callback to simplify things up. I should write a follow-up post then! Thanks a bunch, Chris!
by ksetyadi on October 31, 2011 at 11:14 pm. #
>$ npm install mongodb
I would mention what npm is and how to get it, especially for a beginner’s how-to guide.
by Phil Grayson on November 1, 2011 at 12:11 am. #
Yes Phil, I will mention about npm and how to install it in another post. Thanks for reminding me about defining it thoroughly.
by ksetyadi on November 1, 2011 at 10:30 am. #
[...] Nodejs and MongoDB, A Beginner’s Approach [...]
by Cheatsheet: 2011 11.01 ~ 11.07 - gOODiDEA.NET on November 7, 2011 at 6:25 pm. #
[...] Read full article. Categories: All, Async I/O, Cloud, NoSQL Tags: JavaScript, MongoDB, Node.JS, NodeJS, NoSQL /* [...]
by async I/O News » Nodejs and MongoDB, A Beginner’s Approach on November 7, 2011 at 8:28 pm. #
Nice work, keep on posting :)
by __herlambang on November 25, 2011 at 4:37 pm. #
I can’t get this code to work. Nothing is ever printed to the console. When the ‘client’ is initialized, should the values provided work or do I need to change it to my current IP address?
var Db = require(‘mongodb’).Db;
var Server = require(‘mongodb’).Server;
var client = new Db(‘test’, new Server(’127.0.0.1′, 27017, {}));
var insertData = function(err, collection) {
console.log(“Inserting data”);
collection.insert({name: “Kristiono Setyadi”});
}
var listAllData = function(err, collection) {
collection.find().toArray(function(err, results) {
console.log(results);
});
}
Anyone know why this isn’t printing anything?
by plivesey on January 29, 2012 at 9:33 am. #
Nice post, dude! To me, as a beginner, you were explaining exactly what I need. After that, I just find the advance tutorial about this. Thanks!
by Brian on February 20, 2012 at 2:22 am. #
[...] you might know, I have been catching up my self learning NodeJS and MongoDB and it turns out I like them. NodeJS is not new to you who has been playing around with JavaScript. [...]
by Welcome to 2012. Welcome New Resolutions. | Ksetyadi's [B]Log on February 20, 2012 at 2:49 am. #
Please change ‘client’ to ‘pClient’ in this snippet above:
client.open(function(err, pClient) {
client.collection(‘test_insert’, insertData);
client.collection(‘test_insert’, removeData);
// etc.
}
Thanks for your article
by Tom on May 3, 2012 at 10:32 pm. #
Why? I’ve been googling the difference between the initial client and the callback client, but cannot find information about the distinction.
by Redsandro on September 13, 2012 at 8:50 pm. #
[...] and MongoDB for Beginners http://blog.ksetyadi.com/2011/10/nodejs-and-mongodb-a-beginners-approach/ Heroku + Node.js + MongoDB (not for free heroku) [...]
by node.js » Agile Mobile Developer on June 14, 2012 at 9:38 pm. #
thanks its good for me. but i am totally confuse on this :-{} at below line:-
var client = new Db(‘test’, new Server(’127.0.0.1′, 27017, {});
but finally i know its a callback function.
I think you should mention this. (because nodejs basically based on callback function. I am new in this technology, may be i am wrong about nodejs. )
by jitender on April 2, 2013 at 10:40 pm. #