在mongo中验证数据的最佳方法是什么?

时间:2022-10-22 10:52:13

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?

验证插入或更新到MongoDB中的数据的最佳方法是什么?是编写某种服务器执行的Javascript代码来进行验证吗?

5 个解决方案

#1


9  

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

从MongoDB 3.2开始,他们添加了文档验证(幻灯片)。

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

您可以使用几乎所有mongo查询运算符的验证程序选项为每个集合指定验证规则($ geoNear,$ near,$ nearSphere,$ text和$ 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.

验证仅适用于插入/更新,因此当您在旧集合上创建验证器时,以前的数据将不会被验证(您可以为以前的数据编写应用程序级别验证)。您还可以指定validationLevel和validationAction来说明如果文档未通过验证将会发生什么。

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):

如果您尝试使用未通过验证的内容插入/更新文档(并且未指定任何奇怪的validationLevel / action),那么您将在writeResult上收到错误(遗憾的是,错误不会告诉您失败的原因,而您只能获得默认验证失败):

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

#2


6  

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

MongoDB没有约束或触发器,因此应用程序必须验证数据。

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.

如果存在无效数据,您还可以编写每天检查一次或更多的Javascript脚本。您可以使用它来检查应用程序的业务逻辑的质量。

#3


1  

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


1  

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/

从2.4开始,MongoDB在写入MongoDB数据文件时为mongod和mongorestore启用基本的BSON对象验证。这可以防止任何客户端将无效或格式错误的BSON插入MongoDB数据库。来源:http://docs.mongodb.org/manual/release-notes/2.4/

#5


0  

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

我刚刚开始在基于Zend Framework的应用程序中使用MongoDB和PHP。

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.

我为每个MongoDB集合创建了一个对象(例如User.php映射到用户集合)。每个对象都知道它映射到哪个集合,以及需要哪些字段。它还知道哪些过滤器(Zend_Filter_Input)和验证器(Zend_Validate)应该应用于每个字段。在执行MongoDB insert()或save()之前,我运行$ object-> isValid(),它执行所有验证器。如果它们全部通过isValid()将返回true,我继续运行insert()或save(),否则我显示错误。

#1


9  

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

从MongoDB 3.2开始,他们添加了文档验证(幻灯片)。

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

您可以使用几乎所有mongo查询运算符的验证程序选项为每个集合指定验证规则($ geoNear,$ near,$ nearSphere,$ text和$ 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.

验证仅适用于插入/更新,因此当您在旧集合上创建验证器时,以前的数据将不会被验证(您可以为以前的数据编写应用程序级别验证)。您还可以指定validationLevel和validationAction来说明如果文档未通过验证将会发生什么。

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):

如果您尝试使用未通过验证的内容插入/更新文档(并且未指定任何奇怪的validationLevel / action),那么您将在writeResult上收到错误(遗憾的是,错误不会告诉您失败的原因,而您只能获得默认验证失败):

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

#2


6  

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

MongoDB没有约束或触发器,因此应用程序必须验证数据。

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.

如果存在无效数据,您还可以编写每天检查一次或更多的Javascript脚本。您可以使用它来检查应用程序的业务逻辑的质量。

#3


1  

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


1  

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/

从2.4开始,MongoDB在写入MongoDB数据文件时为mongod和mongorestore启用基本的BSON对象验证。这可以防止任何客户端将无效或格式错误的BSON插入MongoDB数据库。来源:http://docs.mongodb.org/manual/release-notes/2.4/

#5


0  

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

我刚刚开始在基于Zend Framework的应用程序中使用MongoDB和PHP。

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.

我为每个MongoDB集合创建了一个对象(例如User.php映射到用户集合)。每个对象都知道它映射到哪个集合,以及需要哪些字段。它还知道哪些过滤器(Zend_Filter_Input)和验证器(Zend_Validate)应该应用于每个字段。在执行MongoDB insert()或save()之前,我运行$ object-> isValid(),它执行所有验证器。如果它们全部通过isValid()将返回true,我继续运行insert()或save(),否则我显示错误。