I have a userSchema
like so:
我有一个像这样的userSchema:
var userSchema = new Schema({
name: {
type: String
, required: true
, validate: [validators.notEmpty, 'Name is empty']
}
, username: {
type: String
, required: true
, unique: true
, validate: [validators.notEmpty, 'Username is empty']
}
});
The username
field is supposed to be unique. Mongoose will throw an error if this username already exists in the database. However, it is not case insensitive, which I need it to be.
用户名字段应该是唯一的。如果此用户名已存在于数据库中,则Mongoose将抛出错误。但是,它不是不区分大小写的,我需要它。
Am I right in thinking that the only way to achieve a case insensitive unique check is to write my own validation rule, which will perform a query on the collection? Is it OK to write validation checks like this, creating more connections to the collection? I will need to do something similar for email
, too.
我是否正确地认为实现不区分大小写的唯一检查的唯一方法是编写我自己的验证规则,该规则将对集合执行查询?可以编写这样的验证检查,创建更多与集合的连接吗?我也需要为电子邮件做类似的事情。
5 个解决方案
#1
13
What about using:
怎么样使用:
{ type: String, lowercase: true, trim: true }
to achieve your purpose?
实现你的目的?
#2
2
I don't know if you are doing this in node. But you can use a npm like this: https://github.com/blakehaswell/mongoose-unique-validator to check unique validation on the fields of collections. Other way could be checking in the collection every time a new requests come. http://timstermatic.github.io/blog/2013/08/06/async-unique-validation-with-expressjs-and-mongoose/ You can refer the material here and use it the way suitable for your case.
我不知道你是否在节点中这样做。但是您可以使用这样的npm:https://github.com/blakehaswell/mongoose-unique-validator来检查集合字段的唯一验证。其他方式可能是每次新请求到来时检查集合。 http://timstermatic.github.io/blog/2013/08/06/async-unique-validation-with-expressjs-and-mongoose/您可以在此处参考材料,并以适合您的情况的方式使用它。
#3
1
The best way is to use already existing npm packages as shared below. https://www.npmjs.com/package/mongoose-unique-validator
最好的方法是使用已存在的npm包,如下所示。 https://www.npmjs.com/package/mongoose-unique-validator
To make it case sensitive you can follow uniqueCaseInsensitive in the same page.
要使其区分大小写,您可以在同一页面中跟随uniqueCaseInsensitive。
No need to write your own validation logic when there is already a package available for this(follow Avinash's post too).
当已经有一个可用的软件包时,无需编写自己的验证逻辑(也可以关注Avinash的帖子)。
#4
0
Very simple solution
很简单的解决方案
username : {
trim:true,
//lowercase:true,
type:String,
required:[true, '{PATH} is required.'],
match : [
new RegExp('^[a-z0-9_.-]+$', 'i'),
'{PATH} \'{VALUE}\' is not valid. Use only letters, numbers, underscore or dot.'
],
minlength:5,
maxlength:30,
//unique:true
validate : [
function(un, cb){
console.log(v);
student.findOne({username:/^un$/i}, function(err, doc){
if(err) return console.log(err);
if(!_.isEmpty(doc)) return cb(false);
return cb(true);
});
},
'Username already exists.'
]
},
Here, I am using async validation and checking in my model student
if same field exist. Use can obviously use regex if you want.
在这里,我正在使用异步验证并检查我的模型学生是否存在相同的字段。如果你愿意,使用显然可以使用正则表达式。
But I would not recommend this method, it just doesn't fit in my head.
但我不推荐这种方法,它只是不适合我的想法。
Instead stick with { type: String, lowercase: true, trim: true, unique:true }
approach and copy the original username to some other field in case you need it.
而是坚持使用{type:String,lowercase:true,trim:true,unique:true}方法并将原始用户名复制到其他字段以备不时之需。
#5
-1
How about using a regular expression?
使用正则表达式怎么样?
var pattern = [ /some pattern/, "{VALUE} is not a valid user name!" ];
{ type: String, match: pattern }
For further reference: http://mongoosejs.com/docs/api.html#schematype_SchemaType-required
如需进一步参考:http://mongoosejs.com/docs/api.html#schematype_SchemaType-required
#1
13
What about using:
怎么样使用:
{ type: String, lowercase: true, trim: true }
to achieve your purpose?
实现你的目的?
#2
2
I don't know if you are doing this in node. But you can use a npm like this: https://github.com/blakehaswell/mongoose-unique-validator to check unique validation on the fields of collections. Other way could be checking in the collection every time a new requests come. http://timstermatic.github.io/blog/2013/08/06/async-unique-validation-with-expressjs-and-mongoose/ You can refer the material here and use it the way suitable for your case.
我不知道你是否在节点中这样做。但是您可以使用这样的npm:https://github.com/blakehaswell/mongoose-unique-validator来检查集合字段的唯一验证。其他方式可能是每次新请求到来时检查集合。 http://timstermatic.github.io/blog/2013/08/06/async-unique-validation-with-expressjs-and-mongoose/您可以在此处参考材料,并以适合您的情况的方式使用它。
#3
1
The best way is to use already existing npm packages as shared below. https://www.npmjs.com/package/mongoose-unique-validator
最好的方法是使用已存在的npm包,如下所示。 https://www.npmjs.com/package/mongoose-unique-validator
To make it case sensitive you can follow uniqueCaseInsensitive in the same page.
要使其区分大小写,您可以在同一页面中跟随uniqueCaseInsensitive。
No need to write your own validation logic when there is already a package available for this(follow Avinash's post too).
当已经有一个可用的软件包时,无需编写自己的验证逻辑(也可以关注Avinash的帖子)。
#4
0
Very simple solution
很简单的解决方案
username : {
trim:true,
//lowercase:true,
type:String,
required:[true, '{PATH} is required.'],
match : [
new RegExp('^[a-z0-9_.-]+$', 'i'),
'{PATH} \'{VALUE}\' is not valid. Use only letters, numbers, underscore or dot.'
],
minlength:5,
maxlength:30,
//unique:true
validate : [
function(un, cb){
console.log(v);
student.findOne({username:/^un$/i}, function(err, doc){
if(err) return console.log(err);
if(!_.isEmpty(doc)) return cb(false);
return cb(true);
});
},
'Username already exists.'
]
},
Here, I am using async validation and checking in my model student
if same field exist. Use can obviously use regex if you want.
在这里,我正在使用异步验证并检查我的模型学生是否存在相同的字段。如果你愿意,使用显然可以使用正则表达式。
But I would not recommend this method, it just doesn't fit in my head.
但我不推荐这种方法,它只是不适合我的想法。
Instead stick with { type: String, lowercase: true, trim: true, unique:true }
approach and copy the original username to some other field in case you need it.
而是坚持使用{type:String,lowercase:true,trim:true,unique:true}方法并将原始用户名复制到其他字段以备不时之需。
#5
-1
How about using a regular expression?
使用正则表达式怎么样?
var pattern = [ /some pattern/, "{VALUE} is not a valid user name!" ];
{ type: String, match: pattern }
For further reference: http://mongoosejs.com/docs/api.html#schematype_SchemaType-required
如需进一步参考:http://mongoosejs.com/docs/api.html#schematype_SchemaType-required