Convert MongoDB BsonDocument into a string

20,710

Solution 1

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

Solution 2

Code used shown below:

string rc = responseDoc.ToJson<MongoDB.Bson.BsonDocument>();
Share:
20,710
Journeyman
Author by

Journeyman

I'm a solution architect and developer working with iOS, Objective-C, XCode, C#, ASP.NET, MVC, jQuery, SQL, MongoDB.

Updated on August 12, 2020

Comments

  • Journeyman
    Journeyman almost 4 years

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

    (I'm using the official C# driver)

  • Blake Niemyjski
    Blake Niemyjski almost 10 years
    You need to call the extension method with options to generate valid json: ToJson(new JsonWriterSettings { OutputMode = JsonOutputMode.Strict })
  • kbpontius
    kbpontius over 9 years
    I realize @BlakeNiemyjski wrote this a little while ago, but could someone elaborate on where the BsonDocument that you're going to convert would go in Blake's example?
  • Blake Niemyjski
    Blake Niemyjski over 9 years
    What do you mean by go? You'd get the BsonDocument from the .Net Mongo Driver.