[英]Mongoose Regex on Virtual Field
I'd like to run a regex to achieve a "LIKE" type functionality on a virtual field in my User schema. 我想运行一个正则表达式来在我的用户模式中的虚拟字段上实现“LIKE”类型功能。
The following statement works for all of the fields except * fullName * 以下语句适用于除 * fullName * 之外的所有字段
var searchString = stringUtils.removeMultipleSpaces(stringUtils.stripSpecialCharacters(req.param('searchString')));
var regex = new RegExp(searchString);
var query = User.find().or([{ 'firstName' : { $regex: regex}}, { 'lastName': { $regex: regex }},
{ 'userName': { $regex: regex }}, { 'fullName' : {$regex: regex }}]).sort('lastName');
query.select('firstName lastName userName fullName');
query.exec(function(err, users) {
res.send(users);
});
The virtual field declaration in the mongoose schema for User 用户的mongoose模式中的虚拟字段声明
//full name
UserSchema.virtual('fullName')
.get(function() {
return this.firstName + ' ' + this.lastName;
});
What is the right approach to get the fullName field regex to work properly? 使fullName字段正则表达式正常工作的正确方法是什么?
As you already stated (and showed us), fullName
is a virtual, it's on the application layer. 正如您已经说过的(并向我们展示),
fullName
是虚拟的,它位于应用程序层。 You cannot query it. 你不能查询它。
See also: Sorting by virtual field in mongoDB (mongoose) 另请参阅: 按mongoDB中的虚拟字段排序(mongoose)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.