MongoDB聚合函数无法按预期运行

时间:2019-11-10 10:38:46

标签: javascript mongodb mongoose

我正在尝试显示用户A和任何其他用户之间的最后一条消息的列表。这是我到目前为止的内容,但让我解释以下查询的错误之处:

如果用户A向用户B发送消息“ Test1”,并且用户B答复“ Test2”,然后用户A答复“ Test3”,则仅显示Test2消息。

“ Test1”消息将首先显示,因为它是会话中的第一条消息,但是从那以后,它将仅显示用户B的消息。

在这种情况下,我想要的行为是查询始终返回“ Test3”。这是我所拥有的:

我的模特:

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"
  },
  deletedBy: {
    type: [String]
  },
  date: {
    type: Date,
    default: Date.now
  }
});
        await MostRecentMessages.aggregate(
          [
            {
              $match: {
                $or: [
                  { from: mongoose.Types.ObjectId(userId) },
                  { to: mongoose.Types.ObjectId(userId) }
                ],
                deletedBy: { $ne: mongoose.Types.ObjectId(userId) },
                _id: { $ne: filteredIds }
              }
            },
            { $project: { _id: 1, from: 1, to: 1, conversation: 1, date: 1 } },
            { $sort: { date: -1 } },
            {
              $group: {
                _id: {
                  userConcerned: {
                    $cond: {
                      if: {
                        $eq: ["$to", mongoose.Types.ObjectId(userId)]
                      },
                      then: "$to",
                      else: "$from"
                    }
                  },
                  interlocutor: {
                    $cond: {
                      if: {
                        $eq: ["$to", mongoose.Types.ObjectId(userId)]
                      },
                      then: "$from",
                      else: "$to"
                    }
                  }
                },
                from: { $first: "$from" },
                to: { $first: "$to" },
                date: { $first: "$date" },
                conversation: { $first: "$conversation" }
              }
            },
            {
              $lookup: {
                from: "conversations",
                localField: "conversation",
                foreignField: "_id",
                as: "conversation"
              }
            },
            { $unwind: { path: "$conversation" } },
            {
              $lookup: {
                from: "users",
                localField: "to",
                foreignField: "_id",
                as: "to"
              }
            },
            { $unwind: { path: "$to" } },
            {
              $lookup: {
                from: "users",
                localField: "from",
                foreignField: "_id",
                as: "from"
              }
            },
            { $unwind: { path: "$from" } }
          ],
          function(err, mostRecentMessages) {
            if (err) {
              console.log("Error fetching most recent messages", err);
            } else {
              if (connectedUsersAllMessages[userId]) {
                allMessagesSocket.sockets.connected[
                  connectedUsersAllMessages[userId].socketId
                ].emit("response", {
                  mostRecentMessages
                });
              }
            }
          }
        );
      }, 5000);
    }

我在这里做错了什么?这是POC。它应该返回

  {
    from: "A",
    to: "C",
    number: 3,
    message: "A-C Test 4"
  },

但它返回

  {
    from: "C",
    to: "A",
    number: 3,
    message: "A-C Test 3"
  },

0 个答案:

没有答案