【发布时间】:2018-03-05 14:55:21
【问题描述】:
我想返回所有聊天对话,其中登录用户 (user_id) 是参与者。
我想填充仅返回 profile.firstname 的参与者(可能稍后会出现其他名称),然后我想过滤掉参与者,以便它不会带回参与者数组中的 loggedInUser (user_id)。
chat.controller.js 索引
Chat.find({participants: user_id})
.populate('participants', {
select: 'profile.firstname',
where('_id').ne(user_id) // This need to change
})
.exec(function (err, chats) { });
chat.model.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
let ChatSchema = new Schema({
participants: [{
type: Schema.Types.ObjectId, ref: 'User'
}],
messages: [{
type: Schema.Types.ObjectId, ref: 'Message'
}],
},
{
timestamps: {createdAt: 'created_at', updatedAt: 'updated_at'}
});
module.exports = mongoose.model('Chat', ChatSchema);
【问题讨论】:
标签: node.js mongodb express mongoose mongoose-populate