在Mongo shell中创建Mongo更改流

3

MongoDB在其3.6版本中引入了变更流
我想在我的代码中实现mongo变更流,并想要了解它的工作原理。我将使用java驱动程序进行实现,这非常清晰明了。 但我想知道是否有任何方法在mongo shell上打开变更流?找不到太多相关资源。

1个回答

7
db.collection.watch 命令会打开一个变更流游标,与变更流相关。
例如:
watchCursor = db.getSiblingDB("data").sensors.watch(
   [
      { $match : {"operationType" : "insert" } }
   ]
)

while (!watchCursor.isExhausted()){
   if (watchCursor.hasNext()){
      print(tojson(watchCursor.next()));
   }
}

更多详细信息请参阅文档


把这个更明显是个好主意。我已经更新了答案,包括这个:print(tojson(watchCursor.next())); - glytching

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接