繁体   English   中英

MongoDB - 如何展开数组的 object

[英]MongoDB - How to unwind object of array

我正在尝试操作数据集以使其易于在 mongoCharts 中显示。 有两组team_a和team_b,每组包含一组playerId、score、rank、prize。 我想展开 Object 内的数组,还想将两个 arrays 合并为一个。

样本文件:

{
  team_a: {
    team_score: 94,
    team_name: "team_1",
    players: [
      {
        id: "604f00d43776e45a448628f9",
        username: "test_1",
        score: "33",
        rank: "1",
        prize: 15.4,
      },
      {
        id: "60058dd9b88cc1a1e40f2f54",
        username: "test_2",
        score: "31",
        rank: "2",
        prize: 15.4,
      },
      {
        id: "60058dd9b88cc1a1e40f2f55",
        username: "test_3",
        score: "30",
        rank: "3",
        prize: 15.4,
      }
    ],
  },
  team_b: {
    team_score: 62,
    team_name: "team_2",
    players: [
      {
        id: "602ce34a39c7496600940774",
        username: "test_4",
        score: "32",
        rank: "1",
      },
      {
        id: "60058db6b88cc1a1e40f2f4f",
        username: "test_5",
        score: "30",
        rank: "2",
      },
    ],
  },
}

而所需的 output 是:

{
    team_a: [
      {
          username: "test_1",
          score: "33",
          rank: "1",
          prize: 15.4,
      },
      {
          username: "test_2",
          score: "31",
          rank: "2",
          prize: 15.4,
       },
       {
         username: "test_3",
         score: "30",
         rank: "3",
         prize: 15.4,
      }
    ],
    team_b: [
      {
         username: "test_4",
         score: "32",
         rank: "1",
      },
      {
        username: "test_5",
        score: "30",
        rank: "2",
       },
    ],
    all_winners: [
        {
          username: "test_1",
          score: "33",
          rank: "1",
          prize: 15.4,
        },
        {
          username: "test_2",
          score: "31",
          rank: "2",
          prize: 15.4,
        },
        {
          username: "test_3",
          score: "30",
          rank: "3",
          prize: 15.4,
        }
        {
          username: "test_4",
          score: "31",
          rank: "4",
        },
        {
          username: "test_5",
          score: "30",
          rank: "5",
        },
    ]
}

非常感谢收到任何指导或指示。 谢谢。

解决方案 1

第一$project阶段:

  1. 带有team_a.playersteam_a字段
  2. 带有team_b.playersteam_b字段
  3. 带有$concatArraysall_winners字段用于team_a.playersteam_b.players

第二个$project阶段:

  • team_ateam_ball_winners数组字段中删除id字段。
db.collection.aggregate([
  {
    $project: {
      "team_a": "$team_a.players",
      "team_b": "$team_b.players",
      "all_winners": {
        "$concatArrays": [
          "$team_a.players",
          "$team_b.players"
        ]
      }
    }
  },
  {
    $project: {
      "all_winners": {
        id: 0
      },
      "team_a": {
        id: 0
      },
      "team_b": {
        id: 0
      }
    }
  }
])

示例 Mongo 游乐场(解决方案 1)


方案二

或者,使用$unset删除team_a.players.idteam_b.players.id作为第一阶段。

db.collection.aggregate([
  {
    $unset: [
      "team_a.players.id",
      "team_b.players.id"
    ]
  },
  {
    $project: {
      "team_a": "$team_a.players",
      "team_b": "$team_b.players",
      "all_winners": {
        "$concatArrays": [
          "$team_a.players",
          "$team_b.players"
        ]
      }
    }
  }
])

示例 Mongo 游乐场(解决方案 2)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM