Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent _t and _v when inserting into MongoDB?

Tags:

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                 }             };         }      } 
like image 260
Jeb50 Avatar asked Mar 22 '17 05:03

Jeb50


1 Answers

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.

like image 51
James Crosswell Avatar answered Nov 13 '22 07:11

James Crosswell