Himmators
Himmators

Reputation: 15026

Insert documents into collection unless they are already inserted?

I get a list of objects from an API:

let sold = [
 {  objId: 3240747,
    soldDate: '2018-09-27',
    soldPrice: 4610000,
    apartmentNumber: '1202',
    soldPriceSource: 'bid',
  },
  { objId: 3234263,
    soldDate: '2018-09-24',
    soldPrice: 2580000,
    soldPriceSource: 'bid',
  }
...
]

I store these in a collection:

soldCollection.insertMany(sold)

Some of the objects have been retrieved before, and I only want to store the once that are not already in the database.

 dbo.collection("sold").createIndex({ "objId": 1 }, { unique: true })

What would be an efficient way of doing this? Should I ask for each object before storing it or is there method for dealing with this?

Upvotes: 3

Views: 55

Answers (1)

mickl
mickl

Reputation: 49985

By default insertMany will stop inserting when first error occurs (E11000 duplicate key error in this case). You can change that behavior by specifying ordered parameter set to false. In that case you'll get a list of errors from failed inserts however all valid documents will be inserted succesfully:

db.sold.insertMany(sold, { ordered: false })

docs example here

Upvotes: 1

Related Questions