简体   繁体   中英

Python - PyMongo Insert and Update

I'm trying to do an Insert and Update operation on my seat, without the insertion of duplicate data.

My collection has the id attribute that to which I made an operating createIndex() to pass the id attribute as unique. It works because every time I try to enter new data through my code in Python, appears the message:

pymongo.errors.DuplicateKeyError: E11000 duplicate key error collection: test index: id_1 dup key: { : "400728540046889_1108783115908091" }

Ok, mongoDB is not letting enter data that is already stored in the bank. But I need my code to continue running ignoring the new data already ExSite in the bank and continue to enter new ones. The working order this:

Run the script - Insert command in the Bank - Error: Dup Key

and what I look for is:

Run the script - Insert command in the Bank - Error: Dup Key (Ignore this key) - Continues running the script looking for new data and inserting

I, tried to use the Update approach with upsert, using the command:

db.myDB.update({'id':status['id']},status,True)

Here we have:

  • Status: Contains my data
  • Id: Field in status, contains the ID of the text.

The problem is: He's entering duplicate data with the same id, only now putting all under _id: ObjectId of mongoDB.

How can i use an approach that I can enter new data and not re-enter data that already exist in the bank?

try as follows:

try:
    # insertion logic
except DuplicateKeyError as err:
    continue;  # will continue though DuplicateKeyError is raised.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM