Chapter 3 insert order quiz answer incorrect only 1, 4 correct

db.pets.insert([ { “_id”: 1, “pet”: “cat”},
… { “_id”: 1, “pet”: “dog”},
… { “_id”: 3, “pet”: “fish”},
… { “_id”: 4, “pet”: “snake” }], {“ordered”: false })
BulkWriteResult({
“writeErrors” : [
{
“index” : 0,
“code” : 11000,
“errmsg” : “E11000 duplicate key error collection: pet.pets index: id dup key: { _id: 1.0 }”,
“op” : {
“_id” : 1,
“pet” : “cat”
}
},
{
“index” : 1,
“code” : 11000,
“errmsg” : “E11000 duplicate key error collection: pet.pets index: id dup key: { _id: 1.0 }”,
“op” : {
“_id” : 1,
“pet” : “dog”
}
},
{
“index” : 2,
“code” : 11000,
“errmsg” : “E11000 duplicate key error collection: pet.pets index: id dup key: { _id: 3.0 }”,
“op” : {
“_id” : 3,
“pet” : “fish”
}
}
],
“writeConcernErrors” : ,
“nInserted” : 1,
“nUpserted” : 0,
“nMatched” : 0,
“nModified” : 0,
“nRemoved” : 0,
“upserted” :

It looks like you did not empty you collection between each of your trials to satisfy the part of the question that say: into an empty pets collection.

Because if the collection is empty there is no way to get E11000 twice for _id:1.

1 Like

Hi @Raj_Neerohoo ,

Let us know if you are still facing any issues.

Hi, I just took this quiz and to me it does seem to me that only choices 1 and 4 are correct. Here is the quiz for reference.

The question posed is:

Which of the following commands will successfully insert 3 new documents into an empty pets collection?

Here is the 2nd option to choose from, which I believe is incorrect:

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

The detailed answer states that this option is correct for the following reason:

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.

This statement appears to be false, as there are only 2 documents with unique _id values, the fish and the snake.

From what I understand only the “fish” and “snake” documents would be inserted, as they are the only documents with unique _id values. If I am mistaken please help me understand

What make you think that none of the _id:1 get inserted?

The collection is empty, so

{ "_id": 1, "pet": "cat" }

will be inserted and the other _id:1 will fail but since ordered:false then the rest is processed.

That’s what is nice with MongoDB. You can try stuff easily. With SQL you would need to create your table first. Here you just fire the shell and try

> use test
switched to db test
> db.pets.find()
> db.pets.insert([{ "_id": 1, "pet": "cat" },
...             { "_id": 1, "pet": "dog" },
...             { "_id": 3, "pet": "fish" },
...             { "_id": 4, "pet": "snake" }], { "ordered": false })
BulkWriteResult({
	"writeErrors" : [
		{
			"index" : 1,
			"code" : 11000,
			"errmsg" : "E11000 duplicate key error collection: test.pets index: _id_ dup key: { : 1.0 }",
			"op" : {
				"_id" : 1,
				"pet" : "dog"
			}
		}
	],
	"writeConcernErrors" : [ ],
	"nInserted" : 3,
	"nUpserted" : 0,
	"nMatched" : 0,
	"nModified" : 0,
	"nRemoved" : 0,
	"upserted" : [ ]
})
> db.pets.find()
{ "_id" : 1, "pet" : "cat" }
{ "_id" : 3, "pet" : "fish" }
{ "_id" : 4, "pet" : "snake" }

This is confusing when comparing your results with the lesson before the quiz - Inserting New Documents - insert() order.

The instructor explains the ordered false option at the 2:38 mark of the video. She specifically states:

All documents with unique _id values will be inserted, and all the documents that have duplicate underscore values will produce their own error.

Which conflicts with the example you have just provided.

In the example shown in the video the results show {“nInserted” : 1}. From what your saying, her results should have been {“nInserted” : 2}.

What am I understanding wrong? How are you and the instructor providing different results?

At the time the first _id:1 is inserted the database is empty and so it is not a duplicate. When the second one is about to be inserted, there is a document with the _id in the database so the new one is a duplicate and not inserted.