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

396
Vistas
How to prevent _t and _v when inserting into MongoDB?

I'm utilizing Dictionary. After .insert() there are "_t" and "_v". Two posts here talked about serialization converting to JSON first then BSON. I'm using MongoDB's driver v2.4.3,

mCollection.InsertOne(x);
IMongoCollection<myDoc> mCollection = Db.GetCollection<myDoc>("whatever");

If I do JSON-to-BSON, it complains about can't convert BsonDocument to myDoc. Switching to IMongoCollection<BsonDocument> mCollection = Db.GetCollection<BsonDocument>("whatever"); still get _t and _v.

How to avoid _t and _v?

enter image description here

Here is my code of data type and utilization:

public class myObjForDictionary
    {
        //...
    }
    public class myDoc
    {
        // ... some other elements, then Dictionary
        public Dictionary<string, object> myDictionary { get; set; }
    }

    // to instantiate the
    class myClass
    {
        // define MongoDB connection, etc. 
        // instantiate myDoc and populate data
        var x = new myDoc
        {
            //...
            myDictionary = new Dictionary<string, object>
            {
                { "type", "something" },
                { "Vendor", new object[0] },
                { "obj1", //data for myObjForDictionary
                }
            };
        }

    }
about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

I think you're looking for the DictionarySerializationOption... which gives you a couple of different options out of the box to determine how your dictionary gets serialized.

about 4 years ago · Santiago Trujillo Denunciar

0

You'll need to save some information (_t) of what object the deserializer needs to create when deserializing the object, if not it won't know what to create from the BSON.

Alternatively, you could change the Dictionary<string, object> to Dictionary<string, BsonDocument> and deal with the BsonDocuments directly within your code. This will be very similar to how you use JObject within Newtonsoft.Json.

about 4 years ago · Santiago Trujillo Denunciar

0

It also happens when the passed model to mongo is not exactly the type that is defined on the model.

In this case, mongo will add _t as it identifies that you expect to read the inheritor's class type, and not the type that is defined on the model.

For example:

public class ParentClass { }

public class ChildClass : ParentClass { }

public class ModelToInsert 
{
   public ParentClass property { get; set; }
}

...
// ChildClass is passed instead of ParentClass 
collection.InsertOne(new ModelToInsert { property = new ChildClass() });
about 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