I have a fairly complicated query in pymongo, which looks like that:
frames = collection.find(
{
"inspectionId": inspectionId,
"timestamp":
{
"$lt": time.time() - 3600
}
}
).limit(100).sort('img', 1)
Now I'd like to update the timestamp
field in all of the documents the cursor frames
points to. Right now I'm iterating through the cursor and set the timestamp for each document:
for frame in frames:
collection.update_one(
{
"_id" : frame["_id"]
},
{
"$set" :
{
"timestamp": time.time()
}
}
)
However, this does not seem very efficient, as it takes quite some time. Is there a handy way to update all documents at once, that are stored in a pymongo cursor?
Edit:
it's not a duplicate as I'm using .limit()
and .sort()
, which are not compatible with the update option {multi: true}
.