Schema Validation

In subset pattern, how to enforce schema validation like top_five_reviews contain only 5 reviews ?

You can use JSON Schema for validating the documents.

If the question is about ensuring that the number of elements varies from 0 to 5, you would the following attributes for the array definition:

{
 "type": "array",
 "minItems": 0,
 "maxItems": 5
 ...
}

Thanks. https://docs.mongodb.com/manual/reference/operator/query/jsonSchema/index.html

1 Like