I am trying to find distinct conversations sorted by timestamp and with conditions that sender
is userId
or recipient
is userId
. I think the only way to do this is with aggregations. This is what I have right now:
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;
});
However, I am getting a "double callback" error with this (hence why I commented out the offending $or
line.
The pseudocode (that I am trying to achieve) is:
Notification.find().or([
{ sender: userId },
{ recipient: userId }
])
.distinct('conversationId')
.sort('-timestamp')
.limit(LIMIT)
.skip((page - 1) * LIMIT)
.exec(function (error, result) {
return result;
});