-
Type: Improvement
-
Resolution: Unresolved
-
Priority: Major - P3
-
None
-
Affects Version/s: 2.11.0
-
Component/s: Json
-
None
-
Environment:Windows 10, C#
I'm trying to convert a BsonDocument to a JSON string that MongoShell can correctly interpret. I'm not getting the expected results. Here's my sample code to reproduce the issue:
public static void Main() { var json = "{\"myinteger\" : 12345 }"; var bsonDocument = BsonDocument.Parse(json); if (!bsonDocument["myinteger"].IsInt32) throw new ApplicationException("myInteger element is not an int32"); var settings = new JsonWriterSettings { OutputMode = JsonOutputMode.Shell }; var mainJson = bsonDocument.ToJson(settings); }
This is just a very small part of the code that is trying to generate MongoShell commands to update a MongoDB database. What I get stored in mainJson is this:
{ "myinteger" : 12345 }
What I expected is this:
{ "myinteger" : NumberInt(12345) }
Notice that NumberInt is missing.
This is a critical difference because MongoShell will interpret the first as a floating point number and store it as a floating point number when doing a $set, but will correctly interpret the second as an int32 and store it as an int32 when doing a $set.
I'm using the latest official MongoDB C# NuGet driver, version 2.8.0. Is it possible I've found a bug in ToJson() or am I just missing something?