Reputation: 3020
Using 10gen mondgo db c# driver.I have following class
[BsonId]
public ObjectId Id { get; set; }
public int AttemptId { get; set; }
public int UserId { get; set; }
public int QId { get; set; }
public string UserInput { get; set; }
public string Feedback{ get; set; }
By default if i dont sent values of UserInput or Feedback (any string ) mongodb put them as null
. Is there any way to override this with string.empty
while inserting or while fetching the data.
Tried setting [BsonDefaultValue("")]
but this also didnt work.
Upvotes: 6
Views: 9507
Reputation: 12187
[BsonDefaultValue("")] applies only when reading a document from the database that doesn't have a value for the corresponding field. If you want new objects you create in memory to have a value other than null you need to set that value in your constructor. Also, when you save an object to the database if the field is null then that is what is saved and read back.
So just set the default value in your constructor to handle new objects created in memory (and presumably inserted to the database) and use [BsonDefaultValue] to handle reading back documents that might not have a corresponding field.
Upvotes: 19