Connection model for Mongo using Node.js

73 views
Skip to first unread message

Travis Briggs

unread,
May 9, 2012, 8:26:49 PM5/9/12
to nod...@googlegroups.com
Hello,

I just got started with Node.js and I'm trying to use Mongo.

I'm just looking for a basic connection model for Mongo when using

Node.js. I'm using the native driver
(https://github.com/mongodb/node-mongodb-native) and I have the
following for getting a "database object":

At the top of the file I have
var db = new mongo.Db('app_name', new mongo.Server("127.0.0.1", 27017, {}));

Then, inside my request handler:

function handle_request(request, response) {
       db.open(function(err, p_client) {
               db.collection('users', function(err, collection) {
                       ...etc...

So it looks to me like I'm opening a connection for every request.
Should I instead call db.open immediately after initializing it and
use the same connection for multiple requests? Should I actually make
it more individual and define the db object inside the request
handler?

This is preliminary to any discussions as to the optimal model for
extreme throughput, and is more on a par with: how do I ensure that
I'm using the objects correctly, I get correct results and I don't
hang the resources of the process/OS.

Thanks,
-Travis

Alexey Petrushin

unread,
May 10, 2012, 10:07:57 AM5/10/12
to nod...@googlegroups.com
You should reuse existing connection, i.e. call `open` in initialization. Also there are lots of wrappers that make API of MongoDB Driver more friendly and remove some of callbacks like mongoskin, mongojs or http://alexeypetrushin.github.com/mongo-lite 

James O'Reilly

unread,
May 10, 2012, 11:19:02 PM5/10/12
to nod...@googlegroups.com
I just want to add to Alexey's list, Mongoose (http://mongoosejs.com/) is another good option.

jason.桂林

unread,
May 11, 2012, 2:21:39 AM5/11/12
to nod...@googlegroups.com
https://github.com/kissjs/node-mongoskin

2012/5/11 James O'Reilly <spam.i...@hotmail.com>
I just want to add to Alexey's list, Mongoose (http://mongoosejs.com/) is another good option.


On Thursday, May 10, 2012 10:07:57 AM UTC-4, Alexey Petrushin wrote:
You should reuse existing connection, i.e. call `open` in initialization. Also there are lots of wrappers that make API of MongoDB Driver more friendly and remove some of callbacks like mongoskin, mongojs or http://alexeypetrushin.github.com/mongo-lite 

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en



--
Best regards,

桂林 (Gui Lin)

guileen@twitter

Reply all
Reply to author
Forward
0 new messages