Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

510
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda