1

I'm using insertMany to insert documents collected from the spotify api, with the below structure:

{
  spotify_id: "6400dnyeDyD2mIFHfkwHXN",
  name: "Pablo Honey",
  ...
}

Using:

  db.collection.insertMany(albums)

(where albums is an array containing objects of the above structure)

On subsequent calls the same operation runs, but I would like to only insert new documents, i.e not those where spotify_id already exists in the database

Tom Keyte
  • 71
  • 1
  • 10

1 Answers1

1

Assuming you want a unique index on spotify_id, then you can use that plus insertMany with ordered = false to keep inserting even if a dupe is found. You'll want to double check the errors of course:

db.foo.drop();
db.foo.createIndex({"spotify_id":1}, {unique:true});

function loadEm(r) {
    try {
        rc = db.foo.insertMany(r, {ordered:false});
    } catch(e) {
    // Check for expected dupe errors:                                          
    e.getWriteErrors().forEach(function(err) {
        if(err.code != 11000) {
                    // real error!                                                  
                    throw e;
                }
            });
    }
}

loadEm(
[
 {spotify_id: "A", name: "foo"},
 {spotify_id: "B", name: "bar"}
]
       );


loadEm(
[
 {spotify_id: "A", name: "foo"},
 {spotify_id: "B", name: "bar"},
 {spotify_id: "C", name: "corn"},
 {spotify_id: "C", name: "dog"},
 {spotify_id: "D", name: "zap"}
]
       );
Buzz Moschetti
  • 7,057
  • 3
  • 23
  • 33