简体   繁体   English

环回事件流跟踪服务器端模型中的更改

[英]Loopback event stream track changes in a model on server side

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). 我陷入了一种情况,我想要同步两个mongoDB,一个是本地机器,第二个是远程(在mlab沙箱上)。 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. 首先我使用mongorestore转储使用mongodump进行更改的集合我正在远程MongoDB上恢复该集合。

But there are two problems which I am facing. 但是我面临两个问题。

First is how can I update the collection on a remote mongoDB. 首先是如何在远程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. 我正在使用loopback框架和事件流npm模块将更改发送到客户端。 but I unable to read change stream on server side. 但我无法在服务器端读取更改流。

my server\\boot\\realtime.js is : 我的server \\ boot \\ realtime.js是:

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);
  });

}

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. 第二个问题LoopBack在底层数据库中的数据发生变化时不发送事件,但仅在模型发生变化时才发送。 Mongo 3.6 provides change stream functionality and you can use it to trigger model change/perform your functionality. Mongo 3.6提供了更改流功能,您可以使用它来触发模型更改/执行功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM