【发布时间】:2016-08-08 23:50:01
【问题描述】:
我正在尝试查找按时间戳排序的不同对话,条件是 sender 是 userId 或 recipient 是 userId。我认为做到这一点的唯一方法是使用聚合。这就是我现在拥有的:
Notification.aggregate([
// { $or: [ { $sender: userId }, { $recipient: userId } ] },
{ $group: { _id: '$conversationId' } },
{ $skip: (page - 1) * LIMIT },
{ $limit: LIMIT },
{ $sort: { timestamp: 1 } }
], function (err, result) {
// console.log(result);
return result;
});
但是,我收到了一个“双重回调”错误(因此我注释掉了有问题的 $or 行。
伪代码(我想要实现的)是:
Notification.find().or([
{ sender: userId },
{ recipient: userId }
])
.distinct('conversationId')
.sort('-timestamp')
.limit(LIMIT)
.skip((page - 1) * LIMIT)
.exec(function (error, result) {
return result;
});
【问题讨论】: