Can a field validation in MongoDB collection contain string and null both as validation types.
{
"$jsonSchema": {
"bsonType": "object",
"title": "Validations",
"properties": {
"t1": {
"bsonType": "string",
"description": "label field is required with min. 1 character in length"
}
}
}
}
Can we specify field t1
be either a null or string type?
2
Answers
Just use
{ "bsonType": [ "null", "string" ] }
like the official doc did.I think just add ‘null’ into bsonType. We also could combine with another validation (example with minLength). It will be validated if the value is not null