Validation 在mongo中验证数据的最佳方法是什么?
验证插入或更新到MongoDB中的数据的最佳方法是什么?是为了编写某种服务器执行的Javascript代码来进行验证吗?我认为应用程序处理这种事情是正常的。如果数据在某种程度上无效,在用户纠正您检测到的任何错误之前,不要将其添加到数据存储中。MongoDB没有约束或触发器,因此应用程序必须验证数据Validation 在mongo中验证数据的最佳方法是什么?,validation,mongodb,Validation,Mongodb,验证插入或更新到MongoDB中的数据的最佳方法是什么?是为了编写某种服务器执行的Javascript代码来进行验证吗?我认为应用程序处理这种事情是正常的。如果数据在某种程度上无效,在用户纠正您检测到的任何错误之前,不要将其添加到数据存储中。MongoDB没有约束或触发器,因此应用程序必须验证数据 您还可以编写Javascript脚本,每天检查一次或多次是否存在无效数据。您可以使用它来检查应用程序的业务逻辑的质量 我刚刚开始在基于Zend框架的应用程序中同时使用MongoDB和PHP 我为每个M
您还可以编写Javascript脚本,每天检查一次或多次是否存在无效数据。您可以使用它来检查应用程序的业务逻辑的质量 我刚刚开始在基于Zend框架的应用程序中同时使用MongoDB和PHP
我为每个MongoDB集合创建了一个对象(例如,User.php映射到用户集合)。每个对象都知道它映射到哪个集合,以及需要哪些字段。它还知道应该对每个字段应用哪些过滤器()和验证器()。在执行MongoDB insert()或save()之前,我运行$object->isValid(),它执行所有验证器。如果它们都通过isValid()将返回true,我将继续运行insert()或save(),否则我将显示错误。从2.4开始,MongoDB在写入MongoDB数据文件时为mongod和mongorestore启用基本的BSON对象验证。这可以防止任何客户端将无效或格式错误的BSON插入MongoDB数据库。
来源:从他们添加的MongoDB 3.2开始() 您可以使用几乎所有mongo查询运算符(除了
$geoNear
,$near
,$nearSphere
,$text
,和$where
)的选项为每个集合指定验证规则
要使用验证器创建新集合,请使用:
db.createCollection("your_coll", {
validator: { `your validation query` }
})
要将验证器添加到现有集合,可以添加验证器:
db.createCollection("your_coll", {
validator: { `your validation query` }
})
验证只在insert/update上工作,因此在旧集合上创建验证程序时,不会验证以前的数据(您可以为以前的数据编写应用程序级验证)。您还可以指定并告知如果文档未通过验证会发生什么情况
如果您尝试使用未通过验证的内容插入/更新文档(并且未指定任何奇怪的验证级别/操作),则您将在writeResult
上收到一个错误(遗憾的是,该错误没有告诉您失败的内容,您只收到默认的验证未通过
):
在启动MongoDB 3.6时,还可以使用JSON模式来表示。这些检查将在插入/更新时在数据库端进行 以下是文档中的一个示例:
validator = {
$jsonSchema: {
bsonType: "object",
required: [ "name", "year", "major", "address" ],
properties: {
name: {
bsonType: "string",
description: "must be a string and is required"
},
year: {
bsonType: "int",
minimum: 2017,
maximum: 3017,
description: "must be an integer in [ 2017, 3017 ] and is required"
},
major: {
enum: [ "Math", "English", "Computer Science", "History", null ],
description: "can only be one of the enum values and is required"
},
gpa: {
bsonType: [ "double" ],
description: "must be a double if the field exists"
},
address: {
bsonType: "object",
required: [ "city" ],
properties: {
street: {
bsonType: "string",
description: "must be a string if the field exists"
},
city: {
bsonType: "string",
description: "must be a string and is required"
}
}
}
}
}
}
将进行验证。
validator = {
$jsonSchema: {
bsonType: "object",
required: [ "name", "year", "major", "address" ],
properties: {
name: {
bsonType: "string",
description: "must be a string and is required"
},
year: {
bsonType: "int",
minimum: 2017,
maximum: 3017,
description: "must be an integer in [ 2017, 3017 ] and is required"
},
major: {
enum: [ "Math", "English", "Computer Science", "History", null ],
description: "can only be one of the enum values and is required"
},
gpa: {
bsonType: [ "double" ],
description: "must be a double if the field exists"
},
address: {
bsonType: "object",
required: [ "city" ],
properties: {
street: {
bsonType: "string",
description: "must be a string if the field exists"
},
city: {
bsonType: "string",
description: "must be a string and is required"
}
}
}
}
}
}
db.runCommand( {
collMod: "collectionName",
validator: validator
} )