查找后的Mongodb聚合项目

时间:2017-09-08 08:19:32

标签: mongodb aggregation-framework lookup spring-mongodb

我正在进行MongoDB聚合。我想查找两个集合,然后只在嵌套数组中投影所需的字段。

要查找的两个集合:

  

db.pitcher.find()。漂亮()

{
         "_id" : ObjectId("59b22eeef224252e6c7eeaf6"),
        "userId" : "a0",
        "name" : "test50000",
        "index" : 50000,
        "position" : "SP",
        "order" : 0,
        "gameRecord" : [
                {
                        "seasonIndex" : 2017251,
                        "gameIndex" : 1,
                        "ERA" : 3.00,
                },
        {
                        "seasonIndex" : 2017251,
                        "gameIndex" : 2,
                        "ERA" : 4.50,
                }
        ]
       }
  

db.gameResult.find()。漂亮()

{
        "_id" : ObjectId("59b22b7dac48252e6c7eeaf6"),
        "seasonIndex" : 2017251,
        "gameIndex" : 1,
        "away" : "a9",
        "home" : "a0",
        "awayScore" : 9,
        "homeScore" : 4,
        "awayPitcherList" : [
                50180
        ],
        "homePitcherList" : [
                50000,
                50049,
                50048,
                50047
        ]
      }

汇总查询:

> db.gameResult.aggregate([
{
    $match : {gameIndex : 1 ,home : "a0"}
},
{
    $lookup:
        {
        from: "pitcher",
        localField : "awayPitcherList",
        foreignField : "index",
        as: "awayPitcherList"
        }
},
{
    $lookup:
        {
        from: "pitcher",
        localField : "homePitcherList",
        foreignField : "index",
        as: "homePitcherList"
        }
}
]).pretty()

最后期望输出:

"_id" : ObjectId("59b22b7dac48252e6c7eeaf6"),
"seasonIndex" : 2017251,
"gameIndex" : 1,
"away" : "a9",
"home" : "a0",
"awayScore" : 9,
"homeScore" : 4,

"awayPitcherList" : [
    {
    "name" : "test50180",
    "gameRecord" : [
        {
                "seasonIndex" : 2017251,
                "gameIndex" : 1,
                "ERA" : 3.00,
        }
]
],
"homePitcherList" : [
             {
    "name" : "test50000",
    "gameRecord" : [
        {
                "seasonIndex" : 2017251,
                "gameIndex" : 1,
                "ERA" : 3.00,
        }
],
             {
    "name" : "test50049",
    "gameRecord" : [
        {
                "seasonIndex" : 2017251,
                "gameIndex" : 1,
                "ERA" : 3.00,
        }
],
             {
    "name" : "test50048",
    "gameRecord" : [
        {
                "seasonIndex" : 2017251,
                "gameIndex" : 1,
                "ERA" : 3.00,
        }
],
            {
    "name" : "test50047",
    "gameRecord" : [
        {
                "seasonIndex" : 2017251,
                "gameIndex" : 1,
                "ERA" : 3.00,
        }
]
]

我想要名称和gameRecord,其中包含(在这种情况下)1的gameIndex。

请改进我的汇总查询。 如果你有一个,那么Spring代码有很多tnx。

1 个答案:

答案 0 :(得分:4)

您可以在3.4。

中使用以下查询

以下查询使用$addFields覆盖现有的awayPitcherList,其中包含awayPitcherListname的更新gameRecord

$map阶段保持name字段和$filter过滤gameRecord以仅保留匹配的gameIndex元素。

homePitcherList的类似汇总。

db.gameResult.aggregate(
[
  {
    "$match": {
      "gameIndex": 1,
      "home": "a0"
    }
  },
  {
    "$lookup": {
      "from": "pitcher",
      "localField": "awayPitcherList",
      "foreignField": "index",
      "as": "awayPitcherList"
    }
  },
  {
    "$addFields": {
      "awayPitcherList": {
        "$map": {
          "input": "$awayPitcherList",
          "as": "awayPitcher",
          "in": {
            "name": "$$awayPitcher.name",
            "gameRecord": {
              "$filter": {
                "input": "$$awayPitcher.gameRecord",
                "as": "gameRecord",
                "cond": {
                  "$eq": [
                    "$$gameRecord.gameIndex",
                    1
                  ]
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "$lookup": {
      "from": "pitcher",
      "localField": "homePitcherList",
      "foreignField": "index",
      "as": "homePitcherList"
    }
  },
  {
    "$addFields": {
      "homePitcherList": {
        "$map": {
          "input": "$homePitcherList",
          "as": "homePitcher",
          "in": {
            "name": "$$homePitcher.name",
            "gameRecord": {
              "$filter": {
                "input": "$$homePitcher.gameRecord",
                "as": "gameRecord",
                "cond": {
                  "$eq": [
                    "$$gameRecord.gameIndex",
                    1
                  ]
                }
              }
            }
          }
        }
      }
    }
  }
])

使用以下聚合查询3.2。

 db.gameResult.aggregate(
    [
      {
        "$match": {
          "gameIndex": 1,
          "home": "a0"
        }
      },
      {
        "$lookup": {
          "from": "pitcher",
          "localField": "awayPitcherList",
          "foreignField": "index",
          "as": "awayPitcherList"
        }
      },
      {
        "$project": {
          "homePitcherList":1,
          "awayPitcherList": {
            "$map": {
              "input": "$awayPitcherList",
              "as": "awayPitcher",
              "in": {
                "name": "$$awayPitcher.name",
                "gameRecord": {
                  "$filter": {
                    "input": "$$awayPitcher.gameRecord",
                    "as": "gameRecord",
                    "cond": {
                      "$eq": [
                        "$$gameRecord.gameIndex",
                        1
                      ]
                    }
                  }
                }
              }
            }
          }
        }
      },
      {
        "$lookup": {
          "from": "pitcher",
          "localField": "homePitcherList",
          "foreignField": "index",
          "as": "homePitcherList"
        }
      },
      {
        "$project": {
          "awayPitcherList":1,
          "homePitcherList": {
            "$map": {
              "input": "$homePitcherList",
              "as": "homePitcher",
              "in": {
                "name": "$$homePitcher.name",
                "gameRecord": {
                  "$filter": {
                    "input": "$$homePitcher.gameRecord",
                    "as": "gameRecord",
                    "cond": {
                      "$eq": [
                        "$$gameRecord.gameIndex",
                        1
                      ]
                    }
                  }
                }
              }
            }
          }
        }
      }
    ])