如何在重复的密钥错误后使用PyMongo继续插入 [英] How to continue insertion after duplicate key error using PyMongo

查看:226
本文介绍了如何在重复的密钥错误后使用PyMongo继续插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我需要在MongoDB中插入文档(如果尚不存在的话)

If I need to insert a document in MongoDB if it does not exist yet

db_stock.update_one(document, {'$set': document}, upsert=True)

.将胜任工作(如果我做错了,请随时纠正我)

.will do the job (feel free to correct me if I am wrong)

但是,如果我有一份文档列表,并且想要全部插入它们,那是最好的方法吗?

But if I have a list of documents and want to insert them all what would be a best way of doing it?

问题有单记录版本,但是我需要一个它的大量版本,因此有所不同.

There is a single-record version of this question but I need an en mass version of it, so it's different.

让我改写我的问题.我有数百万个文档,其中很少可以存储.如何在几秒钟而不是几分钟/几小时内将剩余的存储在MongoDB中?

Let me reword my question. I have millions of documents, few of which can be already stored. How do I store remaining ones in MongoDB in a matter of seconds, not minutes/hours?

推荐答案

您需要使用 ordered 选项文档中所述:

已排序(可选):如果将按提供的顺序在服务器上串行插入True(默认)文档.如果发生错误,则所有剩余的插入都将中止.如果为False,文档将以任意顺序(可能并行)插入到服务器上,并且将尝试所有文档插入.

ordered (optional): If True (the default) documents will be inserted on the server serially, in the order provided. If an error occurs all remaining inserts are aborted. If False, documents will be inserted on the server in arbitrary order, possibly in parallel, and all document inserts will be attempted.

这意味着即使重复的密钥错误,插入也将继续.

Which means that insertion will continue even if there is duplicate key error.

演示:

>>> c.insert_many([{'_id': 2}, {'_id': 3}])
<pymongo.results.InsertManyResult object at 0x7f5ca669ef30>
>>> list(c.find())
[{'_id': 2}, {'_id': 3}]
>>> try:
...     c.insert_many([{'_id': 2}, {'_id': 3}, {'_id': 4}, {'_id': 5}], ordered=False)
... except pymongo.errors.BulkWriteError:
...     list(c.find())
... 
[{'_id': 2}, {'_id': 3}, {'_id': 4}, {'_id': 5}]

如您所见,将带有_id 4,5的文档插入到集合中.

As you can see document with _id 4, 5 were inserted into the collection.

值得注意的是,使用 insertMany 方法.您所需要做的只是将未记录的选项ordered设置为false.

It worth noting that this is also possible in the shell using the insertMany method. All you need is set the undocumented option ordered to false.

db.collection.insertMany(
    [ 
        { '_id': 2 }, 
        { '_id': 3 },
        { '_id': 4 }, 
        { '_id': 5 }
    ],
    { 'ordered': false }
)

这篇关于如何在重复的密钥错误后使用PyMongo继续插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆