最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

猫鼬查询以在每次对话中提取最新文档

IT培训 admin 3浏览 0评论

猫鼬查询以在每次对话中提取最新文档

我正在尝试显示其他用户发送给用户的最新消息。

如果,

User A sends Message 1 to User B,
User A sends Message 2 to User B,
User B sends Message 3 to User A,
User A sends Message 4 to User B,
User C sends Message 5 to User B

如果我是用户B,请查看我的收件箱,仅将消息4从用户A返回给用户B,将消息5从用户C返回给用户B。

我该怎么做?到目前为止,我已经尝试过:

    const messages = await Conversation.find({ recipient: id })
      .sort({ date: -1 })
      .distinct("sender")
      .populate("sender")

但是a)它不填充sender,它返回messages [ 5d9b5142d6606f12f5434d41 ],b)我不确定这是正确的查询。

我的模特:

const conversationSchema = new Schema({
  sender: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "user"
  },
  senderHandle: {
    type: String,
    required: true
  },
  recipient: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "user"
  },
  text: {
    type: String,
    required: true
  },
  unread: {
    type: Boolean,
    default: true
  },
  date: {
    type: Date,
    default: Date.now
  }
});

编辑:

我已经尝试过:

    await Conversation.aggregate(
      [
        // Matching pipeline, similar to find
        {
          $match: {
            recipient: id
          }
        },
        // Sorting pipeline
        {
          $sort: {
            date: -1
          }
        },
        // Grouping pipeline
        {
          $group: {
            _id: "$sender",
            text: {
              $first: "$text"
            },
            date: {
              $first: "$date"
            }
          }
        },
        // Project pipeline, similar to select
        {
          $project: {
            _id: 0,
            sender: "$_id"
            text: 1
          }
        }
      ],
      function(err, messages) {
        // Result is an array of documents
        if (err) {
          return res.status(400).send({
            message: getErrorMessage(err)
          });
        } else {
          console.log("latestMessages", messages);
          return res.json(messages);
        }
      }
    )

here的答案,但是返回一个空数组+它似乎不会填充sender

回答如下:

[Distinct返回一个ID数组而不是对象数组,因此,它没有属性,因此无法填充它。

由于要获取最新消息,建议您使用findOne而不是find。

const message = await Conversation.findOne({recipient: id}, {}, { 
                sort: { 'date' : -1 } })
                .populate('sender');

猫鼬查询以在每次对话中提取最新文档

我正在尝试显示其他用户发送给用户的最新消息。

如果,

User A sends Message 1 to User B,
User A sends Message 2 to User B,
User B sends Message 3 to User A,
User A sends Message 4 to User B,
User C sends Message 5 to User B

如果我是用户B,请查看我的收件箱,仅将消息4从用户A返回给用户B,将消息5从用户C返回给用户B。

我该怎么做?到目前为止,我已经尝试过:

    const messages = await Conversation.find({ recipient: id })
      .sort({ date: -1 })
      .distinct("sender")
      .populate("sender")

但是a)它不填充sender,它返回messages [ 5d9b5142d6606f12f5434d41 ],b)我不确定这是正确的查询。

我的模特:

const conversationSchema = new Schema({
  sender: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "user"
  },
  senderHandle: {
    type: String,
    required: true
  },
  recipient: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "user"
  },
  text: {
    type: String,
    required: true
  },
  unread: {
    type: Boolean,
    default: true
  },
  date: {
    type: Date,
    default: Date.now
  }
});

编辑:

我已经尝试过:

    await Conversation.aggregate(
      [
        // Matching pipeline, similar to find
        {
          $match: {
            recipient: id
          }
        },
        // Sorting pipeline
        {
          $sort: {
            date: -1
          }
        },
        // Grouping pipeline
        {
          $group: {
            _id: "$sender",
            text: {
              $first: "$text"
            },
            date: {
              $first: "$date"
            }
          }
        },
        // Project pipeline, similar to select
        {
          $project: {
            _id: 0,
            sender: "$_id"
            text: 1
          }
        }
      ],
      function(err, messages) {
        // Result is an array of documents
        if (err) {
          return res.status(400).send({
            message: getErrorMessage(err)
          });
        } else {
          console.log("latestMessages", messages);
          return res.json(messages);
        }
      }
    )

here的答案,但是返回一个空数组+它似乎不会填充sender

回答如下:

[Distinct返回一个ID数组而不是对象数组,因此,它没有属性,因此无法填充它。

由于要获取最新消息,建议您使用findOne而不是find。

const message = await Conversation.findOne({recipient: id}, {}, { 
                sort: { 'date' : -1 } })
                .populate('sender');
发布评论

评论列表 (0)

  1. 暂无评论