更新时间:2022-03-02 22:27:14
我在您的代码中发现了几个错误,这些错误导致了不希望的结果.
I see couple of mistakes in your code causing undesired result.
聚合管道接受一组聚合框架操作.就您而言,您缺少 []
运算符.应该是
User.aggregate([{{$ project ...},{$ match ...}])
在$ match阶段,您使用的是正则表达式,如果您使用的是/../
风格的正则表达式,则无需将其用字符串引号引起来.它应该是/bob j/i
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
这是完成的示例:
User.aggregate([
{$project: { "name" : { $concat : [ "$firstName", " ", "$lastName" ] } }},
{$match: {"name": {$regex: /bob j/i}}}
]).exec(function(err, result){
console.log(result);
});
您应该在屏幕上看到 [{_id:574c3e20be214bd4078a9149,名称:'Bob Jerry'}]
.