猫鼬-保持本地数据库与远程数据库同步 [英] Mongoose - Keep local database in sync with remote database

查看:68
本文介绍了猫鼬-保持本地数据库与远程数据库同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以访问两个我想保持同步的数据库,一个是新数据库,另一个是现有数据库,它们将位于单独的物理位置.新版本将用于服务外部API,因此,为了减少请求时间,我认为只查询本地数据库中的API请求是有道理的.

I have access to two separate databases that I'd like to keep in sync, a new one and an existing one, which will be in separate physical locations. The new one is going to be used to service an external API, so to cut down on request time, I think it makes sense to only query the local database for API requests.

我最初的方法是使用mongoose.createConnection并将本地集合限制为次要元数据并直接访问远程集合,但这就是我现在要避免的事情.

My initial approach was to use mongoose.createConnection and limit the local collection to minor metadata and directly access the remote collection, but that's what I'm now looking to avoid.

另一种方法可能是使用mongoose.createConnection定期查询远程数据库并更新本地数据库,但是如果我想进行频繁的更新,则可能会很昂贵.

Another approach might be to use mongoose.createConnection to periodically query the remote db and update the local one, but it could be costly if I want to do make frequent updates.

有很多方法可以降低成本-例如,现有数据库的相关集合中有一个lastUpdated属性,该属性可用于将远程查询限制为最近更新的记录,例如:

There are ways to cut down the cost - for example, there is a lastUpdated property in the relevant collection on the existing database, which could be used to limit the remote query to recently updated records such as:

RemoteCollection.find({
   lastUpdated: {$gte: Date.now() - lookbackPeriod}
})

但是我想知道是否有mongoose/mongoDB的任何本机功能可以更有效地用于进行更新.我还考虑过mongodumpmongorestore来保留所需记录的完整本地副本,但这似乎也很昂贵.

But I'm wondering if there's any native functionality of mongoose/mongoDB that can be used more efficiently make the updates. I also thought about mongodump and mongorestore to keep a full local copy of the records I needed, but that also seems costly.

感谢您的帮助.

推荐答案

经过一番阅读并感谢Jake的评论,看来它正在工作.我需要做更多的设置,但是下面的代码应该可以正常工作,并且基于docs的这一部分:

After a bit of reading and thanks to Jake's comment, it looks like it's working. I need to do some more setup, but the code below should work and is based on this section from the docs:

https://mongoosejs.com/docs/models.html#change-streams

第一步将以--replSet标志开始mongod:

mongod --replSet "rs0" --bind_ip localhost,<hostname(s)|ip address(es)>

然后关闭并重新启动mongo并在数据库上运行rs.initiate().然后,您可以使用rs.status()检查副本集的状态.如果该命令有效并返回结果,则副本集功能应该存在.

Then close and restart mongo and run rs.initiate() on the database. You can then check the status of the replica set with rs.status(). If that command works and returns a result, the replica set functionality should be there.

然后在Node中,您可以执行以下操作:

Then within Node, you can do something like this:

// The docs reference creating a new model but you can just import an existing one

const RemotePerson = require('./models/RemotePerson');
const LocalPerson = require('./models/LocalPerson');

RemotePerson.watch().on('change', data => {
    if (data.operationType === "insert") {
        LocalPerson.create(data.fullDocument);
    } else if (data.operationType === "update") {
        LocalPerson.findByIdAndUpdate(data.documentKey, {
            $set: data.updateDescription.updatedFields
        });
    }
});

这篇关于猫鼬-保持本地数据库与远程数据库同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆