mongodb - MongoClient multiple connections using Node.js -
i want create module nodejs connecto mongodb. i've seen new, better approach use mongoclient, can't know how can make concurrent operations on database. goal want achieve have functions abstract database, following:
exports.insertitem(item){ //whatever }
according docs, supposed connect database way:
mongoclient.connect("mongodb://localhost:27017/integration_test", function(err, db) { //do stuff on db object });
the problem how supposed reuse db object if it's not in scope can use export functions in node? supposed make mongoclient.connect() on every function deals db?
you make single db connection , reuse everywhere
a typical pattern modules is
export.myinsert = function(db) { return function(whatever) { } }
and do
require('mymodule')(db)
have @ example
Comments
Post a Comment