Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

117
Visualizações
Update a nested Array in Mongo with c#

I have a document like this

{
"_id": "63dafa72f21d48312d8ca405",
"tasks": [{
    "_ref": "63d8d8d01beb0b606314e322",
    "data": {
        "values": [{
            "key": "Deadline",
            "value": "2014-10-13"
        }]
    }
}, {
    "_ref": "84dd046c6695e32322d842f5",
    "data": {
        "values": []
    }
}]
}

Now I want to update the value inside values which is inside data if the _ref field do match my input.

My code so far:

public bool updateProject(Project dbPro, Project pro)
{
    var collection = db.GetCollection<BsonDocument>("projects");
    var filter = Builders<BsonDocument>.Filter.Eq("_id", ObjectId.Parse( dbPro.Id));
    var update = Builders<BsonDocument>.Update.AddToSetEach("tasks", pro.Tasks);
    var result = collection.UpdateOne(filter, update);
    if (result.IsModifiedCountAvailable)
    {
        if (result.ModifiedCount == 1)
        {
            return true;
         }
    }
    return false;
}

At the moment this code does only append the documents as new tasks instead to append the values to the matching tasks. Maybe someone has an idea how to achieve this behavior?

UPDATE

I tried it like @Shane Oborn said. But its still not working for me.

            var collection = db.GetCollection<BsonDocument>("projects");
        var filter = Builders<BsonDocument>.Filter.Eq("_id", ObjectId.Parse( dbPro.Id));
        var update = Builders<BsonDocument>.Update.Push("tags", buildBsonArrayFromTags(pro.Tags));
        var result = collection.UpdateOne(filter, update);
        if (result.IsModifiedCountAvailable)
        {
            if (result.ModifiedCount == 1)
            {
                return true;
            }

        }


        return false;
    }

Instead to override the data it appends an array to my array.

enter image description here

UPDATE

OK instead of push i did need set. And it worked then.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I don't have the exact code accessible, but close. I have a method that performs "upserts" (which "adds" if new, or "updates" if existing). This should get you close:

// The variable "doc" below is a BsonDocument

var updateRequests = new List<WriteModel<BsonDocument>>();
updateRequests.Add(new ReplaceOneModel<BsonDocument>(
    CreateBsonDocumentFilterDefinition(filterKeyName, filterKeyValue), doc)
    {
        IsUpsert = true
    });
var writeResult = await collection.BulkWriteAsync(updateRequests);

The key objects here for you are "ReplaceOneModel" and the "IsUpsert" property for the filter definition.

Good luck!

UPDATE:

Another method I have that does updates in subdocuments looks like this:

// Below, "subDocument" is a BsonDocument, and "subDocArrayName" is a string
// that should match the name of the array that contains your sub-document
// that will be updated.

var collection = _database.GetCollection<BsonDocument>(collectionName);

var builder = Builders<BsonDocument>.Update;
var update = builder.Push(subDocArrayName, subDocument);

await collection.UpdateOneAsync(CreateBsonDocumentFilterDefinition(filterKeyName, filterKeyValue), update);
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda