插入MongoDB时如何防止_t和_v?

Jeb*_*b50 20 c# mongodb

我正在使用词典.在.insert()之后有"_t"和"_v".这里有两篇文章谈到序列化转换为JSON然后BSON.我正在使用MongoDB的驱动程序v2.4.3,

mCollection.InsertOne(x);
IMongoCollection<myDoc> mCollection = Db.GetCollection<myDoc>("whatever");
Run Code Online (Sandbox Code Playgroud)

如果我做JSON-to-BSON,它抱怨无法将BsonDocument转换为myDoc.切换到IMongoCollection<BsonDocument> mCollection = Db.GetCollection<BsonDocument>("whatever");仍然得到_t和_v.

如何避免_t和_v?

在此输入图像描述

这是我的数据类型和利用率代码:

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
                }
            };
        }

    }
Run Code Online (Sandbox Code Playgroud)

Jam*_*ell 1

我认为您正在寻找 DictionarySerializationOption ...它为您提供了几个开箱即用的不同选项来确定您的字典如何序列化。