I am using mongodb 3.6 and have below validator for the students collection:
rs1:PRIMARY> db.getCollectionInfos({name: 'students'})
[
{
"name" : "students",
"type" : "collection",
"options" : {
"validator" : {
"$jsonSchema" : {
"properties" : {
"name" : {
"bsonType" : "string",
"description" : "name"
},
"age" : {
"bsonType" : "int",
"minimum" : 10,
"maximum" : 30
}
},
"required" : [
"name",
"age"
],
"bsonType" : "object"
}
},
"validationLevel" : "strict",
"validationAction" : "error"
},
"info" : {
"readOnly" : false,
"uuid" : BinData(4,"111r7z+rQpGoebZ1jAHO4g==")
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "test.students"
}
}
]
it requires two properties name and age. Name must be a string and age must be between 10 and 30. But why does below insert fail?
rs1:PRIMARY> db.students.insert({age: 11, name: 'Mike'})
WriteResult({
"nInserted" : 0,
"writeError" : {
"code" : 121,
"errmsg" : "Document failed validation"
}
})