Hello there:
I want to pull the latest chatBucket between two users. There are 51 messages in each chatBucket. “recent_t” is the timestamp column stored as a number in the database. I use an aggregation query with “$group,” which returns a different result at different times. According to my research, $group is causing the issues. When I remove the $group, all buckets appear. I think there is something wrong with the codes. I would appreciate any help you can give me. Below is the code.
ChatBucket.aggregate(
[
{ $match: { unames: { $in: [uname] } } },
{
$project: {
unames: 1,
recent_t: 1,
msg: { $slice: ["$messages", -1] },
},
},
{
$group: {
_id: "$unames",
time: { $min: "$recent_t" },
msg: { $last: "$msg" },
},
},
{ $unwind: "$_id" },
{ $match: { _id: { $ne: uname } } },
{ $skip: skip },
{ $limit: 5 },
{ $sort: { time: -1 } },
],
async (e, data) => {
if (e) {
reject(e);
}
resolve(data);
}
);