如何在mongoDB中使用$graphlookup和$lookup创建文件夹和文件的树形结构

92dk7w1h  于 2022-11-28  发布在  Go
关注(0)|答案(1)|浏览(234)

我有文件夹和文件架构,我想创建文件夹的树结构,并需要将相关文件添加到特定文件夹。到目前为止,我已经创建了文件夹的树结构,现在我正在尝试获取文件(另一个集合)并将它们添加到相关文件夹中。文件将文件夹ID作为引用。
请查找mongo play ground链接了解收集详情,并查询:https://mongoplayground.net/p/07pwbbiF7W6
以下是我的输出:

[
  {
    "_id": "6360468045689c3d0c70e53c",
    "children": [
      {
        "_id": "636a732946b670e689afd454",
        "children": [],
        "files": [
          {
            "_id": ObjectId("635a68aa0651c2d869fc3fd3"),
            "chunkSize": 261120,
            "contentType": "image/jpeg",
            "filename": "pexels-lucie-liz-3165335_2022-10-27T11-16-58.jpg",
            "length": 878669,
            "metadata": {
              "originalname": "pexels-lucie-liz-3165335.jpg",
              "parentFolder": ObjectId("6360468045689c3d0c70e53c"),
              "path": "files/pexels-lucie-liz-3165335_2022-10-27T11-16-58.jpg"
            },
            "uploadDate": ISODate("2022-10-27T11:16:58.612Z")
          },
          {
            "_id": ObjectId("635a80060651c2d869028e68"),
            "chunkSize": 261120,
            "contentType": "image/jpeg",
            "filename": "download_2022-10-27T12-56-38.jpg",
            "length": 5495,
            "metadata": {
              "originalname": "download.jpg",
              "parentFolder": ObjectId("6360468045689c3d0c70e53c"),
              "path": "files/download_2022-10-27T12-56-38.jpg"
            },
            "uploadDate": ISODate("2022-10-27T12:56:38.674Z")
          },
          {
            "_id": ObjectId("636bb8e85d480470d0a6657c"),
            "chunkSize": 261120,
            "contentType": "image/png",
            "filename": "signature_2022-11-09T14-27-52.png",
            "length": 128069,
            "metadata": {
              "originalname": "signature.png",
              "parentFolder": ObjectId("6360468d45689c3d0c70e558"),
              "path": "files/signature_2022-11-09T14-27-52.png"
            },
            "uploadDate": ISODate("2022-11-09T14:27:52.152Z")
          }
        ],
        "level": NumberLong(0),
        "name": "images",
        "parentFolder": "6360468045689c3d0c70e53c",
        "path": "files/images"
      },
      {
        "_id": "6360468d45689c3d0c70e558",
        "children": [
          {
            "_id": "6360469445689c3d0c70e55c",
            "children": [],
            "files": [
              {
                "_id": ObjectId("635a68aa0651c2d869fc3fd3"),
                "chunkSize": 261120,
                "contentType": "image/jpeg",
                "filename": "pexels-lucie-liz-3165335_2022-10-27T11-16-58.jpg",
                "length": 878669,
                "metadata": {
                  "originalname": "pexels-lucie-liz-3165335.jpg",
                  "parentFolder": ObjectId("6360468045689c3d0c70e53c"),
                  "path": "files/pexels-lucie-liz-3165335_2022-10-27T11-16-58.jpg"
                },
                "uploadDate": ISODate("2022-10-27T11:16:58.612Z")
              },
              {
                "_id": ObjectId("635a80060651c2d869028e68"),
                "chunkSize": 261120,
                "contentType": "image/jpeg",
                "filename": "download_2022-10-27T12-56-38.jpg",
                "length": 5495,
                "metadata": {
                  "originalname": "download.jpg",
                  "parentFolder": ObjectId("6360468045689c3d0c70e53c"),
                  "path": "files/download_2022-10-27T12-56-38.jpg"
                },
                "uploadDate": ISODate("2022-10-27T12:56:38.674Z")
              },
              {
                "_id": ObjectId("636bb8e85d480470d0a6657c"),
                "chunkSize": 261120,
                "contentType": "image/png",
                "filename": "signature_2022-11-09T14-27-52.png",
                "length": 128069,
                "metadata": {
                  "originalname": "signature.png",
                  "parentFolder": ObjectId("6360468d45689c3d0c70e558"),
                  "path": "files/signature_2022-11-09T14-27-52.png"
                },
                "uploadDate": ISODate("2022-11-09T14:27:52.152Z")
              }
            ],
            "level": NumberLong(1),
            "name": "Folder3",
            "parentFolder": "6360468d45689c3d0c70e558",
            "path": "files/Folder3"
          },
          {
            "_id": "636a735f46b670e689afd46d",
            "children": [],
            "files": [
              {
                "_id": ObjectId("635a68aa0651c2d869fc3fd3"),
                "chunkSize": 261120,
                "contentType": "image/jpeg",
                "filename": "pexels-lucie-liz-3165335_2022-10-27T11-16-58.jpg",
                "length": 878669,
                "metadata": {
                  "originalname": "pexels-lucie-liz-3165335.jpg",
                  "parentFolder": ObjectId("6360468045689c3d0c70e53c"),
                  "path": "files/pexels-lucie-liz-3165335_2022-10-27T11-16-58.jpg"
                },
                "uploadDate": ISODate("2022-10-27T11:16:58.612Z")
              },
              {
                "_id": ObjectId("635a80060651c2d869028e68"),
                "chunkSize": 261120,
                "contentType": "image/jpeg",
                "filename": "download_2022-10-27T12-56-38.jpg",
                "length": 5495,
                "metadata": {
                  "originalname": "download.jpg",
                  "parentFolder": ObjectId("6360468045689c3d0c70e53c"),
                  "path": "files/download_2022-10-27T12-56-38.jpg"
                },
                "uploadDate": ISODate("2022-10-27T12:56:38.674Z")
              },
              {
                "_id": ObjectId("636bb8e85d480470d0a6657c"),
                "chunkSize": 261120,
                "contentType": "image/png",
                "filename": "signature_2022-11-09T14-27-52.png",
                "length": 128069,
                "metadata": {
                  "originalname": "signature.png",
                  "parentFolder": ObjectId("6360468d45689c3d0c70e558"),
                  "path": "files/signature_2022-11-09T14-27-52.png"
                },
                "uploadDate": ISODate("2022-11-09T14:27:52.152Z")
              }
            ],
            "level": NumberLong(1),
            "name": "images",
            "parentFolder": "6360468d45689c3d0c70e558",
            "path": "files/images"
          }
        ],
        "files": [
          {
            "_id": ObjectId("635a68aa0651c2d869fc3fd3"),
            "chunkSize": 261120,
            "contentType": "image/jpeg",
            "filename": "pexels-lucie-liz-3165335_2022-10-27T11-16-58.jpg",
            "length": 878669,
            "metadata": {
              "originalname": "pexels-lucie-liz-3165335.jpg",
              "parentFolder": ObjectId("6360468045689c3d0c70e53c"),
              "path": "files/pexels-lucie-liz-3165335_2022-10-27T11-16-58.jpg"
            },
            "uploadDate": ISODate("2022-10-27T11:16:58.612Z")
          },
          {
            "_id": ObjectId("635a80060651c2d869028e68"),
            "chunkSize": 261120,
            "contentType": "image/jpeg",
            "filename": "download_2022-10-27T12-56-38.jpg",
            "length": 5495,
            "metadata": {
              "originalname": "download.jpg",
              "parentFolder": ObjectId("6360468045689c3d0c70e53c"),
              "path": "files/download_2022-10-27T12-56-38.jpg"
            },
            "uploadDate": ISODate("2022-10-27T12:56:38.674Z")
          },
          {
            "_id": ObjectId("636bb8e85d480470d0a6657c"),
            "chunkSize": 261120,
            "contentType": "image/png",
            "filename": "signature_2022-11-09T14-27-52.png",
            "length": 128069,
            "metadata": {
              "originalname": "signature.png",
              "parentFolder": ObjectId("6360468d45689c3d0c70e558"),
              "path": "files/signature_2022-11-09T14-27-52.png"
            },
            "uploadDate": ISODate("2022-11-09T14:27:52.152Z")
          }
        ],
        "level": NumberLong(0),
        "name": "Folder2",
        "parentFolder": "6360468045689c3d0c70e53c",
        "path": "files/Folder2"
      }
    ],
    "files": [
      {
        "_id": ObjectId("635a68aa0651c2d869fc3fd3"),
        "chunkSize": 261120,
        "contentType": "image/jpeg",
        "filename": "pexels-lucie-liz-3165335_2022-10-27T11-16-58.jpg",
        "length": 878669,
        "metadata": {
          "originalname": "pexels-lucie-liz-3165335.jpg",
          "parentFolder": ObjectId("6360468045689c3d0c70e53c"),
          "path": "files/pexels-lucie-liz-3165335_2022-10-27T11-16-58.jpg"
        },
        "uploadDate": ISODate("2022-10-27T11:16:58.612Z")
      },
      {
        "_id": ObjectId("635a80060651c2d869028e68"),
        "chunkSize": 261120,
        "contentType": "image/jpeg",
        "filename": "download_2022-10-27T12-56-38.jpg",
        "length": 5495,
        "metadata": {
          "originalname": "download.jpg",
          "parentFolder": ObjectId("6360468045689c3d0c70e53c"),
          "path": "files/download_2022-10-27T12-56-38.jpg"
        },
        "uploadDate": ISODate("2022-10-27T12:56:38.674Z")
      },
      {
        "_id": ObjectId("636bb8e85d480470d0a6657c"),
        "chunkSize": 261120,
        "contentType": "image/png",
        "filename": "signature_2022-11-09T14-27-52.png",
        "length": 128069,
        "metadata": {
          "originalname": "signature.png",
          "parentFolder": ObjectId("6360468d45689c3d0c70e558"),
          "path": "files/signature_2022-11-09T14-27-52.png"
        },
        "uploadDate": ISODate("2022-11-09T14:27:52.152Z")
      }
    ],
    "name": "Folder1",
    "parentFolder": null
  },
  {
    "_id": "6360468045689c3d0c70e53f",
    "children": [
      {
        "children": [],
        "files": [
          {
            "_id": ObjectId("635a68aa0651c2d869fc3fd3"),
            "chunkSize": 261120,
            "contentType": "image/jpeg",
            "filename": "pexels-lucie-liz-3165335_2022-10-27T11-16-58.jpg",
            "length": 878669,
            "metadata": {
              "originalname": "pexels-lucie-liz-3165335.jpg",
              "parentFolder": ObjectId("6360468045689c3d0c70e53c"),
              "path": "files/pexels-lucie-liz-3165335_2022-10-27T11-16-58.jpg"
            },
            "uploadDate": ISODate("2022-10-27T11:16:58.612Z")
          },
          {
            "_id": ObjectId("635a80060651c2d869028e68"),
            "chunkSize": 261120,
            "contentType": "image/jpeg",
            "filename": "download_2022-10-27T12-56-38.jpg",
            "length": 5495,
            "metadata": {
              "originalname": "download.jpg",
              "parentFolder": ObjectId("6360468045689c3d0c70e53c"),
              "path": "files/download_2022-10-27T12-56-38.jpg"
            },
            "uploadDate": ISODate("2022-10-27T12:56:38.674Z")
          },
          {
            "_id": ObjectId("636bb8e85d480470d0a6657c"),
            "chunkSize": 261120,
            "contentType": "image/png",
            "filename": "signature_2022-11-09T14-27-52.png",
            "length": 128069,
            "metadata": {
              "originalname": "signature.png",
              "parentFolder": ObjectId("6360468d45689c3d0c70e558"),
              "path": "files/signature_2022-11-09T14-27-52.png"
            },
            "uploadDate": ISODate("2022-11-09T14:27:52.152Z")
          }
        ]
      }
    ],
    "files": [
      {
        "_id": ObjectId("635a68aa0651c2d869fc3fd3"),
        "chunkSize": 261120,
        "contentType": "image/jpeg",
        "filename": "pexels-lucie-liz-3165335_2022-10-27T11-16-58.jpg",
        "length": 878669,
        "metadata": {
          "originalname": "pexels-lucie-liz-3165335.jpg",
          "parentFolder": ObjectId("6360468045689c3d0c70e53c"),
          "path": "files/pexels-lucie-liz-3165335_2022-10-27T11-16-58.jpg"
        },
        "uploadDate": ISODate("2022-10-27T11:16:58.612Z")
      },
      {
        "_id": ObjectId("635a80060651c2d869028e68"),
        "chunkSize": 261120,
        "contentType": "image/jpeg",
        "filename": "download_2022-10-27T12-56-38.jpg",
        "length": 5495,
        "metadata": {
          "originalname": "download.jpg",
          "parentFolder": ObjectId("6360468045689c3d0c70e53c"),
          "path": "files/download_2022-10-27T12-56-38.jpg"
        },
        "uploadDate": ISODate("2022-10-27T12:56:38.674Z")
      },
      {
        "_id": ObjectId("636bb8e85d480470d0a6657c"),
        "chunkSize": 261120,
        "contentType": "image/png",
        "filename": "signature_2022-11-09T14-27-52.png",
        "length": 128069,
        "metadata": {
          "originalname": "signature.png",
          "parentFolder": ObjectId("6360468d45689c3d0c70e558"),
          "path": "files/signature_2022-11-09T14-27-52.png"
        },
        "uploadDate": ISODate("2022-11-09T14:27:52.152Z")
      }
    ],
    "name": "xxx",
    "parentFolder": null
  }
]

这是我需要的输出:

[
  {
    "_id": "6360468045689c3d0c70e53f",
    "children": [],
    "files": [],
    "name": "xxx",
    "parentFolder": null
  },
  {
    "_id": "6360468045689c3d0c70e53c",
    "children": [
      {
        "_id": "636a732946b670e689afd454",
        "children": [],
        "files": [],
        "level": NumberLong(0),
        "name": "images",
        "parentFolder": "6360468045689c3d0c70e53c",
        "path": "files/images"
      },
      {
        "_id": "6360468d45689c3d0c70e558",
        "children": [
          {
            "_id": "6360469445689c3d0c70e55c",
            "children": [],
            "files": [],
            "level": NumberLong(1),
            "name": "Folder3",
            "parentFolder": "6360468d45689c3d0c70e558",
            "path": "files/Folder3"
          },
          {
            "_id": "636a735f46b670e689afd46d",
            "children": [],
            "files": [],
            "level": NumberLong(1),
            "name": "images",
            "parentFolder": "6360468d45689c3d0c70e558",
            "path": "files/images"
          }
        ],
        "files": [
          {
            "_id": ObjectId("636bb8e85d480470d0a6657c"),
            "chunkSize": 261120,
            "contentType": "image/png",
            "filename": "signature_2022-11-09T14-27-52.png",
            "length": 128069,
            "metadata": {
              "originalname": "signature.png",
              "parentFolder": ObjectId("6360468d45689c3d0c70e558"),
              "path": "files/signature_2022-11-09T14-27-52.png"
            },
            "uploadDate": ISODate("2022-11-09T14:27:52.152Z")
          }
        ],
        "level": NumberLong(0),
        "name": "Folder2",
        "parentFolder": "6360468045689c3d0c70e53c",
        "path": "files/Folder2"
      }
    ],
    "files": [
      {
        "_id": ObjectId("635a68aa0651c2d869fc3fd3"),
        "chunkSize": 261120,
        "contentType": "image/jpeg",
        "filename": "pexels-lucie-liz-3165335_2022-10-27T11-16-58.jpg",
        "length": 878669,
        "metadata": {
          "originalname": "pexels-lucie-liz-3165335.jpg",
          "parentFolder": ObjectId("6360468045689c3d0c70e53c"),
          "path": "files/pexels-lucie-liz-3165335_2022-10-27T11-16-58.jpg"
        },
        "uploadDate": ISODate("2022-10-27T11:16:58.612Z")
      },
      {
        "_id": ObjectId("635a80060651c2d869028e68"),
        "chunkSize": 261120,
        "contentType": "image/jpeg",
        "filename": "download_2022-10-27T12-56-38.jpg",
        "length": 5495,
        "metadata": {
          "originalname": "download.jpg",
          "parentFolder": ObjectId("6360468045689c3d0c70e53c"),
          "path": "files/download_2022-10-27T12-56-38.jpg"
        },
        "uploadDate": ISODate("2022-10-27T12:56:38.674Z")
      }
    ],
    "name": "Folder1",
    "parentFolder": null
  }
]

先谢谢你。

bbuxkriu

bbuxkriu1#

您必须添加另一个$addfileds以获取子级中的文件。我已根据您的要求添加了另一个$lookup。这很困难,您希望按父级和子级查找“localField”:“子项._id”和“本地字段”:“_id”这两个字段不同,父项将在子项“children._id”中,但子项不在父项“_id”中。还要确保您的聚合不应增加更多管道。

db.collection_A.aggregate([
  {
    $match: {
      $or: [
        {
          parentFolder: {
            $exists: false
          }
        },
        {
          $expr: {
            $eq: [
              "$_id",
              "$parentFolder"
            ]
          }
        }
      ]
    }
  },
  {
    $graphLookup: {
      from: "collection_A",
      startWith: "$_id",
      connectFromField: "_id",
      connectToField: "parentFolder",
      depthField: "level",
      as: "children"
    }
  },
  {
    $unwind: {
      path: "$children",
      preserveNullAndEmptyArrays: true
    }
  },
  {
    $lookup: {
      "from": "collection_B",
      "localField": "children._id",
      "foreignField": "metadata.parentFolder",
      "as": "children.files"
    }
  },
  {
    $addFields: {
      "children": {
        $cond: [
          {
            $eq: [
              "$children._id",
              undefined
            ]
          },
          "$$REMOVE",
          "$children"
        ]
      }
    }
  },
  {
    $sort: {
      "children.level": -1
    }
  },
  {
    $group: {
      _id: "$_id",
      parentFolder: {
        $first: "$parentFolder"
      },
      name: {
        $first: "$name"
      },
      children: {
        $push: "$children"
      },
      files: {
        $first: "$children.files"
      }
    }
  },
  {
    $lookup: {
      "from": "collection_B",
      "localField": "_id",
      "foreignField": "metadata.parentFolder",
      "as": "files"
    }
  },
  {
    $addFields: {
      children: {
        $reduce: {
          input: "$children",
          initialValue: {
            level: -1,
            presentChild: [],
            prevChild: []
          },
          in: {
            $let: {
              vars: {
                prev: {
                  $cond: [
                    {
                      $eq: [
                        "$$value.level",
                        "$$this.level"
                      ]
                    },
                    "$$value.prevChild",
                    "$$value.presentChild"
                  ]
                },
                current: {
                  $cond: [
                    {
                      $eq: [
                        "$$value.level",
                        "$$this.level"
                      ]
                    },
                    "$$value.presentChild",
                    []
                  ]
                }
              },
              in: {
                level: "$$this.level",
                prevChild: "$$prev",
                presentChild: {
                  $concatArrays: [
                    "$$current",
                    [
                      {
                        $mergeObjects: [
                          "$$this",
                          {
                            children: {
                              $filter: {
                                input: "$$prev",
                                as: "e",
                                cond: {
                                  $eq: [
                                    "$$e.parentFolder",
                                    "$$this._id"
                                  ]
                                }
                              }
                            }
                          }
                        ]
                      }
                    ]
                  ]
                }
              }
            }
          }
        }
      }
    }
  },
  {
    $addFields: {
      children: "$children.presentChild"
    }
  }
])

请看mongoplay地链接:
https://mongoplayground.net/p/r9Ovj_xIA2W

相关问题