How to work with node.js and mongoDB -


i read :

and confused. how should work mongodb node.js? i’m rookie, , question may stupid.

var db = new db.mongoclient(new db.server('localhost', 27017));     db.open(function(err, database) {        //all code here?        database.close();     }); 

or every time when needing db need call:

mongoclient.connect("mongodb://localhost:27017/mydb", function(err, database) {     //all code here      database.close(); }); 

what difference betwen open , connect? read in manual open: initialize , second connect. mean? assume both same, in other way, when should use 1 instead other?

i wanna ask it's normal mongoclient needing 4 socket? running 2 mywebserver @ same time, here’s picture: http://i43.tinypic.com/29mlr14.png

edit: wanna mention isn't problem ( rather doubt :d), server works perfect. ask because wanna know if using mongodb driver correctly. now/actually use first option,init mongo dirver @ beginning , inside load put code.

i'd recommend trying mongodb tutorial offer. in same boat, breaks down nicely. in addition, there's this article on github explains basics of db connection.

in short, you're doing right.

mongoclient.connect("mongodb://localhost:27017/mydb", function(err, database) {     //all code here      var collection = database.collection('users');     var document1 = {'name':'john doe'};     collection.insert(document1, {w:1}, function(err,result){         console.log(err);     });     database.close(); }); 

Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -