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

517
Visualizações
How to save/pass MongoDB UpdateDefinition for logging and later use?

I am stumped on how to save/pass MongoDB UpdateDefinition for logging and later use

I have created general functions for MongoDB in Azure use on a collection for get, insert, delete, update that work well.

The purpose is to be able to have a standard, pre-configured way to interact with the collection. For update especially, the goal is to be able to flexibly pass in an appropriate UpdateDefinition where that business logic is done elsewhere and passed in.

I can create/update/set/combine the UpdateDefinition itself, but when i try to log it by serializing it, it shows null:

JsonConvert.SerializeObject(updateDef)

When I try to log it, save it to another a class or pass it to another function it displays null:

public class Account
    {
        [BsonElement("AccountId")]
        public int    AccountId     { get; set; }
        [BsonElement("Email")]
        public string Email         { get; set; }
    }

var updateBuilder = Builders<Account>.Update;
var updates = new List<UpdateDefinition<Account>>();

//just using one update here for brevity - purpose is there could be 1:many depending on fields updated
updates.Add(updateBuilder.Set(a => a.Email, email));

//Once all the logic and field update determinations are made
var updateDef = updateBuilder.Combine(updates);

//The updateDef does not serialize to string, it displays null when logging.
_logger.LogInformation("{0} - Update Definition: {1}", actionName, JsonConvert.SerializeObject(updateDef));

//Class Created for passing the Account Update Information for Use by update function
public class AccountUpdateInfo
    {
        [BsonElement("AccountId")]
        public int                          AccountId   { get; set; }
        [BsonElement("Update")]
        public UpdateDefinition<Account>    UpdateDef   { get; set; }
    }

var acct = new AccountUpdateInfo();
acctInfo.UpdateDef = updateDef

//This also logs a null value for the Update Definition field when the class is serialized.
_logger.LogInformation("{0} - AccountUpdateInfo: {1}", actionName, JsonConvert.SerializeObject(acct));

Any thoughts or ideas on what is happening? I am stumped on why I cannot serialize for logging or pass the value in a class around like I would expect

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

0

give this a try:

var json = updateDef.Render(
               BsonSerializer.SerializerRegistry.GetSerializer<Account>(),
               BsonSerializer.SerializerRegistry)
           .AsBsonDocument
           .ToString();

and to turn a json string back to an update definition (using implicit operator), you can do:

UpdateDefinition<Account> updateDef = json;

this is off the top of my head and untested. the only thing i'm unsure of (without an IDE) is the .Document.ToString() part above.

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