How to work with node.js and mongoDB -
i read :
- how manage mongodb connections in node.js web application?
- http://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html
- how can set mongodb on node.js server using node-mongodb-native in ec2 environment?
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
Post a Comment