Mongoose - 聚合添加 'is_self' 字段

Mongoose - Add 'is_self' field in aggregate

我正在构建一个聊天应用程序,它应该从 MongoDB 中检索所有新消息,分组到对话中。但是每条消息都应该有一个新的 'is_self' 字段

编辑: 'is_self' 字段包含一个布尔值,用于判断消息是否来自用户。

这么伪:

is_self: {$cond: {if: {message.sender == MYID)}, then: true, else: false}

假设我有消息模型

    var MessageSchema  = new Schema({
    conversation_id:{type: mongoose.Schema.ObjectId, ref: 'Conversation', required: true},
    message: {type: String, required: true},
    sender: {type: mongoose.Schema.ObjectId, ref: 'User'},
    created: {type: Date, default: Date.now},
    read: {type: Boolean, default: false}
});

还有一个对话模型

    var ConversationSchema = new Schema({
    from: {type: mongoose.Schema.ObjectId, ref: 'User', required: true},
    to: {type: mongoose.Schema.ObjectId, ref: 'User', required: true},
    last_changed: {type: Date, default: Date.now},
    created: {type: Date, default: Date.now}
});

现在我尝试做一个聚合,以加载 conversation_id 数组中的所有消息并创建 > last_checked 日期...

所以看起来像这样:

mongoose.model("Message").aggregate([
            // First find all messages
            {
                $match: {
                    $and: [{conversation_id: {$in: idArray}}, {created: {$gt: lastChecked}}]
                }
            },
            // Add is self field
            {
                $group: {
                    _id: $_id,
                    $is_self: {
                        $cond: {'if(message.sender == MYID then true else false': '??'}
                    }
                }
            },
            // Sort by date
            {$sort: {created: -1}},

            // Then group by conversation
            {
                $group: {
                    _id: '$conversation_id',
                    messages: {
                        $push: '$$ROOT'
                    },

                }
            }
            // TODO: find users for unknown conversation
            /*,
            {
                $project: {
                    user: {
                        $or: [{conversation_id: {$in: knownConversations}}]
                    }
                }
            }*/
        ])

我尝试使用 $cond 和 if / else 语句,但 Mongo 不允许这样做..

谢谢!

$eq operator which returns boolean. Also $push 的简单用法将采用您输入的任何对象格式:

var senderId = // whatever;

mongooose.model("Message").aggregate([
    { "$match": {
        "conversation_id": { "$in": idArray },
        "created": { "$gt": lastChecked }
    }},
    { "$group": {
        "_id": "$conversation_id",
        "messages": {
            "$push": {
                "message": "$message",
                "is_self": { 
                    "$eq": [ "$sender", senderId ]
                }
            }
        }
    }}
    // whatever else
],function(err,results) {

})

如果需要,则与 $cond 结合,仅在检测到时交替添加 "is_self":

    { "$group": {
        "_id": "$conversation_id",
        "messages": {
            "$push": {
                "$cond": [
                  { "$eq": [ "$sender", senderId] },
                  {
                    "message": "$message",
                    "is_self": true
                  },
                  {
                     "messsage": "$message" 
                  }
                ]
            }
        }
    }}