Update Mongodb in array

3
{
    "_id" : "55dcb404478e7227203d3a65",
    "Nome" : "Grupo Familia",
    "Pessoas" : [ 
        {
            "PessoaId" : "55dcb425478e72207833e970",
            "Nome" : "Carlos",
            "Habilidades" : [
                {
                    "HabilidadeId" : "55dcb433478e7229b0e3ee07",
                    "Valor" : 20,
                    "Nome": "José"
                },
                {
                    "HabilidadeId" : "55dcb425478e72207833e961",
                    "Valor" : 40,
                    "Nome" : "Vitor"
                }
            ],
        }
    ],
}

Using mongocsharpdriver , how do I give update of type modify (without using save) in Array of Skills? I've got a way to do it however I need the index of the array , but I do not know how to find it. In the example below I passed the index to 0, then it takes the first person and adds the new skill:

var novaHabilidade = new Habilidade { };
var update = Update<Grupo>.AddToSet(a => a.Pessoas[0].Habilidades, novaHabilidade);
context.Grupos.Update(Query.EQ("_id", "55dcacb7478e722a60e7c002"), update);

I've tried in a different way, trying to filter the person I want:

var update = Update<Grupo>.AddToSet(a => a.Pessoas.Find(b => b.PessoaId == "55dcb425478e72207833e970").Habilidades, novaHabilidade);

I was not successful, giving the error "Object reference not instantiated". Does anyone know a way to do it? I wanted to use the objects I created, nothing to create BsonDocument .

This is the error that is giving "Unable to determine the serialization information for the expression"

    
asked by anonymous 25.08.2015 / 21:14

1 answer

1

I found out how to do it, if anyone has an interest:

var update = Update.AddToSet("Pessoas.$.Habilidades", novaHabilidade.ToBsonDocument());
var retorno = context.Grupos.Update(Query.EQ("Pessoas.PessoaId", new ObjectId("55de157b478e72231cfeca69")), update);
    
25.08.2015 / 21:32