In MongoDB, I can set up a JSON Schema validation in a collection I create, such as these:
db.createCollection("teste", {
validator: {
jsonSchema: {
bsonType: "object",
required: ["campo1", "numero1"],
properties: {
campo1: {
bsonType: "string",
description: "Deve ser uma string"
},
numero1: {
bsonType: "string",
description: "Deve ser um texto"
},
}
}
}
})
But I have mixed-type collections that make use of the Polymorphic Pattern. Where I have something like five or more "types" of schema that are accepted in a collection.
Can I setup multiple JSON Schema validators in a collection? or just one?
Yes, you can. $jsonSchema is just another query operator, you can use $or and $and the same way as you would in the filter:
{
$or: [
{
$jsonSchema: {
...
}
},
{
$jsonSchema: {
...
}
}
]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With