Convert MongoDB BsonDocument to valid JSON in C#

后端 未结 10 2575
旧巷少年郎
旧巷少年郎 2020-12-01 14:16

I am working with the MongoDB C# driver. I have a BsonDocument with some data which includes some MongoDB-specific types (like ObjectIDs and ISODates). I want t

10条回答
  •  天命终不由人
    2020-12-01 14:51

    I've ran into the same thing, you can get valid JSON via:

    var jsonWriterSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict };
    JObject json = JObject.Parse(postBsonDoc.ToJson(jsonWriterSettings));
    

    However it will return something like:

    {"_id":{"$oid":"559843798f9e1d0fe895c831"}, "DatePosted":{"$date":1436107641138}}
    

    I'm still trying to find a way to flatten that.

提交回复
热议问题