【问题标题】:how to get count of unread messages against each user Mongoose如何获取针对每个用户 Mongoose 的未读消息计数
【发布时间】:2019-05-04 00:28:15
【问题描述】:

我想从用户集合中选择用户列表,如果用户有未读消息,消息模式中的状态为 0/false,那么也选择未读消息计数,可以是 0/5/10/1/0。提前谢谢你!我确信它可以通过聚合框架完成,并且已经尝试了一些查询(如下),但是我没有得到我需要的结果。

//User Schema 
import mongoose from 'mongoose';

const userSchema = new mongoose.Schema({
 name: { type: String, required: true, trim: true },
 email:{type: String, required: true, unique: true, minlength: 3, trim: true},
 password: { type: String, required: true, minlength: 6, trim: true },
});

   export default mongoose.model('user', userSchema);

//Message Schema
import mongoose from 'mongoose';

const messageSchema = new mongoose.Schema({
    from: { type: mongoose.Schema.Types.ObjectId, ref: 'user' },
    to: { type: mongoose.Schema.Types.ObjectId, ref: 'user' },
    text: { type: String, trim: true },
    unread: { type: Boolean, default: false }
});
messageSchema.set('timestamps', true);

export default mongoose.model('message', messageSchema);

我想要的结果应该是

[ { _id: 5cc984981fa38539f4a61ce0,
     name: 'Jhon Doe',
     unreadTotal: 5 },

   { _id: 5cc98f651fa38539f4a61cfd,
    name: 'Markas',
     unreadTotal: 3 },

   { _id: 5cc994b164745026c4e25546,
     name: 'Mike',
    unreadTotal: 0 } ]


   // i tried the following but getting unread of total not for each user
    const userId = 'loggedinUserId'
    const userList = await User.aggregate([
            {
                $match: { _id: {$ne: ObjectId(userId) } }
            },
            { $lookup: 
                {
                    from: 'messages',
                    let: { 'to': ObjectId(userId) },
                    pipeline: [
                        { 
                            $match: 
                            {
                                'unread': false,
                                $expr: { $eq: [ '$$to', '$to' ] }
                            }
                        },
                        { $count: 'count' }
                    ],
                    as: 'messages'    
                }
            },
            { 
                $addFields: 
                {
                    'unreadTotal': { $sum: '$messages.count' }
                }
            }
          ]);

【问题讨论】:

    标签: node.js mongoose


    【解决方案1】:

    不确定这是否对您有帮助,但您将收到每个用户针对已登录用户的所有未读消息,如您所描述的,稍后您可以使用 count 等做任何您想做的事情。

    const userList = await User.aggregate([
                {
                    $match: { _id: {$ne: ObjectId(userId) } }
                },
                { 
                    $lookup: 
                    {
                        from: 'messages',
                        localField: '_id',
                        foreignField: 'from',
                        as: 'messages'
                    },
                },
                {
                    $project: 
                    {
                        name: 1,
                        email: 1,
                        profilePhoto: 1,
                        messages: 
                        { 
                            $filter: 
                            { 
                                input: "$messages", 
                                as: "message", 
                                cond: { 
                                    $and: [
                                        { $eq: [ '$$message.unread', false ] },
                                        { $eq: [ '$$message.to', ObjectId(userId) ] }
                                    ]
                                }
                            }
                        }
                    }
                }
            ]);
    
    

    【讨论】:

      猜你喜欢
      • 2020-04-01
      • 2010-09-09
      • 1970-01-01
      • 1970-01-01
      • 2021-10-11
      • 2021-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多