0

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}.

Thomas
  • 705
  • 6
  • 21

1 Answers1

0

As you said they are in pymongo cursor (or data is at application end). Iterate over them, update your frames and make a bulk save call.

One more thing I'm not getting your use case. Why for update thing you are opting for limit and sort. If these are not required then you can directly go for update with multi=true

Rajat Goel
  • 2,207
  • 17
  • 35
  • Thank's for your answer. Our data points are stored in so-called inspections. Each inspection may contain several thousand images and I'd like to query them in batches of 100 images sorted by frames. Therefore, the rather complicated query. – Thomas Jun 15 '19 at 10:52