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

180
Visualizações
Update operation in mongoDb and asp.net core

I am new in mongoDb and try to do an CRUD operation using mongoDb and Asp.net Core web api. My problem is altime whole object becomes update. I want to update specific fields that I send in web api.

Example :

   BusinessUnit oBU = new BusinessUnit(){
         Id = "586e262268d90b290001b46e",
         Name = "BU_Name",
         Address = "my_Add"
   };

Now I want to update only address to "my_New_add" and want to make below object :

    BusinessUnit oBU = new BusinessUnit(){
         Id = "586e262268d90b290001b46e",
         Name = "BU_Name",
         Address = "my_New_Add"
   };

API Call : http://localhost:88786/api/BusinessUnit/586e262268d90b290001b46e

body : {"Id" : "586e262268d90b290001b46e", "Address" : "my_New_add"}

But all time it updates full object. How can I solve it?

Below is my code :

Controller Code :

    [HttpPut("{id}")]
    public void Put(string id, [FromBody]BusinessUnit businessUnit)
    {
        _businessUnitRepository.UpdateBusinessUnit(id, businessUnit);
    }

Repository Code :

    public async Task<ReplaceOneResult> UpdateBusinessUnit(string id, BusinessUnit businessUnit)
    {
        return await _context.BusinessUnits.ReplaceOneAsync(doc => doc.Id == id, businessUnit);
    }

Actually I want to do below query (I know mongodb is noSql, but for making my question clear I write below sql query)

UPDATE BusinessUnit Set Address = "my_New_add" WHERE Id = "586e262268d90b290001b46e"

Thanks in advance.

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

0

FindOneAndReplace

A single document can be replaced atomically using the FindOneAndReplace or FindOneAndReplaceAsync methods.

var filter = new BsonDocument("FirstName", "Jack");
var update = Builders<BsonDocument>.Update.Set("FirstName", "John");

var result = collection.FindOneAndUpdate(filter, update);

if (result != null)
{
  Assert(result["FirstName"] == "Jack");
}

The above will find a document where the FirstName is Jack and set its FirstName field to John. It will then return the document that was replaced.

Please see the documentation here

over 4 years ago · Santiago Trujillo Relatório

0

Use this method :

 public ToDoItem Update(ToDoItem toDoItem)
 {
        yourCollection.ReplaceOne(item => item.Id == toDoItem.Id, toDoItem);
        return toDoItem;
 }
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