英文:
Using a collection's aggregation pipeline with all records of another collection
问题
你有两个集合:
Books
{
"BOOK_ID": "100",
"BOOK_NAME": "Book 1",
"BOOK_DESC": "abcd"
},
{
"BOOK_ID": "101",
"BOOK_NAME": "Book 2",
"BOOK_DESC": "efgh"
},
{
"BOOK_ID": "102",
"BOOK_NAME": "Book 3",
"BOOK_DESC": "ijkl"
}
BookGroup
{
"GROUP_ID": "100",
"GROUP_NAME": "G1",
"GROUPS": [
{
"BOOK_ID": "100",
"BOOK_NAME": "Book 1"
},
{
"BOOK_ID": "101",
"BOOK_NAME": "Book 2"
}
]
},
{
"GROUP_ID": "101",
"GROUP_NAME": "G2",
"GROUPS": [
{
"BOOK_ID": "101",
"BOOK_NAME": "Book 2"
}
]
},
{
"GROUP_ID": "102",
"GROUP_NAME": "G3",
"GROUPS": [
{
"BOOK_ID": "100",
"BOOK_NAME": "Book 1"
},
{
"BOOK_ID": "102",
"BOOK_NAME": "Book 3"
}
]
}
我有一个查询来获取单本书的关联组,以下是查询:
db.BookGroup.aggregate([
{
"$unwind": "$GROUPS"
},
{
"$match": {
"GROUPS.BOOK_NAME": "Book 2"
}
},
{
"$group": {
"_id": null,
"group_name": {
"$push": "$GROUP_NAME"
}
}
},
{
"$project": {
"_id": false,
"group_name": true
}
}
])
它可以获取单本书Book 2
的组名,以下是输出:
[
{
"group_name": [
"G1",
"G2"
]
}
]
它正常工作。现在我想获取与每本书关联的组,但我无法使用管道来完成。
以下是预期输出:
{
"BOOK_ID": "100",
"BOOK_NAME": "Book 1",
"BOOK_DESC": "abcd",
"group_name": ["G1", "G3"]
},
{
"BOOK_ID": "101",
"BOOK_NAME": "Book 2",
"BOOK_DESC": "efgh",
"group_name": ["G1", "G2"]
},
{
"BOOK_ID": "102",
"BOOK_NAME": "Book 3",
"BOOK_DESC": "ijkl",
"group_name": ["G3"]
}
这是 MongoDB Playground 的链接。
英文:
I have two collections:
Books
{
"BOOK_ID": "100",
"BOOK_NAME": "Book 1",
"BOOK_DESC": "abcd",
},
{
"BOOK_ID": "101",
"BOOK_NAME": "Book 2",
"BOOK_DESC": "efgh",
},
{
"BOOK_ID": "102",
"BOOK_NAME": "Book 3",
"BOOK_DESC": "ijkl",
}
BookGroup
{
"GROUP_ID": "100",
"GROUP_NAME": "G1",
"GROUPS": [
{
"BOOK_ID": "100",
"BOOK_NAME": "Book 1"
},
{
"BOOK_ID": "101",
"BOOK_NAME": "Book 2"
}
]
},
{
"GROUP_ID": "101",
"GROUP_NAME": "G2",
"GROUPS": [
{
"BOOK_ID": "101",
"BOOK_NAME": "Book 2"
}
]
},
{
"GROUP_ID": "102",
"GROUP_NAME": "G3",
"GROUPS": [
{
"BOOK_ID": "100",
"BOOK_NAME": "Book 1"
},
{
"BOOK_ID": "102",
"BOOK_NAME": "Book 3"
}
]
}
I have a query to get the associated groups of a single book, following is the query:
db.BookGroup.aggregate([
{
"$unwind": "$GROUPS"
},
{
"$match": {
"GROUPS.BOOK_NAME": "Book 2"
}
},
{
"$group": {
"_id": null,
"group_name": {
"$push": "$GROUP_NAME"
}
}
},
{
"$project": {
"_id": false,
"group_name": true
}
}
])
It is able to get the group names of a single book Book 2
. And following is the output:
[
{
"group_name": [
"G1",
"G2"
]
}
]
It is working as expected. Now I would like to get the groups associated to each book but I am not able to do it using the pipeline.
Following is the expected output:
{
"BOOK_ID": "100",
"BOOK_NAME": "Book 1",
"BOOK_DESC": "abcd",
"group_name": ["G1", "G3"]
},
{
"BOOK_ID": "101",
"BOOK_NAME": "Book 2",
"BOOK_DESC": "efgh",
"group_name": ["G1", "G2"]
},
{
"BOOK_ID": "102",
"BOOK_NAME": "Book 3",
"BOOK_DESC": "ijkl",
"group_name": ["G3"]
}
This is the mongo playground link.
Kindly advise. Thank you.
答案1
得分: 1
你应该使用 $lookup
来连接 Books 和 BookGroup 集合,并获取 GROUP_NAME
。
db.Books.aggregate([
{
$lookup: {
from: "BookGroup",
localField: "BOOK_ID",
foreignField: "GROUPS.BOOK_ID",
as: "groups"
}
},
{
$set: {
group_name: {
$map: {
input: "$groups",
in: "$$this.GROUP_NAME"
}
}
}
},
{
$unset: "groups"
}
])
英文:
You should work with $lookup
to join both Books and BookGroup collections and get the GROUP_NAME
.
db.Books.aggregate([
{
$lookup: {
from: "BookGroup",
localField: "BOOK_ID",
foreignField: "GROUPS.BOOK_ID",
as: "groups"
}
},
{
$set: {
group_name: {
$map: {
input: "$groups",
in: "$$this.GROUP_NAME"
}
}
}
},
{
$unset: "groups"
}
])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论