在pymongo的使用过程中,发现当使用insert函数批量插入时,如果主键有冲突,那么就会被中断。而我想在批量插入的同时保证程序不会被中断。官方文档翻了一遍,网上也没有找到解决办法,有没有同志知道如何解决这个问题?
比如: data = [ { "_id" : "1", “name” : "frog"}, { "_id" : “1”, "age" : 33 } , {"_id" : "2", "sex" : "boy" } ]
collection.insert(data)
此时会因为第一个json和第二个json的主键相同,而导致程序被中断。而我希望程序能跳过第二个json,把第三个json也插入数据库。
谢谢大家
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.
try except