MongoDB provides the capability to validate documents during updates and insertions. Validation rules are specified on a per-collection basis using the
validator
option, which takes a document that specifies the validation rules or expressions. Specify the expressions using any query operators, with the exception of$near
, $nearSphere
, $text
, and $where
.
Add document validation to an existing collection using the
collMod
command with the validator
option. You can also specify document validation rules when creating a new collection usingdb.createCollection()
with the validator
option, as in the following:db.createCollection( "contacts",
{ validator: { $or:
[
{ phone: { $type: "string" } },
{ email: { $regex: /@mongodb\.com$/ } },
{ status: { $in: [ "Unknown", "Incomplete" ] } }
]
}
} )
Comments
Post a Comment