3

I am trying to use Change Streams to lookup/monitor the changes to a mongo collection.

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

While using updateMany to update multiple documents at once, I'm not able to see all the changed documents. However if I update one at a time, then I see each change in the Stream.

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

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

Results:

{u'_id': ObjectId('580694d039811a468b96fc7b')}
{u'_id': ObjectId('58aeebed39811a468b974e97')}
{u'_id': ObjectId('59efe28b39811a468b97b9cc')}
Jeremy
  • 682
  • 2
  • 8
  • 17
user9447867
  • 31
  • 1
  • 3

2 Answers2

0

I figured out the issue. Need to iterate through the change stream cursor to get all updates.

while True:
    cursor = db.collection.watch(full_document='updateLookup')
    while cursor:
        print next(cursor)['documentKey']
user9447867
  • 31
  • 1
  • 3
0

I found this article very informative, and they did mention your situation:

Operations that affect multiple documents, like insertMany(), generate one 'change' event per affected document. For example, if you call insertMany() with 2 documents, you'll get two 'change' events. In general, each 'change' event describes changes to a single document.

Zack S
  • 1,412
  • 1
  • 23
  • 37