user1069536
user1069536

Reputation:

How can i retrieve modified documents after an update operation in mongodb with pymongo?

I'm using an update operation with upsert. I want to retrieve all documents that have been modified after an update.

for key in categories_links:
    collection.update({"name" : key}, {"name": key ,"url" : categories_links[key]}, True)

Upvotes: 4

Views: 1394

Answers (2)

poundifdef
poundifdef

Reputation: 19353

To my knowledge, pymongo will not return a list of all of the records which have been modified by an update.

However, if you are using a replicaset, you might be able to accomplish this by looking at the oplog.

According to the documentation:

The oplog must translate multi-updates into individual operations in order to maintain idempotency. This can use a great deal of oplog space without a corresponding increase in data size or disk use.

If you want to keep track of each element being updated, you might instead do a find(), and then loop through those to do an individual update() on each. Obviously this would be much slower, but perhaps a tradeoff for your specific use case.

Upvotes: 0

Roman Rdgz
Roman Rdgz

Reputation: 13254

You should use a timestamp field in your documents if you ever need to find which ones where updated and when. There is a BSON type for that.

Upvotes: 1

Related Questions