我到处搜索,没有解决方案。这是我插入数据的方式:
string value = db.StringGet("test");
string cleaned = value.Replace("u'", "'").Replace("\"", "");
var jsonDoc = Newtonsoft.Json.JsonConvert.SerializeObject(cleaned);
Dictionary<string, string> dict = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonDoc);
values.Add(dict);
_collection.InsertMany(values.Select(x => x.ToBsonDocument()));
这是数据在数据库中的外观
{
"_id" : ObjectId("5aaabf7ac03af44892673031"),
"_t" : "MongoDB.Bson.BsonDocument, MongoDB.Bson",
"_v" : {
"profile" : "myprofile",
"source" : "thesource",
"hostname" : "myhost",
"pgm" : "mypgm"
}
}
我不希望数据在mongo中这样格式化。我的原因是因为我有几个客户端正在访问数据库。我宁愿这样格式化数据:
{
"_id" : ObjectId("5aaabf7ac03af44892673031"),
"profile" : "myprofile",
"source" : "thesource",
"hostname" : "myhost",
"pgm" : "mypgm"
}
最佳答案
在我们的团队中,我们通过序列化和反序列化对象(使其成为ExpandoObject)解决了该问题。尝试这样的事情:
JsonSerializer DynamicDataJsonSerializer;
DynamicDataJsonSerializer = JsonSerializer.Create(JsonConverterSetup.GetTransparentSerializerSettings());
MyClass dataToSaveToMogo;
var dataToSaveToMogoAsDynamic = DynamicDataJsonSerializer.Deserialize<ExpandoObject>(DynamicDataJsonSerializer.Serialize(dataToSaveToMogo));
然后保存dataToSaveToMogoAsDynamic。
希望能帮助到你
关于c# - 如何从mongo文档中删除_v和_t,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49307482/