如何在mongodb中保存正则表达式模式。
示例:电子邮件地址模式
/ [A-ZA-Z0-9 ._%+ - ] + @ [A-ZA-Z0-9 .-] + [A-ZA-Z] {2,4} / < / strong>(电子邮件地址),
示例:用户名模式
/ [A-Za-z0-9 - \ @。,_] {6,50} /(用户名)。
这里我验证了用户类型,就像我需要根据模式验证电子邮件地址一样。
{
"_id" : "Validation_rule",
"flowId" : "String",
"lineOfBusiness" : "String",
"description" : "Invalid User type.It should be either of 'Admin', 'enduser'",
"spelExprRecord" : {
"className" : "com.my.Account.User",
"booleanSpelExpression" : "(role.equalsIgnoreCase('admin') == true) || (role.equalsIgnoreCase('enduser') == true) "
}
}
答案 0 :(得分:1)
这是使用验证器创建的示例集合。仅当满足验证器中指定的3个三个条件时才会插入/更新文档
db.createCollection( "contacts",
{ validator: { $and:
[
{ phone: { $type: "string" } },
{ email: { $regex: /@mongodb\.com$/ } },
{ status: { $in: [ "Unknown", "Incomplete" ] } }
]
}
} )