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>();
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, 2020Comments
-
Journeyman almost 4 years
How do a convert an entire MongoDB BsonDocument into a string?
(I'm using the official C# driver)
-
Blake Niemyjski almost 10 yearsYou need to call the extension method with options to generate valid json: ToJson(new JsonWriterSettings { OutputMode = JsonOutputMode.Strict })
-
kbpontius over 9 yearsI 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 over 9 yearsWhat do you mean by go? You'd get the BsonDocument from the .Net Mongo Driver.