How to search for users by both first and last name with MongoDB?
I see couple of mistakes in your code causing undesired result.
Aggregation pipeline accepts array of aggregation framework operations. In your case, you are missing
[]
operator. It should be likeUser.aggregate([{$project...},{$match...}])
In $match stage you are using regex, if you are using
/../
style of regex, you don't need to wrap it around string quotes. It should be/bob j/i
Here is finished example:
User.aggregate([
{$project: { "name" : { $concat : [ "$firstName", " ", "$lastName" ] } }},
{$match: {"name": {$regex: /bob j/i}}}
]).exec(function(err, result){
console.log(result);
});
You should see [ { _id: 574c3e20be214bd4078a9149, name: 'Bob Jerry' } ]
on screen.