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.

Hi Kiran,
You can see in db.pets.insert([{ “_id”: 1, “pet”: “cat” },
{ “_id”: 2, “pet”: “dog” },
{ “_id”: 3, “pet”: “fish” },
{ “_id”: 3, “pet”: “snake” }]) this document, it will take internally ordered:true, now it inserted first 3 and then at 4th element got error hence that will not get inserted and you will able to see error.But as question says 3 documents needs to be inserted.They already right?

Hello Kiran_Kumar_Kona,
I hope this clarifies your doubt.

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

// In this option, as it considers order = true hence the insertion by default would be done in a manner they are mentioned. So, when control moves to 3rd document “pet” : “fish” it gets inserted as no document yet has “_id” : 3 but when control moves to next document “_id” : 3 already exists and it will throw an error.
Therefore 1st 3 documents get inserted while the 4th one throws id error.

Thank you @UTKARSH_TRIVEDI @Pradnya_Kulkarni & @Uwe_Scheffer for your response.

We hope that @Kiran_Kumar_Kona is not stuck in this issue as it’s 3 months older post.
So, we are closing this thread as of now.

For any other queries/issues feel free to create a new post in the forum under the respective course categories of MongoDB University. We will be happy to help.

Regards,
Kushagra