Reputation: 538
I have to insert 3 recordset from array 1 already exists and 2 are new
e.g:
db.products.insert(
[
{ imagename: "pen1", qty: 21 },
{ imagename: "pen", qty: 20 },
{ imagename: "eraser", qty: 25 }
]
)
Wherein "{ imagename: "pen", qty: 20 }"` already exists and has unique key on field "imagename" in mongodb
as for now none of them are getting inserted and throwing err: 'E11000 duplicate key error index: mongotest.mothership.$imagename_1 dup
any suggestion how to insert remaining two ignoring error in single go !
Upvotes: 8
Views: 18721
Reputation: 14789
Set the ordered
option to false
. This is not a trick, but a reliable method to achieve it. If errors occur, you may obtain information regarding each of them.
const documents = [{name: "Star Wars"}, {name: "Sword Art Online"}];
db.product.insertMany(documents, {ordered: false});
If you're using Mongoose, use Model.insertMany()
.
Perform an Unordered Insert
The following example performs an unordered insert of three documents. With unordered inserts, if an error occurs during an insert of one of the documents, MongoDB continues to insert the remaining documents in the array.
Error Handling
Inserts throw a
BulkWriteError
exception.Excluding Write Concern errors, ordered operations stop after an error, while unordered operations continue to process any remaining write operations in the queue.
db.collection.insertMany()
Inserts multiple documents into a collection.
db.collection.insertMany( [ <document 1> , <document 2>, ... ], { writeConcern: <document>, ordered: <boolean> } )
ordered
: Optional. A boolean specifying whether the mongod instance should perform an ordered or unordered insert. Defaults totrue
.
Upvotes: 11
Reputation: 2033
Unordered insert will do the trick
db.products.insert(
[{ imagename: "pen1", qty: 21 },
{ imagename: "pen", qty: 20 },
{ imagename: "eraser", qty: 25 }],
{ ordered: false }
)
Upvotes: 16
Reputation: 179
Note: ensureIndex is deprecated since v3.0.0 https://docs.mongodb.com/manual/reference/method/db.collection.ensureIndex/
The following response is left as-is (since ensure
is now an alias for create
- both will work at present), but for newer versions you should replace db.products.ensureIndex
with db.products.createIndex
.
first you need to create unique index. ex:
db.products.ensureIndex({ url: 1 }, { unique: true, background: true, dropDups: true })
then insert
db.products.insert(
[{ imagename: "pen", qty: 21 },
{ imagename: "pen", qty: 20 },
{ imagename: "eraser", qty: 25 }],
{ ordered: false }
)
it will happen error : E11000 duplicate key error collection: mongotest.products index: imagename_1 dup key :{:'pen'}。 it doesn't matter. you can try ... catche
try {
db.products.insert(
[{ imagename: "pen", qty: 21 },
{ imagename: "pen", qty: 20 },
{ imagename: "eraser", qty: 25 }],
{ ordered: false }
)
}catch(e){
print(e)
}
Note: when you are calling ensureIndex, you cannot call insert.because ensureIndex is asynchronous, or ensureIndex will fail.
Upvotes: -1