Validation in DB on a cloud?

I’m fairly new to the back-end stuff and I was wondering if it’s possible to implement validation in a db that is on a cloud like mLab. I use MongoDB and I know that MongoDB doesn’t care about validation, that’s why we use mongoose to create models using validation.

Let’s assume we have a collection called genres, the model of a genre is as followed:

mongoose.model("Genre", new mongoose.Schema({
 name: {
    type: String,
    minlength: 5,
    maxlength: 50,
    required: true

mLab doesn’t care about the above validation when creating a model, I can create a genre with a name of 2 characters long for example.

So my question is, is it possible to use this validation in my db on mLab?, if not, what would be an option to create a genre (in the db on the cloud) using my model validation?

If it’s a complex object, do you create a model with mongoose, store it in mongoDB and then copy it and paste it to the db on the cloud?

This doesn’t seem like an mLab specific question, since mLab’s database is just the average mongodb instance that is managed for you.

The main point of your application is the provide the validation, mLabs/Mongodb is there to hold it. So if you wanted to “add validation” to your db then your doing something wrong. Mongoose provides utilities todo this in a declarative way so you don’t have to manually do it. But yea nothing is stopping you from going into the database, bypassing your application validation and adding whatever you want (that’s noSql for you!)

If you for some reason wanted to provide validation for items created by you (you being the db admin) you can write js scripts to run against the database using Mongoose. Otherwise your own your own “adding” items into the database.

Finally, the whole point of NOSQL is really the “no validation” part. There’s no structure for a reason. If you wanted to structure you DB you should use an SQL database which is structured, which will enforce some validation, preventing you from “screwing it up” to a degree.

1 Like

This is not seems like to be mat-Lab question. MongoDB is a schema-less database.The schema-less feature has given MongoDB great flexibility and the capability to adapt the database to the changing needs of applications.In real applications built on MongoDB there is always some kind of “fixed schema” or “validation rules” in collections and in documents. It’s possible to have in a collection two documents that represent two completely different things.

1 Like

Not sure if you’d want to. But there are some ways to add Javascript code to NoSQL databases that’s executed by the database itself. For instance, CouchDB even looks like it has a function to do just what you’re saying: