I am not able to add

I am not able to add or insert data into databases via inserting documents into collections. I have a list of methods that I am trying to insert data through and none of it is coming. Please let me know what is going on and how I can add documents to collections by calling and using the mongo method.

Thanks.

Hello @Quiet_Services and welcome to the MongoDB Community forums. :wave:

Can you tell us what errors you are getting when you try to run any of the insert statements from the screenshot you’ve posted (looks to be part of one of the University courses).

Knowing what error you’re getting will help us better know what’s going on so we can point you in the right direction.

Hi Doug, when I enter command line prompts or statements like the following:

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

or

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

I get the following errors:

MongoDB Enterprise atlas-o2a0p3-shard-0:PRIMARY> db.pets.insert({"pet":"cat"},{ "pet":"dog"}, {"pet":"fish"})
WriteCommandError({
        "ok" : 0,
        "errmsg" : "(Unauthorized) not authorized on admin to execute command { insert: \"pets\", ordered: true, lsid: { id: {4 [240 184 37 69 164 234 79 180 171 92 104 163 106 217 101 174]} }, $clusterTime: { clusterTime: {1661671493 2}, signature: { hash: {0 [37 55 125 33 194 184 27 154 103 223 56 230 69 119 135 230 97 114 182 242]}, keyId: 7094555532198936576.000000 } }, $db: \"admin\" }",
        "code" : 8000,
        "codeName" : "AtlasError"
})

MongoDB Enterprise atlas-o2a0p3-shard-0:PRIMARY> db.pets.insert([{"pet":"cat"},{ "pet":"dog"}, {"pet":"fish"}])
WriteCommandError({
        "ok" : 0,
        "errmsg" : "(Unauthorized) not authorized on admin to execute command { insert: \"pets\", ordered: true, lsid: { id: {4 [240 184 37 69 164 234 79 180 171 92 104 163 106 217 101 174]} }, $clusterTime: { clusterTime: {1661748125 1}, signature: { hash: {0 [242 110 186 233 136 125 162 12 27 127 145 245 195 46 132 135 2 137 49 87]}, keyId: 7094555532198936576.000000 } }, $db: \"admin\" }",
        "code" : 8000,
        "codeName" : "AtlasError"
})

You can see the errors in the attached files in this message.

Please let me know what you can do. Thanks a lot.

QUIET Services

Are you connected to correct db?
You can check by command db
If not in correct db you can switch to correct db by use db
Do you see the collection you are querying in the current connected session where you are getting the error

yes it is all connected. I am able to finally insert items in there but the Question asks me which command will insert 3 connections successfully -

Problem:

Which of the following commands will successfully insert 3 new documents into an empty pets collection?
Attempts Remaining:2 Attempts left

Check all answers that apply:

I have all the possible answers uploaded. NONE of them work successfully when I try them out on the Mongo Command prompt. Only the first ID works. {"_id":1, “pet”:“cat”}

The error message you posted shows:

"errmsg" : "(Unauthorized) not authorized on admin to execute command { insert: \"pets\", ordered: true, lsid: { id: {4 [240 184 37 69 164 234 79 180 171 92 104 163 106 217 101 174]} }, $clusterTime: { clusterTime: {1661671493 2}, signature: { hash: {0 [37 55 125 33 194 184 27 154 103 223 56 230 69 119 135 230 97 114 182 242]}, keyId: 7094555532198936576.000000 } }, $db: \"admin\" }"

This means you’re in the admin database and trying to perform an insert(). This matches up with Ramamchandra’s comments:

You will want to change to the database you created for the course which by default is myFirstDatabase if I remember correctly.

1 Like