Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 139833

Aggregation query for most recent message from all users and one singular user returning empty array [duplicate]

$
0
0

This question already has an answer here:

I'm trying to get all of the messages between User A and any other user.

My schema is:

const MostRecentMessageSchema = new Schema({
  to: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "user"
  },
  from: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "user"
  },
  conversation: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "conversation"
  },
  date: {
    type: Date,
    default: Date.now
  }
});

And my query:

    await MostRecentMessages.aggregate(
      [
        {
          $match: {
            $or: [
              {
                to: id
              },
              {
                from: id
              }
            ]
          }
        },
        { $sort: { date: -1 } },
        {
          $group: {
            _id: "$from",
            from: {
              $first: "$from"
            },
            to: {
              $first: "$to"
            },
            conversation: {
              $first: "$conversation"
            },
            date: {
              $first: "$date"
            }
          }
        },
        {
          $lookup: {
            from: "conversations",
            localField: "conversation",
            foreignField: "_id",
            as: "conversation"
          }
        },
        { $unwind: { path: "$conversation" } },
        {
          $project: {
            from: {
              $cond: { if: { $eq: ["$to", id] }, then: "$from", else: "$to" }
            },
            to: {
              $cond: { if: { $eq: ["$to", id] }, then: "$to", else: "$from" }
            },
            conversation: "$conversation"
          }
        }
      ],
      function(err, docs) {
        if (err) console.log(err);
        else console.log("docs", docs);

        return res.json(docs);
      }
    );

However, this keeps returning an empty array. What am I doing wrong here? I also need to populate the conversation field, which is why I am doing the $lookup, but something else is fundamentally wrong, because it's not finding any documents.


Viewing all articles
Browse latest Browse all 139833

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>