Mass upsert not working pymongo

I am trying to update a document if it exists and insert if it does not exist in the collection. I am inserting pandas data frame records as documents into a collection based on _id . Inserting a new document works fine, but updating fields in the old document does not work.

bulk = pymongo.bulk.BulkOperationBuilder(pros_rides,ordered=False)
for doc in bookings_df:
    bulk.find({ "_id": doc["_id"] }).upsert().update({
        "$setOnInsert": doc
    })
response = bulk.execute()

What am I missing?

+4
source share
1 answer

, ; "$ setOnInsert" , , . , , , .

- :

bulk = pros_rides.initialize_unordered_bulk_op()
for doc in books_df:
    bulk.find({'_id': doc['_id']}).upsert().replace_one(doc)

bulk.execute()
+5

Source: https://habr.com/ru/post/1660063/


All Articles