使用Spring Data更新MongoDB $ jsonSchema

Spring Data文档describes如何使用给定的$jsonSchema创建集合,以及如何执行验证查询。

是否可以为现有集合更新$jsonSchemaMongoTemplate.createCollection()的现有结果导致MongoCommandException的错误代码为48(存在集合),架构未更新。

andy810404 回答:使用Spring Data更新MongoDB $ jsonSchema

好吧,看起来Spring Data中没有现成的方法,但是实现起来很简单:

<T> void updateSchema(MongoTemplate template,Class<T> entityClazz,MongoJsonSchema schema) {
    template.executeCommand(new Document(Map.of(
            "collMod",template.getCollectionName(entityClazz),"validator",schema.toDocument()
    )));
}

还要记住,默认的readWrite角色是不够的,用户需要具有collMod特权。

本文链接:https://www.f2er.com/3079496.html

大家都在问