Convert MongoDB BsonDocument to valid JSON in C#

后端 未结 10 2566
旧巷少年郎
旧巷少年郎 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:45

    Through experimentation I discovered that there is an option that makes this method output proper JSON:

    BsonDocument myBsonDocument = ... //code that loads a BSON document
    myBsonDocument.ToJson(new JsonWriterSettings { OutputMode = JsonOutputMode.RelaxedExtendedJson})
    

    Result:

    { "_id" : { "$oid" : "5fb7a33e73152101d6610e9d" }, "moreProperties" : "moreValues" }
    

提交回复
热议问题