Hi developers,
I am new to node.js and mongodb and trying to use native mongodb driver for
node to read database.
now my very basic example works good, but this is not i really want to
achieve at the end of the day.
//Testing installation
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://127.0.0.1:27017/testDB', function(err, db) {
var collection = db.collection('testTable');
collection.find().toArray(function(err, results) {
console.log(results);
db.close();
});
});
I want to serve my data via http through node server, if on every operation
i would be closing the connection, then i would be ended up creating
thousands of connections simultaneously, and Main objective to choose
node+mongodb is performance and real-time (of-course i will be using ws to
achieve real-time objectives) information distribution system goes for a
toss.
// file dbconnection.js
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://127.0.0.1:27017/testDB', function(err, db) {
//Now here i dont want to close the db connection but want to reuse
exports.getConnection = db;
});
});
// file TestConnection.js
var dbconnection = require('./dbconnection.js')
var db = dbconnection.getConnection;
//Error on next line (basically db object is null)
var collection = db.collection('testTable');
collection.find().toArray(function(err, results) {
console.dir(results);
});
I am very much confuse how i can use require funcationality effectively to
pass object instance across project. one i can think of present scenario is
creating db connection instance on the main server.js page where i am
creating http object and pass this object to function call.
Please advice me what are the best possible ways to achieve such
requirements. so that I will create main instance only once and keep on
using it for my every request come via node HTTP server.
Thanks in advance,
Manish Bansal
[email protected]
--
Job board: http://jobs.nodejs.org/
New group rules:
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules:
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 unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/a5d7fa17-741d-4373-ac56-0f99cfd74062%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.