Reputation: 1385
SQL has the convention of leaving fields as null when not present.
Is there a convention in Mongo to leave the field as null or use an empty value (such as an empty array) if the field is not set?
To be specific, I am using Go and Mongo 4.2 with the official Go mongo driver.
For example,
type A struct {
ID string `bson:"_id"`
Bs []B `bson:"bs"`
}
type B struct {
ID string `bson:"_id"`
}
If an A
does not have any B
s (Bs
is empty), should it be defined as a zero-value ([]B(nil)
) or as an empty array ([]B{}
).
Note this affects the way the data is stored:
for zero-value
{
"_id" : ...,
"bs" : null
}
vs for empty value
{
"_id" : ...,
"bs" : [ ]
}
As well as construction before InsertOne
(or insertion in general), since you would want to make sure memory is allocated beforehand for such objects.
Upvotes: 0
Views: 431
Reputation: 11
In GO a string can not be null but a pointer to a string can be null so just use a pointer.
Upvotes: 1