【问题标题】:Aggregate Combine Two ObjectAarrays聚合组合两个 ObjectAarray
【发布时间】:2018-11-03 00:08:18
【问题描述】:

我想在 NodeJS 中将评论的用户与个人资料图片结合起来,以便在评论出现时显示个人资料图片。

我有以下代码:

 Mission.aggregate([
        {
            $match: {
                _id: ({ $in: [mongoose.Types.ObjectId(id)] })
            }
        },
        {
            $limit: 1
        },
        {
            $unwind: {
                "path": "$comments",
                "preserveNullAndEmptyArrays": true
            }
        },
        {
            $lookup: {
                "from": "users",
                "localField": "comments.user",
                "foreignField": "username",
                "as": "user"
            }
        },
        {
            $unwind: {
                "path": "$user",
                "preserveNullAndEmptyArrays": true
            }
        },
        {
            $group: {
                "_id": "$_id",
                "comment": { "$push": "$comments" },
                "user": { "$push": "$user.profilePicture" },
            }
        }
     ], ...)

任务模型如下所示:

creator: {
        type: String,
        required: true
    },
    text: {
        type: String,
        required: true,
        minlength: 4,
        maxlength: 100
    },
comments: [{
        user: {
            type: String,
            required: true
        },
        text: {
            type: String,
            required: true,
            minLength: 2,
            maxLength: 500
        },
        date: {
            date: Date
        }
    }]

用户模型如下所示:

 username: {
            type: String,
            required: true,
            index: { unique: true },
            minlength: 3,
            maxlength: 16
        },
        password: {
            type: String,
            required: true,
            minlength: 60,
            maxlength: 60
        },
        email: {
            type: String,
            required: true,
            validate: {
                validator: function (v) {
                    return /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/.test(v);
                },
                message: '{VALUE} is not a valid email address!'
            },
        },
        profilePicture: {
            type: String
        },
        creationDate: {
            type: Date,
        },
        api: {
            apiKey: {
                type: String
            },
            apiSecret: {
                type: String
            }
        }

问题是:如何在同一个对象中获取评论和profilePicture?

【问题讨论】:

  • 无法真正告诉您没有数据可重现,因为显示您拥有的代码只是故事的一部分。为了让人们清楚地看到您在做什么以及想要实现的目标,您需要展示一小部分源文档,理想情况下可以从该样本中获得所需的结果。该代码可作为指南来展示您尝试过的内容以及您是如何进行的,而且很可能答案不会只是对您的代码进行一个小的改动。
  • @NeilLunn 我用模型更新了问题
  • 您没有被要求提供模型或 JSONSchema。您被要求提供实际存在于存储文档中的“数据”。再次阅读评论,因为它告诉你做什么很清楚。如果英语不是您的母语,也许可以找人翻译。

标签: javascript node.js mongodb mongoose aggregation-framework


【解决方案1】:

试着写(希望这对你有用)

     {
        $group: {
            "_id": "$_id",
            "userInfo":{
              $push: {
               "comment": "$comments",
               "user": "$user.profilePicture"
             }
           }
        }
     } 

【讨论】:

  • 听起来不错,但不幸的是不起作用,我用更多数据更新了问题
猜你喜欢
  • 2014-12-01
  • 2020-06-13
  • 1970-01-01
  • 2019-06-10
  • 1970-01-01
  • 2019-05-06
  • 2018-08-17
  • 2017-07-21
  • 2017-10-26
相关资源
最近更新 更多