How do a convert an entire MongoDB BsonDocument into a string?
(I'm using the official C# driver)
BsonDocument is the default type used for documents. It handles dynamic documents of any complexity. For instance, the document { a: 1, b: [{ c: 1 }] } can be built as follows: var doc = new BsonDocument { { "a", 1 }, { "b", new BsonArray { new BsonDocument("c", 1) }} };
Unlike systems that store JSON as string-encoded values, or binary-encoded blobs, MongoDB uses BSON to offer powerful indexing and querying features on top of the web's most popular data format.
In programming, serialization is the process of turning an object into a new format that can be stored (e.g. files or databases) or transmitted (e.g. over the internet). Deserialization, therefore, is the process of turning something in that format into an object.
You can convert BsonDocument into a JSON formatted string using MongoDB.Bson.BsonExtensionMethods.ToJson
.
Code used shown below:
string rc = responseDoc.ToJson<MongoDB.Bson.BsonDocument>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With