Mongoose Virtual Field Modes

I would like to run a regular expression to create functionality of type "LIKE" in a virtual field in my user schema.

The following statement works for all fields except * 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); }); 

Declaring a virtual field in a mongoose pattern for a user

 //full name UserSchema.virtual('fullName') .get(function() { return this.firstName + ' ' + this.lastName; }); 

What is the right approach for the regular expression fullName to work correctly?

+4
source share
1 answer

As you said (and showed us), fullName is virtual, it is at the application level. You cannot request it.

See also: Virtual field sorting in mongoDB (mongoose)

+4
source

Source: https://habr.com/ru/post/1484679/


All Articles