Zaid Masud
Zaid Masud

Reputation: 13453

.ToJson() ignores null values using MongoDB C# Driver

Using the MongoDB C# driver, the following code gives me { } in the json variable. It seems to ignore the null value in its serialization.

var document = new BsonDocument{{"x", null}};
var json = document.ToJson();

However I want json to be { "x": null }. Can I set some option for it to serialize null values?

Upvotes: 0

Views: 1840

Answers (1)

AntonioOtero
AntonioOtero

Reputation: 1789

Somebody else asked something similar here

Basically, you have to use BsonNull.Value.

Upvotes: 2

Related Questions