How could meteor know MongoDB changes instantly?

2019-01-14 19:21发布

问题:

I had been wondering about knowing mongoDB changes instantly for a while and best solution that I have ever seen, is tailing mongoDb logs and I just had given wondering up till met Meteor.

When there is a changes on mongoDB somehow(through Meteor or directly), Meteor understands the changes instantly and apply them. We could see it on publishing changes or observing(observe or observeChange) changes.

I guess that, Meteor could understand it by tailing mongoDB logs as the best solution that I have ever seen suggests. But this assumption also make me ask that, Meteor can run with a mongoDB running on different host and if the way that meteor knows MongoDB changes instantly is tailing logs, how could Meteor tails logs of mongoDB running on different machine(different host) and handle changes? Tailing on different host requires painful things and I think Meteor does not follow this way.

I think tailing logs is not my answer because of this question.

I am curious about this knowledge. Cause, I think that, if I have it, I could use it on my other works without Meteor.

So, how could really Meteor knows mongoDB changes instantly? And I wanna ask once again; what would be the best way to know about mongoDB changes instantly?

Thank You!

回答1:

Yes you are right this is from new new oplog tailing functionality. Meteor pretends that it is mongodb in a replica set and it tails mongodb's operation logs for collections it is monitoring.

Usually databases need multiple servers so that they can stay up in case one fails. MongoDB has this functionality in a replica set.

So this is where meteor comes in. Meteor pretends to be a mongodb replica database so when any data changes on this operations log for other copies to see, Meteor knows that data has changed and can push this new data down to the client instantly.

Usage in production

You can use a different mongo database with this new functionality as you set MONGO_OPLOG_URL. You will need to set mongodb as a replica set first so that the operations log is enabled

This was a very recent introduction put in just last week in version 0.7 you can see the blog post about it below

More links about it:

  • https://www.meteor.com/blog/2013/12/17/meteor-070-scalable-database-queries-using-mongodb-oplog-instead-of-poll-and-diff
  • https://www.youtube.com/watch?v=_dzX_LEbZyI
  • https://github.com/meteor/meteor/wiki/Oplog-Observe-Driver