What is the best way to validate data in mongo?

2019-01-15 07:41发布

问题:

What's the best way to validate data being inserted or updated into MongoDB? Is it to write some sort of server executed Javascript code that does the validation?

回答1:

Starting from MongoDB 3.2 they added document validation (slides).

You can specify validation rules for each collection, using validator option using almost all mongo query operators (except $geoNear, $near, $nearSphere, $text, and $where).

To create a new collection with a validator, use:

db.createCollection("your_coll", {
  validator: { `your validation query` }
})

To add a validator to the existing collection, you can add the validator:

db.createCollection("your_coll", {
  validator: { `your validation query` }
})

Validation work only on insert/update, so when you create a validator on your old collection, the previous data will not be validated (you can write application level validation for a previous data). You can also specify validationLevel and validationAction to tell what will happen if the document will not pass the validation.

If you try to insert/update the document with something that fails the validation, (and have not specified any strange validationLevel/action) then you will get an error on writeResult (sadly enough the error does not tell you what failed and you get only default validation failed):

WriteResult({
   "nInserted" : 0,
   "writeError" : {
      "code" : 121,
      "errmsg" : "Document failed validation"
   }
})


回答2:

MongoDB doesn't have constraints or triggers so the application has to validate the data.

You can also write Javascript scripts that check once a day or more if there is invalid data. You can use this to check the quality of the business logic of your application.



回答3:

I think it would be normal for your app to handle this kind of thing. If the data is invalid in some way, don't let it get added to the datastore until the user has corrected whatever error you have detected.



回答4:

Starting in 2.4, MongoDB enables basic BSON object validation for mongod and mongorestore when writing to MongoDB data files. This prevents any client from inserting invalid or malformed BSON into a MongoDB database. source: http://docs.mongodb.org/manual/release-notes/2.4/



回答5:

I've just started using MongoDB and PHP together, inside a Zend Framework based application.

I have created 1 object for each MongoDB collection (e.g. User.php maps to the user collection). Each object knows what collection it maps to, and what fields are required. It also knows which filters (Zend_Filter_Input) and validators (Zend_Validate) should be applied to each field. Before doing a MongoDB insert() or save(), I run $object->isValid(), which executes all the validators. If they all pass isValid() will return true, and I proceed to run the insert() or save(), otherwise I display the errors.