【问题标题】:Why is my results array returning as empty in this mongo query?为什么我的结果数组在这个 mongo 查询中返回为空?
【发布时间】:2021-09-22 09:55:09
【问题描述】:

这是我的查询我是 Mongo 的新手,所以我有点摸索这个查询。我的目标是让球员的队友得到供应。我将显示查询,然后显示播放器的文档。

db.Players.aggregate([{
    $match: {_id: "/players/c/cruzne02.shtml"}}, 
    {$unwind: "$teams"},
    {$unwind: "$teams.years"}, 
    {$lookup: {
        from: "Players",
        let: {team_name: "$teams.name", team_year: "$teams.years"},
        pipeline: [{
            $match: {
                $expr: {
                    $and: [
                        {$eq: ["$teams.name", "$$team_name"]},
                        {$eq: ["$teams.years", "$$team_year"]},
                    ]
                }
            },
        }],
        as: "results"
    }},
    {$unwind: {
        path: "$results",
        preserveNullAndEmptyArrays: true
    }}, 
    {$group: {
        _id: {
            team: "$teams.name",
            year: "$teams.years"
        },
        results: {
            $push: "$results"
        }
    }}, 
    {$project: {
        team: "$_id.team",
        year: "$_id.year",
        results: 1,
        _id: 0
    }}
]);
{
    "_id": "/players/c/cruzne02.shtml",
    "url": "/players/c/cruzne02.shtml",
    "name": "Nelson Cruz",
    "image": "https://www.baseball-reference.com/req/202108020/images/headshots/f/fea2f131_mlbam.jpg",
    "teams": [{
        "name": "MIL",
        "years": [2005]
    }, {
        "name": "TEX",
        "years": [2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013]
    }, {
        "name": "BAL",
        "years": [2014]
    }, {
        "name": "SEA",
        "years": [2015, 2016, 2017, 2018]
    }, {
        "name": "MIN",
        "years": [2019, 2020, 2021]
    }, {
        "name": "TBR",
        "years": [2021]
    }]
}

我可以取回该组,但结果数组始终为空。团队和年份排成一列,但结果永远不会出现。 我当前的结果有空数组我需要更改什么。

编辑:这是我的结果

[ { results: [], team: 'MIN', year: 2020 },
  { results: [], team: 'TEX', year: 2006 },
  { results: [], team: 'MIL', year: 2005 },
  { results: [], team: 'TEX', year: 2008 },
  { results: [], team: 'TBR', year: 2021 },
  { results: [], team: 'SEA', year: 2018 },
  { results: [], team: 'TEX', year: 2007 },
  { results: [], team: 'MIN', year: 2019 },
  { results: [], team: 'SEA', year: 2017 },
  { results: [], team: 'TEX', year: 2013 },
  { results: [], team: 'TEX', year: 2011 },
  { results: [], team: 'TEX', year: 2012 },
  { results: [], team: 'SEA', year: 2016 },
  { results: [], team: 'TEX', year: 2010 },
  { results: [], team: 'SEA', year: 2015 },
  { results: [], team: 'BAL', year: 2014 },
  { results: [], team: 'MIN', year: 2021 },
  { results: [], team: 'TEX', year: 2009 } ]

【问题讨论】:

  • 请提供一些示例数据
  • Show 向我们展示一些输入数据。我怀疑你需要所有这些 $unwind$group
  • 我正在尝试在聚合的 match 部分中获取具有 id 的玩家的所有队友。我想返回一个包含文档数组的对象数组,用于特定的团队名称和年份。所以我希望所有 team.name 等于 MIL 和 team.years 等于 2005 的球员。在给定球员中的每一年都这样做。
  • 在 $lookup 阶段,"$teams.name" 将是一个数组,而 $eq 永远不会是字符串,$teams.years 将是整数数组,永远不会是 @ 987654329@ 一个整数。如果我以后有时间,我会看看我是否能想到一个解决方案。
  • 这是我的结果 [ { results: [], team: 'MIN', year: 2020 }, { results: [], team: 'TEX', year: 2006 }, { results: [], team: 'MIL', year: 2005 }, { results: [], team: 'TEX', year: 2008 }, { results: [], team: 'TBR', year: 2021 }, { results: [], team: 'SEA', year: 2018 }, { results: [], team: 'TEX', year: 2007 }, { results: [], team: 'MIN', year: 2019 }, { results: [], team: 'SEA', year: 2017 }, { results: [], team: 'TEX', year: 2013 }, { results: [], team: 'TEX', year: 2011 }, ]

标签: mongodb mongodb-query


【解决方案1】:

$match放在聚合的末尾。

db.collection.aggregate([
  {
    $unwind: "$teams"
  },
  {
    $unwind: "$teams.years"
  },
  {
    $group: {
      _id: {
        team: "$teams.name",
        year: "$teams.years",
        
      },
      results: {
        $push: {
          "name": "$name",
          "id": "$_id"
        }
      }
    }
  },
  {
    $match: {
      "results.id": "/players/c/cruzne02.shtml"
    }
  }
])

mongoplayground

【讨论】:

  • 这很接近,但我添加了另一个与该播放器重叠的播放器,应该出现在 "MIN":2020"MIN":2021 mongoplayground
  • 我更新我的答案
  • 我提供了$match,因为我想获取具有该 ID 的特定玩家的每个队友,而不是整个数据库中的每个团队,这就是上面的查询所做的。
  • 我再次更新我的答案,将匹配放在聚合末尾
猜你喜欢
  • 2019-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-24
  • 1970-01-01
相关资源
最近更新 更多