I want to define a Collection in Mongo using Spring-boot with a JSON Schema validator option (https://docs.mongodb.com/manual/core/schema-validation/#json-schema), I don't want a JSR-303 Bean validation (this is not a valid answer Spring data mongoDb not null annotation like Spring data Jpa), but define, at the moment of the creation of the Collection, an option that is showed in the JSON using CollectionInfos().
Example, if I define an Account model class likes:
public class Account {
@Id
private String id;
private String name;
private String surname;
@NotNull
private String username;
}
I want that the collection has, using db.getCollectionInfos(), a json likes:
[
{
"name" : "account",
"type" : "collection",
"options" : {
"validator" : {
"$jsonSchema" : {
"bsonType" : "object",
"required" : [
"username"
]
}
}
},
"info" : {
"readOnly" : false,
"uuid" : UUID("979cdc4b-d6f3-4aef-bc89-3eee812773a5")
},
"idIndex" : {
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "databaseName.account"
}
}
]
The procedure could be similar to spring.jpa.hibernate.ddl-auto = create, because it defines rules at the schema level, and not similar to Bean validator, that defines rules at the Application level.