Ask: Python MongoDB to insert multiple records (arrays), but not if they already exist.

problem description

how do I insert an array into Collection? If item_id exists, do not insert?

for example:

item_arr = [
    {"item_id": "1",
     "title" : "AAA"
    },
    {"item_id": "2",
     "title" : "BBB"
    }
]

I would like to ask how to insert item_arr into Collection. If there is a record item_id = 1, only the record item_id = 2 is inserted.

the environmental background of the problems and what methods you have tried

1.pymongoupdate_one upsert = true 

2.update_manyupsert ?
       
3.insert_manyupsert


May.27,2021

your solution 1 is slow because it becomes multiple operations, which needs to be transmitted through the network each time, which has a great impact on performance. There are several corresponding solutions, depending on your environment version and driver version (for specific usage, please consult the documentation of the corresponding version driver. I give the link to pymongo 3.7below):

< H2 > use the bulk_write method < / H2 >

initialize multiple update methods and execute them in a bulk. python/current/api/pymongo/collection.html-sharppymongo.collection.Collection.bulk_write" rel=" nofollow noreferrer "> bulk_write documentation

< H2 > use a unique index + insert_many + ordered=false < / H2 > The

insert_many method is also essentially a bulk operation, but it has less search than update, so it is theoretically faster.
is also inserted without a search, and it has no way to determine whether the inserted data exists, which needs to be ensured by a unique index on item_id .
also by default insert_many uses ordered=true and stops when it encounters an insertion error ( item_id repeat), so you need ordered=false .

Menu