Question: 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 !

Answer: 
[Unordered insert](https://docs.mongodb.com/v5.0/reference/method/db.collection.insert/#perform-an-unordered-insert) will do the trick

```
db.products.insert(
    [{ imagename: "pen1", qty: 21 },
     { imagename: "pen", qty: 20 },
     { imagename: "eraser", qty: 25 }],
    { ordered: false }
)

```
**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.