Providing schema validation in MongoDB

I am looking for ways to verify that the data inserted in MongoDB is consistent with the schema, for example. has all the necessary fields and the correct data types. I know that Mongo itself is sketchy, but if I could check the data at the application level before passing it to the Mongo driver, that would be nice.

I looked at JSON-Schema. My biggest hesitation is that I can only find one Java library for checking the schema, and I don’t know if I should trust it, I prefer libraries supported by Apache or Google.

I also looked at Apache Thrift, Avro, and protocol buffers, which are not specific validation frameworks, but each one has a concept of schema. I would be wondering if there is a way to cancel one of them for verification.

Any suggestions? Or should I accept Mongo's schematics and not even try to verify the data?

+4
source share
2 answers

In the upcoming version of Mongo 3.2, they added document confirmation ( slides ).

You can specify validation rules for each collection using validator using almost all mongo query operators (except $geoNear , $near , $nearSphere , $text and $where ). You can learn more about this in one of my answers .

+5
source

Not sure what you are still looking for, but Object Object Converter (ODM) will do the trick.

I checked both Morphia or Spring Data - MongoDB and will either take care of your security problems of your type, as they will map your documents directly to Java classes.

+3
source

All Articles