node.js - Accessing >=2 collections from the same db connection using Mongo-lite results in error -


i trying access more single mongo collection same db connection using mongo-lite. here sample express app.

var express = require('express')   ; var app = express(); var http = require('http'); var mongolite = require('mongo-lite');  db = mongolite.connect("mongodb://localhost/fnard", ['col1','col2']); app.use(express.bodyparser()); app.use(app.router);  app.get('/', function(request, response){     db.collection('col1').insert({hi:5},function(){});     db.collection('col2').insert({hi:5},function(){});     console.log(request.body);      // json     response.send(request.body);    // echo result });  http.createserver(app).listen(3000, function () {     console.log('express server listening on port ' + '3000'); }); 

here error getting on doing on /:

error: server or replsetservers instance cannot shared across multiple db instances 

however if use following code inserts documents expected

var express = require('express')   ; var app = express(); var http = require('http'); var mongolite = require('mongo-lite');  db = mongolite.connect("mongodb://localhost/fnard", ['col1']); db2 = mongolite.connect("mongodb://localhost/fnard", ['col2']); app.use(express.bodyparser()); app.use(app.router);  app.get('/', function(request, response){     db.collection('col1').insert({hi:5},function(){});     db2.collection('col2').insert({hi:5},function(){});     console.log(request.body);      // json     response.send(request.body);    // echo result }); 

do need create new connection each collection want access? mongo-lite docs seem suggest isn't case - .connect() option lets specify collections want use doesn't seem work.

mongo-lite docs link

a race condition giving error. following works me:

var express = require('express'); var app = express(); var http = require('http'); var mongolite = require('mongo-lite');  db = mongolite.connect("mongodb://localhost/fnard", ['col1','col2']); app.use(express.bodyparser()); app.use(app.router);  app.get('/', function(request, response){     db.collection('col1').insert({hi:5},function(){         db.collection('col2').insert({hi:5},function(){             console.log(request.body);      // json             response.send(request.body);    // echo result         });     }); });  http.createserver(app).listen(3000, function () {     console.log('express server listening on port ' + '3000'); }); 

you need nest callbacks. or, can use library async:

var express = require('express'); var async = require( 'async' ); var app = express(); var http = require('http'); var mongolite = require('mongo-lite');  db = mongolite.connect("mongodb://localhost/fnard", ['col1','col2']); app.use(express.bodyparser()); app.use(app.router);  app.get('/', function(request, response){     async.series( [          // first insert         function ( callback ) {             db.collection('col1').insert({hi:5},callback);         },          // second insert         function ( callback ) {             db.collection('col2').insert({hi:5},callback);         }      // send response     ], function ( error, results ) {         console.log(request.body);      // json         response.send(request.body);    // echo result     } ); });  http.createserver(app).listen(3000, function () {     console.log('express server listening on port ' + '3000'); }); 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -