python - pymongo批量插入主键冲突怎么整?
PHPz
PHPz 2017-04-18 10:18:07
0
2
649

在pymongo的使用过程中,发现当使用insert函数批量插入时,如果主键有冲突,那么就会被中断。而我想在批量插入的同时保证程序不会被中断。官方文档翻了一遍,网上也没有找到解决办法,有没有同志知道如何解决这个问题?
比如: data = [ { "_id" : "1", “name” : "frog"}, { "_id" : “1”, "age" : 33 } , {"_id" : "2", "sex" : "boy" } ]

  collection.insert(data) 

此时会因为第一个json和第二个json的主键相同,而导致程序被中断。而我希望程序能跳过第二个json,把第三个json也插入数据库。
谢谢大家

PHPz
PHPz

学习是最好的投资!

reply all(2)
刘奇

Reasonable circumvention of Google: http://stackoverflow.com/ques...
In fact, the more important question is, knowing that duplicate primary keys are problematic, why not prevent it from the source, but choose such a method? Patch way? Personally I don't agree with this approach, which often leads to more and more complicated situations.

Ty80

try except

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!