insertMany with duplicates

The video shows that insertMany (ordered) stops at (after) the first error.

However if we have followed the previous video, we have already inserted the first _id we are trying to insert (ST II imdb id), thus it would fail at the first document (index 0).

2019-01-13T19:21:24.707+0100 E QUERY [js] BulkWriteError: write error at item 0 in bulk operation :
“writeErrors” : [
“index” : 0,
“code” : 11000,
“errmsg” : “E11000 duplicate key error collection: video.moviesScratch index: id dup key: { : “tt0084726” }”,
“op” : {
“_id” : “tt0084726”,
“title” : “Star Trek II: The Wrath of Khan”,
“year” : 1982,
“type” : “movie”
“writeConcernErrors” : ,
“nInserted” : 0,
“nUpserted” : 0,
“nMatched” : 0,
“nModified” : 0,
“nRemoved” : 0,
“upserted” :
MongoDB Enterprise Sandbox-shard-0:PRIMARY>

What I did was deleted everything in that and then the lesson instructions worked as depicted.
Hope this helps.

1 Like

Indeed … but deleting is the LAST item of the chapter :wink:
I’m not sure if at the end of the previous video something was mentioned about deleting and/or adding that document for some reason … I have the feeling some lessons have been reordered/modified?

In principle you are correct. For this exercise since I had created a new ‘collection’ so deleting all data was not a an issue. But I see your point.