简体   繁体   English

Python - PyMongo插入和更新

[英]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. 我的集合有id属性,我将操作createIndex()传递给id属性作为唯一。 It works because every time I try to enter new data through my code in Python, appears the message: 它的工作原理是因为每次我尝试通过Python中的代码输入新数据时,都会显示以下消息:

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. 好吧,mongoDB不允许输入已存储在银行中的数据。 But I need my code to continue running ignoring the new data already ExSite in the bank and continue to enter new ones. 但我需要我的代码继续运行忽略银行中已经存在ExSite的新数据并继续输入新数据。 The working order this: 这个工作单:

Run the script - Insert command in the Bank - Error: Dup Key 运行脚本 - 在Bank中插入命令 - 错误: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 运行脚本 - 在Bank中插入命令 - 错误:Dup键(忽略此键) - 继续运行脚本查找新数据并插入

I, tried to use the Update approach with upsert, using the command: 我尝试使用upsert的Update方法,使用命令:

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. Id:状态字段,包​​含文本的ID。

The problem is: He's entering duplicate data with the same id, only now putting all under _id: ObjectId of mongoDB. 问题是:他正在输入具有相同ID的重复数据,现在只将所有数据放在mongoDB的_id:ObjectId下。

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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