1

I have stuck in a situation that I want to sync two mongoDB one is at local machine and second is at the remote (on a mlab sandbox). for sync I am doing as:

first I dump the collection which have changes using mongodump next by using mongorestore I am restoring that collection on a remote MongoDB.

But there are two problems which I am facing.

First is how can I update the collection on a remote mongoDB. for example : on change collection should I replace the whole collection on remote side or use other way ? and what is best way for doing this.

And the second one problem is how can I detect changes in a collection. or in a whole database. I am using loopback framework and event-stream npm module for sending changes to the client side. but I unable to read change stream on server side.

my server\boot\realtime.js is :

var es = require('event-stream');
var sync = require('../sync');

module.exports = function(app) {

  var completeOrder = app.models.completeOrder;

  completeOrder.createChangeStream(function(err, changes) {

   sync('completeOrder',function(data){

      console.log(data);
    },function(err){

      console.log(err);
    });
   changes.pipe(es.stringify()).pipe(process.stdout);
  });

}
Osama Saeed
  • 149
  • 8

1 Answers1

0

On second problem LoopBack doesn’t send events when there is change in data in underlying DB but only when there is change in model. Mongo 3.6 provides change stream functionality and you can use it to trigger model change/perform your functionality.