如何限制从Mongoose数组填充返回的项目?

时间:2019-04-02 08:42:49

标签: javascript mongodb mongoose mongodb-query populate

我有一个用于嵌套聊天的模型,该模型使用嵌套数组。每次用户发送一条消息时,它都会被推送到messages数组中。这是我的模型:

3

我正在尝试查找用户所属的所有对话,但是我无法仅返回最新的聊天消息,并且似乎忽略了这些选项。这是我的模特还是人口问题?

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const ConversationSchema = new Schema({
  sender: {
    type: Schema.Types.ObjectId,
    ref: 'User'
  },
  messages: [
    {
      message: String,
      meta: [
        {
          user: {
            type: Schema.Types.ObjectId,
            ref: 'User'
          },
          delivered: Boolean,
          read: Boolean
        }
      ]
    }
  ],
  is_group_message: { type: Boolean, default: false },
  participants: [
    {
      user: {
        type: Schema.Types.ObjectId,
        ref: 'User'
      },
      delivered: Boolean,
      read: Boolean,
      last_seen: Date
    }
  ]
}, { timestamps: true });

module.exports = mongoose.model('Conversation', ConversationSchema);

0 个答案:

没有答案