Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert MongoDB BsonDocument into a string

How do a convert an entire MongoDB BsonDocument into a string?

(I'm using the official C# driver)

like image 320
Journeyman Avatar asked May 09 '11 13:05

Journeyman


People also ask

What is BsonDocument in MongoDB?

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

Why does MongoDB use BSON?

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.

What is serialization in MongoDB?

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.


2 Answers

You can convert BsonDocument into a JSON formatted string using MongoDB.Bson.BsonExtensionMethods.ToJson.

like image 165
AndrewC Avatar answered Oct 17 '22 14:10

AndrewC


Code used shown below:

string rc = responseDoc.ToJson<MongoDB.Bson.BsonDocument>();
like image 39
Journeyman Avatar answered Oct 17 '22 15:10

Journeyman