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
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) { ... });
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With