猫鼬聚合查询仅应返回一条记录时返回多条记录 [英] Mongoose aggregation query return more than one record when it should return only one

查看:53
本文介绍了猫鼬聚合查询仅应返回一条记录时返回多条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下查询,它应该提取从所有用户发送到单个用户的最新消息:

I have the following query, it is supposed to pull out the most recent message sent from all users to a single user:

    await Conversation.aggregate(
      [
        // Matching pipeline, similar to find
        {
          $match: {
            recipient: mongoose.Types.ObjectId(id)
          }
        },
        // Sorting pipeline
        {
          $sort: {
            date: -1
          }
        },
        // Grouping pipeline
        {
          $group: {
            _id: "$_id",
            sender: {
              $first: "$sender"
            },
            text: {
              $first: "$text"
            },
            date: {
              $first: "$date"
            },
            unread: {
              $first: "$unread"
            }
          }
        },
        {
          $lookup: {
            from: "users",
            localField: "sender",
            foreignField: "_id",
            as: "sender"
          }
        },
        { $unwind: { path: "$sender" } },
        // Project pipeline, similar to select
        {
          $project: {
            sender: 1,
            _id: 1,
            text: 1,
            date: 1,
            unread: 1
          }
        }
      ],
      function(err, messages) {
        if (err) {
          console.log("err with agg query", err);
        } else {
          console.log("latestMessages", messages);
        }
      }
    );

问题是它返回多个记录.它返回:

The problem is that it returns more than one record. It returns:

[
  {
    _id: 5d9b51eed6606f12f5434d46,
    sender: {
      _id: 5d9b5142d6606f12f5434d41,
      email: null,
      numberOfPosts: 0,
      date: 2019-10-07T14:52:50.467Z,
      __v: 0
    },
    text: 'Ayyy',
    date: 2019-10-07T14:55:42.141Z,
    unread: false
  },
  {
    _id: 5d9b5178d6606f12f5434d43,
    sender: {
      _id: 5d9b5142d6606f12f5434d41,
      email: null,
      numberOfPosts: 0,
      date: 2019-10-07T14:52:50.467Z,
      __v: 0
    },
    text: 'hey!',
    date: 2019-10-07T14:53:44.073Z,
    unread: false
  }
]

当应该仅返回发件人为5d9b5142d6606f12f5434d41的第一个元素时,即第一个最新对象.

When it should be returning just the first element where sender is 5d9b5142d6606f12f5434d41, the first, latest object.

我在做什么错了?

推荐答案

更改qroup管道就像

您需要在发件人上进行分组,而不是在唯一的_id上进行分组

You need to group by on sender not on unique _id

{
          $group: {
            _id: "$sender",
            sender: {
              $first: "$sender"
            },
            messageId: {
              $first: "$_id"
            },
            text: {
              $first: "$text"
            },
            date: {
              $first: "$date"
            },
            unread: {
              $first: "$unread"
            }
          }
        }

然后更改$ project

And change in $project

$project: {
            sender: 1,
            _id: "$messageId",
            text: 1,
            date: 1,
            unread: 1
          }

这篇关于猫鼬聚合查询仅应返回一条记录时返回多条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆