MongoDB 保存正则表达式,如电子邮件地址、用户名 [英] MongoDB Save Regular Expressions like email address, username
本文介绍了MongoDB 保存正则表达式,如电子邮件地址、用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在mongodb中保存正则表达式模式
How to save regular expression patterns in mongodb.
示例:电子邮件地址模式
/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[A-Za-z]{2,4}/(电子邮件地址),
示例:用户名模式
/[A-Za-z0-9-\@.,_]{6,50}/(用户名).
这里我验证了用户类型,就像我需要根据模式验证电子邮件地址一样.
Here i validated for user type, in the same way i need to validate email address based on the pattern .
{
"_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) "
}
}
推荐答案
这是使用验证器创建的示例集合.只有满足验证器中指定的 3 个三个条件,才会插入/更新文档
Here's a sample collection created with validator. A document will be inserted / updated only if the 3 three conditions specified in the validators are satisfied
db.createCollection( "contacts",
{ validator: { $and:
[
{ phone: { $type: "string" } },
{ email: { $regex: /@mongodb\.com$/ } },
{ status: { $in: [ "Unknown", "Incomplete" ] } }
]
}
} )
这篇关于MongoDB 保存正则表达式,如电子邮件地址、用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文