猫鼬将其他收藏添加到两个已经合并的收藏中

时间:2020-10-22 11:07:17

标签: node.js mongodb mongoose

因此,我一直在研究简单的关注好友功能。这与我之前的问题here

有关

现在,我想加入其他收藏集。因此,总共会有三个集合,分别是用户关注图片

以下架构:

{
 "userId": { type: String },
 "followers": [{ "followerId": type: String }],
 "followings": [{ "followingId": type: String }], 
}

用户架构:

{
 "fullName": { type: String } 
}

图像架构:

{
 "userId": { type: String },
 "image": { type: String }
}

所以这是我到目前为止尝试过的:

db.followings.aggregate([
  {
    $addFields: {
      userIds: {
        $setUnion: [
          {
            $map: {
              input: "$followers",
              in: "$$this.followerId"
            }
          },
          {
            $map: {
              input: "$followings",
              in: "$$this.followingId"
            }
          }
        ]
      }
    }
  },
  {
    $lookup: {
      from: "users",
      localField: "userIds",
      foreignField: "_id",
      as: "users"
    }
  },
  {
        $lookup: {
          from: "images",
          localField: "userId",
          foreignField: "userId",
          as: "images"
        }
    },
    {
        $addFields: {
          "images": {
            $arrayElemAt: [
              "images",
              0
            ]
          }
        }
    },
  {
    $project: {
      userId: 1,
      followers: {
        $map: {
          input: "$followers",
          as: "f",
          in: {
            $mergeObjects: [
              "$$f",
              {
                fullName: {
                  $reduce: {
                    input: "$users",
                    initialValue: "",
                    in: {
                      $cond: [
                        {
                          $eq: [
                            "$$this._id",
                            "$$f.followerId"
                          ]
                        },
                        "$$this.fullName",
                        "$$value"
                      ]
                    }
                  }
                },
                {
                  image: "$images.image"
                }
              }
            ]
          }
        }
      },
      followings: {
        $map: {
          input: "$followings",
          as: "f",
          in: {
            $mergeObjects: [
              "$$f",
              {
                fullName: {
                  $reduce: {
                    input: "$users",
                    initialValue: "",
                    in: {
                      $cond: [
                        {
                          $eq: [
                            "$$this._id",
                            "$$f.followingId"
                          ]
                        },
                        "$$this.fullName",
                        "$$value"
                      ]
                    }
                  }
                },
                {
                  image: "$images.image"
                }
              }
            ]
          }
        }
      }
    }
  }
])

但是,我仍然遇到以下错误消息:

"$reduce requires that 'input' be an array, found: {}"

感谢您的帮助。

P.S。 感谢@turishival的帮助。

0 个答案:

没有答案