MongoDB: 查找具有嵌套关系的 2 个集合?
MongoDB: Lookup 2 collections with nested relation?
我在 MongoDB 中查找 2 个集合时遇到问题
我的数据库包含 3 个集合:PROJECT、CATEGORY 和 SUBCATEGORY,其中 1 个 PROJECT 可以在多个 CATEGORY 中,1 个 CATEGORY 可以有多个 SUBCATEGORY
项目
"project": [
{
_id: 234,
title: "proj 1",
description: "description 1",
category: [
{
_id: 1,
subcategory: [
{
_id: 11
},
{
_id: 12
}
]
},
{
_id: 2,
subcategory: [
{
_id: 21
},
{
_id: 23
}
]
}
]
},
类别
"category": [
{
_id: 1,
title: "cate 1",
subcategory: [
{
_id: 11,
},
{
_id: 12,
},
{
_id: 13,
},
{
_id: 14,
},
]
},
{
_id: 2,
title: "cate 2",
subcategory: [
{
_id: 21,
},
{
_id: 22,
},
{
_id: 23,
},
]
}
]
子类别
"subcategory": [
{
"_id": 11,
title: "subcate 11",
},
{
"_id": 12,
title: "subcate 12",
},
{
"_id": 13,
title: "subcate 13",
},
{
"_id": 14,
title: "subcate 14",
},
{
"_id": 21,
title: "subcate 21",
},
{
"_id": 22,
title: "subcate 22",
},
{
"_id": 23,
title: "subcate 23",
},
]
我想同时查找categoryId 和subcategoryId,但我一次只能查找其中一个,所以当我同时查找它们时,我无法将它们的关系重新连接在一起。
例如,第一种方式,当我首先查找 subcategoryId 时,我得到了子类别的详细信息,但是当我尝试查找 categoryId 时,包含子类别的类别详细信息会覆盖 subcategoryLookup。另一方面,当我首先查找 categoryId 时,我丢失了 subcategoryId 的信息。
这就是我要的数据
[
{
"_id": 234,
"title": "proj 1"
"description: "description 1"
"category": [
{
"_id": 1,
"title": "cate 1"
"subcategory": [
{
"_id": 11,
"title": "subcate 11"
},
{
"_id": 12,
"title": "subcate 12"
}
],
},
{
"_id": 2,
"title": "cate 2"
"subcategory": [
{
"_id": 21,
"title": "subcate 21"
},
{
"_id": 23,
"title": "subcate 23"
}
],
}
],
}
]
我已经尝试了很多方法,现在我处于这个阶段,我得到了 CATEGORY 组但没有 PROJECT
https://mongoplayground.net/p/LXJ-BsTWX3a
请帮助我,我想不出将这些数据组织在一起的方法
密码是
db.project.aggregate([
{
$match: {
_id: 234
}
},
{
"$unwind": "$category"
},
{
"$lookup": {
"from": "subcategory",
"localField": "category.subcategory._id",
"foreignField": "_id",
"as": "category.subcategory"
}
},
{
"$lookup": {
"from": "category",
"localField": "category._id",
"foreignField": "_id",
"as": "joinFortitle"
}
},
{
"$addFields": {
"category.title": {
"$arrayElemAt": [
"$joinFortitle",
0
]
}
}
},
{
"$addFields": {
"category.title": "$category.title.title"
}
},
{
"$group": {
"_id": "$_id",
"title": {
"$first": "$title"
},
"description": {
"$first": "$description"
},
"category": {
$push: "$category"
}
}
}
])
我在 MongoDB 中查找 2 个集合时遇到问题 我的数据库包含 3 个集合:PROJECT、CATEGORY 和 SUBCATEGORY,其中 1 个 PROJECT 可以在多个 CATEGORY 中,1 个 CATEGORY 可以有多个 SUBCATEGORY
项目
"project": [
{
_id: 234,
title: "proj 1",
description: "description 1",
category: [
{
_id: 1,
subcategory: [
{
_id: 11
},
{
_id: 12
}
]
},
{
_id: 2,
subcategory: [
{
_id: 21
},
{
_id: 23
}
]
}
]
},
类别
"category": [
{
_id: 1,
title: "cate 1",
subcategory: [
{
_id: 11,
},
{
_id: 12,
},
{
_id: 13,
},
{
_id: 14,
},
]
},
{
_id: 2,
title: "cate 2",
subcategory: [
{
_id: 21,
},
{
_id: 22,
},
{
_id: 23,
},
]
}
]
子类别
"subcategory": [
{
"_id": 11,
title: "subcate 11",
},
{
"_id": 12,
title: "subcate 12",
},
{
"_id": 13,
title: "subcate 13",
},
{
"_id": 14,
title: "subcate 14",
},
{
"_id": 21,
title: "subcate 21",
},
{
"_id": 22,
title: "subcate 22",
},
{
"_id": 23,
title: "subcate 23",
},
]
我想同时查找categoryId 和subcategoryId,但我一次只能查找其中一个,所以当我同时查找它们时,我无法将它们的关系重新连接在一起。 例如,第一种方式,当我首先查找 subcategoryId 时,我得到了子类别的详细信息,但是当我尝试查找 categoryId 时,包含子类别的类别详细信息会覆盖 subcategoryLookup。另一方面,当我首先查找 categoryId 时,我丢失了 subcategoryId 的信息。
这就是我要的数据
[
{
"_id": 234,
"title": "proj 1"
"description: "description 1"
"category": [
{
"_id": 1,
"title": "cate 1"
"subcategory": [
{
"_id": 11,
"title": "subcate 11"
},
{
"_id": 12,
"title": "subcate 12"
}
],
},
{
"_id": 2,
"title": "cate 2"
"subcategory": [
{
"_id": 21,
"title": "subcate 21"
},
{
"_id": 23,
"title": "subcate 23"
}
],
}
],
}
]
我已经尝试了很多方法,现在我处于这个阶段,我得到了 CATEGORY 组但没有 PROJECT
https://mongoplayground.net/p/LXJ-BsTWX3a
请帮助我,我想不出将这些数据组织在一起的方法
密码是
db.project.aggregate([
{
$match: {
_id: 234
}
},
{
"$unwind": "$category"
},
{
"$lookup": {
"from": "subcategory",
"localField": "category.subcategory._id",
"foreignField": "_id",
"as": "category.subcategory"
}
},
{
"$lookup": {
"from": "category",
"localField": "category._id",
"foreignField": "_id",
"as": "joinFortitle"
}
},
{
"$addFields": {
"category.title": {
"$arrayElemAt": [
"$joinFortitle",
0
]
}
}
},
{
"$addFields": {
"category.title": "$category.title.title"
}
},
{
"$group": {
"_id": "$_id",
"title": {
"$first": "$title"
},
"description": {
"$first": "$description"
},
"category": {
$push: "$category"
}
}
}
])