Reputation: 147
I have a question regarding best practices to insert Documents in MongoDb.
In my data source the key "myData2" can be null or a string. Should I add "myData2" as null to my database or is it better to leave the value out if not defined? What is the "clean" way to deal with this?
[{
"myData1": "Stuff",
"myData2": null
}]
Upvotes: 4
Views: 4877
Reputation: 14480
Since MongoDB permits fields to be added to documents at any time, most (production) applications are written to handle both of the following cases:
What would your application do if the field is missing, as opposed to if it's set to the null value? If it would do the same thing, then I suggest not setting fields to null values for two reasons:
Upvotes: 6