Update in array in Mongodb

Asked

Viewed 2,228 times

4

{
    "_id" : "55dcb404478e7227203d3a65",
    "Nome" : "Grupo Familia",
    "Pessoas" : [ 
        {
            "PessoaId" : "55dcb425478e72207833e970",
            "Nome" : "Carlos",
            "Habilidades" : [
                {
                    "HabilidadeId" : "55dcb433478e7229b0e3ee07",
                    "Nome": "Pular",
                    "Macetes": [
                            {"Descricao" : "Usar um tênis macio"},
                            {"Descricao" : "Se alongar"}
                    ]
                },
                {
                    "HabilidadeId" : "55dcb425478e72207833e961",
                    "Nome" : "Correr"
                }
            ],
        }
    ],
}

I have this Collection on Mongodb and I have the following question: How do I add a new skill trick "correr" ?

2 answers

1


You must use the $addToSet or the $push. The difference is whether you want unique elements in the array or not.

There is a positional operator $ that you can use to update elements in the index that matches your query. So, if you "know" that there is a skill but don’t know its position, you should do the following query:

db.grupos.update({ _id: "...", "Pessoas.Habilidades.Nome": "Correr" }, {
  "$addToSet": {
    "Pessoas.$.Habilidades.$.Macetes": { /*...*/ }  
  }
})

If you want to remove elements, you need to use $pull and to add/remove more than one element you will need to use $each.

Useful links:

I suggest an index in Pessoas.Habilidades.Nome.

EDIT

I just researched a little more and what you want to do is not possible:

Of documentation:

The $ positional operator cannot be used for searches that traverse more than one array, as the searches that traverse arrays "nested" in other arrays, because the substitution by the $ placeholder is a single value

It is necessary to change the way the data is modeled. Or lose the operation atomicity.

0

You can use $set. Example:

db.bla.update({"_id":"55dcb404478e7227203d3a65"},
{
    "$set": {
        "Pessoas.0.Habilidades.1.Macetes": [
            {
                "HabilidadeId": "idnovo",
                "Nome": "Pular"
            }
        ]
    }
})
  • Answer my question, but in my real case, I do not know what index the person has and nor the skill index, can be the top person of Collection and the twentieth skill, How would you insert a headline in this case ? ( without entering the Dice number directly)

  • Do you know Person and Ability? If you know, use them in the query instead of "_id". When manipulating the array, you could use $push. Has several operations specific to handle array, check here: http://docs.mongodb.org/manual/reference/operator/update-array/

  • What is the syntax for using these id’s? Suppose Personal = "teste1" and Ability = "teste2", as I would do this query ?

  • It would be like that: db.bla.update({"Pessoas.Habilidades.HabilidadeId" : "55dcb425478e72207833e961"}, looking directly for the HabilidadeId.

  • I did that too and in $set I used this : "$set": { "People.$.Habilitys.$. Tricks": [ { "Ability": "new", "Name": "Skip" } ] } However it does not work

  • This question may also help: https://stackoverflow.com/questions/23821392/how-to-update-valuof-a-key-in-a-list-of-a-json-in-mongo

  • That way I could get up to skills, but not up to tricks ..

  • Discussion is getting too long, let’s take pro chat: https://chat.stackexchange.com/rooms/11910/stackoverflow

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.