如何从 mongo 文档中删除 _v 和 _t [英] How to remove _v and _t from mongo document

查看:169
本文介绍了如何从 mongo 文档中删除 _v 和 _t的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找遍了,没有任何解决方案.这是我插入数据的方式:

I have searched all over and no solution works. Here is how I am inserting data:

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()));

这是数据在数据库中的样子

Here is how the data looks in the database

{ 
    "_id" : ObjectId("5aaabf7ac03af44892673031"), 
    "_t" : "MongoDB.Bson.BsonDocument, MongoDB.Bson", 
    "_v" : {
        "profile" : "myprofile", 
        "source" : "thesource", 
        "hostname" : "myhost", 
        "pgm" : "mypgm"
    }
}

我不想在 mongo 中将数据格式化成这样.我的原因是因为我有几个客户端访问数据库.我宁愿将数据格式化如下:

I dont want the data formatted like this in mongo. The reason my is because I have several clients accessing the db. I would rather have the data formatted like this:

{ 
    "_id" : ObjectId("5aaabf7ac03af44892673031"), 
    "profile" : "myprofile", 
    "source" : "thesource", 
    "hostname" : "myhost", 
    "pgm" : "mypgm"
}

推荐答案

在我们的团队中,我们通过序列化和反序列化对象来解决这个问题,使其成为 ExpandoObject.尝试这样的事情:

In our team we solved it by serializing and deserializing the object, making it an ExpandoObject. Try something like this:

    JsonSerializer DynamicDataJsonSerializer;
    DynamicDataJsonSerializer = JsonSerializer.Create(JsonConverterSetup.GetTransparentSerializerSettings());

    MyClass dataToSaveToMogo;
    var dataToSaveToMogoAsDynamic = DynamicDataJsonSerializer.Deserialize<ExpandoObject>(DynamicDataJsonSerializer.Serialize(dataToSaveToMogo));

然后保存 dataToSaveToMogoAsDynamic.

then save dataToSaveToMogoAsDynamic.

希望能帮到你

这篇关于如何从 mongo 文档中删除 _v 和 _t的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆