我有文件夹和文件架构,我想创建文件夹的树结构,并需要将相关文件添加到特定文件夹。到目前为止,我已经创建了文件夹的树结构,现在我正在尝试获取文件(另一个集合)并将它们添加到相关文件夹中。文件将文件夹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
}
]
先谢谢你。
1条答案
按热度按时间bbuxkriu1#
您必须添加另一个$addfileds以获取子级中的文件。我已根据您的要求添加了另一个$lookup。这很困难,您希望按父级和子级查找“localField”:“子项._id”和“本地字段”:“_id”这两个字段不同,父项将在子项“children._id”中,但子项不在父项“_id”中。还要确保您的聚合不应增加更多管道。
请看mongoplay地链接:
https://mongoplayground.net/p/r9Ovj_xIA2W