6

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. 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"
}
Luke101
  • 63,072
  • 85
  • 231
  • 359

4 Answers4

1

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

then save dataToSaveToMogoAsDynamic.

Hope it helps

dns_b
  • 81
  • 6
0

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. Same goes with "_v"

        MyClass item = new MyClass();
        var returnDocument = new BsonDocument(item.ToBsonDocument());
        returnDocument.Remove("_t");
        collection.InsertOne(returnDocument);
Joseph
  • 47
  • 8
0

This solution works 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));
0

the finally solution for remove "_t" from all nested values:

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