【问题标题】:Mongo Change Streams not returning all the updates to the collectionMongodb Change Streams 未将所有更新返回到集合
【发布时间】:2018-08-13 14:59:03
【问题描述】:

我正在尝试使用 Change Streams 来查找/监控对 mongo 集合的更改。

cursor = db.collection.watch(full_document='updateLookup')
document = next(cursor)
print document['documentKey']

使用 updateMany 一次更新多个文档时,我无法查看所有更改的文档。但是,如果我一次更新一个,那么我会看到 Stream 中的每个更改。

db.collection.updateMany( {"contacts.firstName": "XXXXXX"}, { $set: {"LocationId" : 11111} } )

{ "acknowledged" : true, "matchedCount" : 77, "modifiedCount" : 77 }

结果:

{u'_id': ObjectId('580694d039811a468b96fc7b')}
{u'_id': ObjectId('58aeebed39811a468b974e97')}
{u'_id': ObjectId('59efe28b39811a468b97b9cc')}

【问题讨论】:

    标签: mongodb mongodb-oplog


    【解决方案1】:

    我发现了这个问题。需要遍历更改流游标以获取所有更新。

    while True:
        cursor = db.collection.watch(full_document='updateLookup')
        while cursor:
            print next(cursor)['documentKey']
    

    【讨论】:

      【解决方案2】:

      我发现this article 信息量很大,他们确实提到了你的情况:

      影响多个文档的操作,例如 insertMany(),会为每个受影响的文档生成一个“更改”事件。例如,如果您使用 2 个文档调用 insertMany(),您将获得两个“更改”事件。通常,每个“更改”事件都描述了对单个文档的更改。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-02-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多