javascript - Can two servers access a common mongoDB database -
i have 2 webservers have created on ports 3000 , 4000. 1 of webservers created database , has 3 collections..
show dbs local 0.000gb sensor_db 0.000gb use sensor_db switched db sensor_db show collections sensors templategroups templates
can 2nd server access database created ? if yes, not able access collections ..is there syntax it?
1st server:
var express = require('express'); var app= express(); var path = require('path'); var bodyparser= require('body-parser'); var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/sensor_db'); var schema = mongoose.schema; var sensorschema = new schema({ value:{ type:number, default:0}, format:{type:string, default:"default"}, id:{type:number,required:true,unique:true}, description:{type:string}, type:{type:string}, groupid:{type:number}, users:{type:array,default:[]}, admin:{type:string,default:'undefined'}, status:{type:string,default:'undefined'}, owner:{type:string,default:'undefined'}, templateid:{type:number} }); var sensor = mongoose.model('sensor',sensorschema); app.get('/sensorlist',function(req,res) { console.log("i recieved /sensorlist request"); sensor.find(function(err,data){ if (err) return console.error(err); console.log(data); res.json(data) }); }); app.post('/check/health',function(req,res){ socket.emit('data', 'i need health status', function ack(data) { console.log('data emit acknowledged monitoring server:', data); return res.json(data); }); });
2nd server:
var app = require('express')(); var server = require('http').server(app); var io = require('socket.io')(server); var express = require('express'); io.on('connection', function(socket){ console.log('connection received provisioning'); // messages provisioning server socket.on('data', function(data, ack) { console.log('message provision : ' + ': ' + data); ack('here data - 1111'); console.log("trying access sensor_db database"); sensor.find(function(err,data){ if(err) return console.error(err); console.log(data); //res.json(data); }); }); }); server.listen(4000, function(){ console.log('socket.io server listening on *:4000'); });
i error - sensor not defined
much jessi
i tried dispay collections once connected db error message : cannot read property 'haslistcollectionscommand' of null
var mongoose = require('mongoose'); mongoose.connect('mongodb://127.0.0.1:27017/sensor_db') ; console.log("successfully connected database"); //mongoose.connection.db mongoose.connection.db.listcollections().toarray(function(err, names) { if (err) { console.log(err); } else { names.foreach(function(e,i,a) { mongoose.connection.db.dropcollection(e.name); console.log("--->>", e.name); }); } });
two different servers cannot share same instance of sensor object.
var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/sensor_db'); var schema = mongoose.schema; var sensorschema = new schema({ value:{ type:number, default:0}, format:{type:string, default:"default"}, id:{type:number,required:true,unique:true}, description:{type:string}, type:{type:string}, groupid:{type:number}, users:{type:array,default:[]}, admin:{type:string,default:'undefined'}, status:{type:string,default:'undefined'}, owner:{type:string,default:'undefined'}, templateid:{type:number} }); var sensor = mongoose.model('sensor',sensorschema);
this code declaring schema 1 server not second . have declare instance in second server also.
issue not mongo data base issue sensor instance not have declaration in second server.
Comments
Post a Comment