gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 13:06:28 26 4
gpt4 key购买 nike

我正在使用字典。 .insert() 之后有“_t”和“_v”。这里有两个帖子谈到serialization转换为 JSON first then BSON .我正在使用 MongoDB 的驱动程序 v2.4.3,

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

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

如何避免_t和_v?

enter image description here

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

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

}

最佳答案

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

关于c# - 插入MongoDB时如何防止_t和_v?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42943709/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com