Quiz: Insert Order

The answer explanation states:

db.pets.insert([{ "_id": 1, "pet": "cat" },
                { "_id": 1, "pet": "dog" },
                { "_id": 3, "pet": "fish" },
                { "_id": 4, "pet": "snake" }], { "ordered": false })

This is correct.

This insert is unordered, which means that each document with a unique _id value will get inserted into the collection, which would make a total of 3 inserted documents."

However, in the video, only one document got inserted because the first two had key errors. Similarly, shouldn’t only 2 documents get read in this instance? The key errors would be the ones with the similar _id and the other _id is unique.

When the first _id:1 is about to be inserted the collection is empty has indicated in the question. The collection being empty does not have a document with _id:1 so it is inserted.

When the second _id:1 is about to be inserted the collection already has a document with _id:1, the one we just inserted, so it generates a duplicate key error.

When _id:3 is about to be inserted, the collection only contains one document and its _id is 1 so it is _id:3 is inserted because it is not a duplicate.

1 Like

@Amaresh_Chittigidde, if you are satisfied with my explications please mark my post as the solution. Otherwise followup with more question. Thanks for keeping this forum efficient.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.