Inserting New Documents - insert() order

For Ch3 Lecture “Inserting New Documents - insert() order”. Im a bit confused on the explanation at timestamp 2m00sec.

This was where the following was added, after 6 records had already been added

db.inspections.insert([{ “_id”: 1, “test”: 1 },{ “_id”: 1, “test”: 2 },{ “_id”: 3, “test”: 3 }])

So after that command, it was the {"_id": 1, "test": 1} document that was added.
There was no good explanation given as to why the 3rd document that had no collission was NOT added, nor why the 1st document was added in preference over the 2nd.

check back the lecture video but this time keep attention to what she says about the default “order” behavior.

and also think about (again she says) what makes a document “unique”. that uniqueness also causes those errors seen in the video.

If you are not already an advanced coder on some programming language and/or some database, give yourself time to digest the topics. Things will get better with time. You will need to repeat things many times. So try to have as much fun the first time you see it and try it yourself later :wink:

1 Like

In addition to the video lesson, and the answer by @Yilmaz_Durmaz, I would recommend reading the documentation on the insertMany() method.

The documentation covers this situation rather well and explains why the first document is inserted and why the other two are not inserted.

I would also read up on the ordered option from that documentation as well and how setting that key to either true or false changes how the insertMany() method works when duplicate key errors occur.

3 Likes