Chapter 3: Creating and Manipulating Documents

I have selected 2 options but it still showing wrong answer.

only 2 options are correct this question but in details answer it is showing 1 more option,due to which i have graded with incorrect answer.

Problem:

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

I have selected these 2 options but it is marked as wrong.
db.pets.insert([{ “pet”: “cat” }, { “pet”: “dog” },
{ “pet”: “fish” }])

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

In key 3 options are correct.

db.pets.insert([{ “pet”: “cat” }, { “pet”: “dog” },
{ “pet”: “fish” }])

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

db.pets.insert([{ “_id”: 1, “pet”: “cat” },
{ “_id”: 2, “pet”: “dog” },
{ “_id”: 3, “pet”: “fish” },
{ “_id”: 3, “pet”: “snake” }])

Please advice me.

If I understand correctly, you think that

does not insert 3 new documents into an empty pets collection.

Why do you think so?

Have you tried it?

Yes, it is not inserting …but in assignment it is showing right answer.

You must be doing something wrong. This command does insert 3 new documents into an empty collection.

Hi Kiran,

I tried your example out in my local Mongo Shell, using copy-and-paste, and it did not work, got syntax error with the 18th character. That made me suspicious of your “double quotes” not being pure ASCII.

In Vim I copied over your code from here and replaced your “double quotes” with pure ASCII ones.

Copied the result back into my Mongo Shell and, voila, it worked, 3 documents inserted (cat , dog, fish).

Here is my code in case you want to try on your own:

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

Maybe I could help a little. Regards, M.