Reputation: 1583
Spring Data documentation describes how to create collection with given $jsonSchema
, and how to perform a validation query.
Is there a way to update $jsonSchema
for an existing collection? MongoTemplate.createCollection()
for an existing one results in MongoCommandException
with error code 48 (collection exists), schema is not being updated.
Upvotes: 3
Views: 1882
Reputation: 1583
Ok, looks like there is no ready-to-use method in Spring Data, but it is pretty simple to implement:
<T> void updateSchema(MongoTemplate template, Class<T> entityClazz, MongoJsonSchema schema) {
template.executeCommand(new Document(Map.of(
"collMod", template.getCollectionName(entityClazz),
"validator", schema.toDocument()
)));
}
Also keep in mind that default readWrite
role is not enough, user needs to have collMod
privilege.
Upvotes: 3