尝试运行我的代码以从mongodb检索消息时,出现错误“参数必须是聚合管道运算符”。我试图以不同的方式构造我的代码,但不确定为什么这行不通。任何帮助将不胜感激,谢谢!
function(callback) {
Message.aggregate([{
$match: {
$or: [{
"senderName": req.user.username
},
{
"receiverName": req.user.username
}
]
}
},
{
$sort: {
"createdAt": -1
}
},
{
$group: {
"_id": {
"last_message_between": {
$cond: [{
$gt: [{
$substr: ["$senderName", 0, 1]
},
{
$substr: ["$receiverName", 0, 1]
}
]
},
{
$concat: ["$senderName", "and", "$receiverName"]
},
{
$concat: ["$receiverName", "and", "$senderName"]
}
]
}
},
"body": {
$first: "$$ROOT"
}
}
},
function(err, newResult) {
callback(err, newResult);
}
])
}