使用集合的聚合管道与另一集合的所有记录。

huangapple go评论59阅读模式
英文:

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"
  }
])

在Mongo Playground上查看示例

英文:

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"
  }
])

Demo @ Mongo Playground

huangapple
  • 本文由 发表于 2023年5月26日 17:09:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76339325.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定