Home > Software engineering >  Mongodb $set Insert is not working correctly
Mongodb $set Insert is not working correctly

Time:03-21

I have the following collection of pets:

{ "_id" : ObjectId("62372a7f30847f1025d9eaaf"), "id" : 1, "name" : "Mikey", "species" : "Gerbil" }
{ "_id" : ObjectId("62372a7f30847f1025d9eab0"), "id" : 2, "name" : "Davey Bungooligan", "species" : "Piranha" }
{ "_id" : ObjectId("62372a7f30847f1025d9eab2"), "id" : 4, "name" : "Mikey", "species" : "Hotdog" }
{ "_id" : ObjectId("62372a7f30847f1025d9eab3"), "id" : 5, "name" : "Terrence", "species" : "Sausagedog" }
{ "_id" : ObjectId("62372c9430847f1025d9eab6"), "id" : 8, "name" : "Gabriel", "species" : "Naked mole rat" }
{ "_id" : ObjectId("6237375930847f1025d9eab9"), "id" : 7, "name" : "James", "species" : "Piranha" }
{ "_id" : ObjectId("6237394e30847f1025d9eabe"), "id" : 3, "name" : "Suzy B", "species" : "Cat" }
{ "_id" : ObjectId("6237394e30847f1025d9eabf"), "id" : 6, "name" : "Philomena Jones", "species" : "Cat" }

I want to insert a list of favourite food for one of the cats and have tried the following:

db.pets.update({name:”Suzy B”},{$set{favorites:{ food:[”Fish Soup”,”Whiskey Fish”]}}})

but I get the following error:

uncaught exception: SyntaxError: illegal character :
@(shell):1:21

what am I doing wrong?

CodePudding user response:

you forgot a : after the $set

and it should be double quote " not

Here is the good request

{
  name: "Suzy B"
},
{
  $set: {
    favorites: {
      food: [
        "Fish Soup",
        "Whiskey Fish"
      ]
    }
  }
}
  • Related