每当集合中插入新文档时获取文档

时间:2020-02-25 13:13:05

标签: node.js mongodb mongoose

我使用mongodb和node js开发应用程序。每当要在集合中插入新文档(从python端插入文档)时,我都希望使用node js mongoose从集合中获取文档。目前,我正在使用setInterval以1秒的时间间隔运行查询。

因此,有没有setinterval的另一种有效方法。

2 个答案:

答案 0 :(得分:1)

您可以在mongodb的tailable cursors上使用oplog

答案 1 :(得分:1)

我想可以通过使用mongodb change streams轻松实现。

您可以在此处通过代码示例了解更多信息。

power of mongodb change streams

基本上侦听插入操作类型并做出相应的反应。这比跟踪oplog效率更高。