Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InsertMany throw E11000 duplicate key error but insert successfully

I am using mongoose to insert bulk data to database. The insertMany throw E11000 duplicate key error although the data is saved succeed in database.

Here is my codes:

  var data = [
      {_id: '598d7ae7c3148a6d853866e8'}, 
      {_id: '598d7afdc3148a6d853866f4'}
    ]
Movies.insertMany(data, function(error, docs) {});

This happen infrequently.

Appreciated any help.

Thanks

like image 764
pham cuong Avatar asked Dec 10 '22 10:12

pham cuong


2 Answers

MongoDB doesn't support transactions.

Because your operation is ordered (the default), MongoDB starts inserting documents until either all documents have been written, or it hits an error (for instance, when one of the _id's already exists in the database), at which point MongoDB will stop inserting the rest of the documents.

So if you try to insert 10 documents, and after 5 documents an error occurs, the first 5 documents will get inserted, but the rest won't. This is documented here.

That same document also suggests how to deal with this: disable ordering, because "With ordered [set] to false, the insert operation would continue with any remaining documents."

Movies.insertMany(data, { ordered : false }, function(error, docs) { ... });
like image 59
robertklep Avatar answered May 23 '23 00:05

robertklep


I had figured out the problem. It's a bug of Studio 3T. I drop all collections then import new collections then refresh collections tree. Some dropped collections are back.

like image 27
pham cuong Avatar answered May 23 '23 02:05

pham cuong