简体   繁体   English

如何从 mongo 文档中删除 _v 和 _t

[英]How to remove _v and _t from mongo document

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

I dont want the data formatted like this in mongo.我不希望在 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"
}

In our team we solved it by serializing and deserializing the object, making it an ExpandoObject.在我们的团队中,我们通过序列化和反序列化对象来解决它,使其成为 ExpandoObject。 Try something like this:尝试这样的事情:

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

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

then save dataToSaveToMogoAsDynamic.然后保存 dataToSaveToMogoAsDynamic。

Hope it helps希望能帮助到你

You will need to call the Remove property of the BsonDocument class and specify "_t" as parameter to be able to remove the "_t" key.您需要调用 BsonDocument 类的 Remove 属性并指定“_t”作为参数才能删除“_t”键。 Same goes with "_v" “_v”也是如此

        MyClass item = new MyClass();
        var returnDocument = new BsonDocument(item.ToBsonDocument());
        returnDocument.Remove("_t");
        collection.InsertOne(returnDocument);

This solution works 100%.该解决方案 100% 有效。

                var bsonDocument = new BsonDocument(userActivityDoc.ToBsonDocument());
                if (bsonDocument.TryGetValue("AdditionalData", out BsonValue additionalData))
                {
                    additionalData.ToBsonDocument().Remove("_t");
                }
                
                await _genericRepository.AddOneAsync(BsonSerializer.Deserialize<UserActivityDoc>(bsonDocument));

the finally solution for remove "_t" from all nested values:从所有嵌套值中删除“_t”的最终解决方案:

 if (bsonDocument.TryGetValue("AdditionalData", out var additionalData) && !additionalData.IsBsonNull)
                    {
                        RemoveAllTObject(additionalData.ToBsonDocument());
                        //additionalData.ToBsonDocument().Remove("_t");
                    }

private void RemoveAllTObject(BsonDocument bsonDocument)
        {
            bsonDocument.Remove("_t");
            foreach (var bsonElement in bsonDocument)
            {
                if (!bsonElement.Value.IsBsonNull)
                {
                    if (bsonElement.Value.GetType() == typeof(BsonArray))
                    {
                        foreach (var bsonValue in bsonElement.Value.AsBsonArray)
                        {
                            if (bsonValue.GetType() == typeof(BsonDocument))
                                RemoveAllTObject(bsonValue.AsBsonDocument);
                        }
                    }

                    if (bsonElement.Value.GetType() == typeof(BsonDocument))
                    {
                        var bDocument = bsonElement.Value.AsBsonDocument;
                        if (bsonDocument.IsBsonNull)
                            continue;
                        if (bDocument.ElementCount > 0)
                        {
                            RemoveAllTObject(bDocument);
                        }
                    }
                }
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM