3

我正在尝试使用 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')}
4

2 回答 2

0

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

while True:
    cursor = db.collection.watch(full_document='updateLookup')
    while cursor:
        print next(cursor)['documentKey']
于 2018-03-06T00:36:21.537 回答
0

我发现这篇文章非常有用,他们确实提到了你的情况:

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

于 2018-07-02T18:40:05.540 回答