避免mongodb批量插入重复键错误

时间:2023-02-14 04:18:27

how can i execute a bulk insert and continue in case of duplicate key error?

如何执行批量插入并在重复键错误的情况下继续?

I have a collection with an unique index on the id field (not _id) and some data in it. Then I get more data and i want to add only the non-present documents to the collection.

我有一个集合,在id字段(不是_id)上有一个唯一索引,其中包含一些数据。然后我获得更多数据,我想只将非现有文档添加到集合中。

I have the following code:

我有以下代码:

let opts = {
  continueOnError: true, // Neither
  ContinueOnError: true, // of
  keepGoing: true,       // this
  KeepGoing: true,       // works
};
let bulk = collection.initializeUnorderedBulkOp( opts );
bulk.insert( d1 );
bulk.insert( d2 );
bulk.insert( d3 );
...
bulk.insert( dN );
let result = yield bulk.execute( opts ); // this keep throwing duplicate key error

And i just want to ignore the errors and let the bulk finish with all the queued operations.

我只想忽略错误,让批量完成所有排队操作。

I searched in npm module api and in the MongoDB api for Bulk, initializeUnorderedBulkOp and the docs for Bulk write with no luck.

我搜索了npm模块api和MongoDB api中的Bulk,initializeUnorderedBulkOp和Bulk写的文档没有运气。


Also in the docs for Unordered Operations they say:

同样在无序操作的文档中,他们说:

Error Handling

If an error occurs during the processing of one of the write operations, MongoDB will continue to process remaining write operations in the list.

如果在处理其中一个写操作期间发生错误,MongoDB将继续处理列表中的剩余写操作。

Wich is not true (at least in my case) :(

这不是真的(至少在我的情况下):(

Any ideas are welcome!

欢迎任何想法!

1 个解决方案

#1


2  

You can use db.collection.insertMany(), (new in version 3.2.) with:

您可以使用db.collection.insertMany(),(版本3.2中的新增功能):

ordered:false

With ordered to false, and in case of duplicate key error, the insert operation would continue with any remaining documents.

如果命令为false,并且在重复键错误的情况下,插入操作将继续任何剩余的文档。

Here is link to documentation: https://docs.mongodb.com/v3.2/reference/method/db.collection.insertMany/

以下是文档链接:https://docs.mongodb.com/v3.2/reference/method/db.collection.insertMany/

#1


2  

You can use db.collection.insertMany(), (new in version 3.2.) with:

您可以使用db.collection.insertMany(),(版本3.2中的新增功能):

ordered:false

With ordered to false, and in case of duplicate key error, the insert operation would continue with any remaining documents.

如果命令为false,并且在重复键错误的情况下,插入操作将继续任何剩余的文档。

Here is link to documentation: https://docs.mongodb.com/v3.2/reference/method/db.collection.insertMany/

以下是文档链接:https://docs.mongodb.com/v3.2/reference/method/db.collection.insertMany/