在对象数组中的两个不同的objectid字段上执行Mongodb聚合查找

时间:2019-02-26 16:44:08

标签: mongodb aggregation-framework lookup

我有一个团队文档,看起来像这样:

_id: ...,
name: ...,
scheduleId: ...

我有一个游戏文件,看起来像这样:

_id: ...,
scheduleId: ...,
homeTeam: {
  teamId: ...
},
awayTeam: {
  teamId: ...}

我想执行mongodb聚合,以便聚合后生成的文档如下所示:

id: (Where this is the teamId of the original input document),
name: (name of the team from the input document),
schedule: [
{
  id: (objectId equivalent to scheduleId's referenced in team and games documents.
  homeTeam: {
    id: (objectId of home team),
    name: (name of home team),
    goals: ...,
    assists: ...,
    saves: ...,
    score: ...,
    winner: ...,
  },
  awayTeam: {
    id: (objectId of away team),
    name: (name of home team),
    goals: ...,
    assists: ...,
    saves: ...,
    score: ...,
    winner: ...,
  }
}

这是我现有的代码(它为每个主队和客队输出一个带有ID的游戏数组,但不包括相应团队文档中的名称。我无法弄清楚如何在ID中查找ID嵌套对象以收集每个团队的名称并将其存储在相应的homeTeam / awayTeam对象中。

Team.withUserInfo({
$match: {
  _id: mongoose.Types.ObjectId(req.params.teamId)
}})

return model.aggregate([
query,
{
  $lookup: {
    from: "users",
    localField: "captainId",
    foreignField: "_id",
    as: "captain"
  }
},
{
  $unwind: {
    path: "$captain"
  }
},
{
  $lookup: {
    from: "users",
    localField: "playerIds",
    foreignField: "_id",
    as: "players"
  }
},
{
  $lookup: {
    from: "circuits",
    localField: "circuitId",
    foreignField: "_id",
    as: "circuit"
  }
},
{
  $unwind: {
    path: "$circuit"
  }
},
{
  $lookup: {
    from: "games",
    localField: "scheduleId",
    foreignField: "scheduleId",
    as: "games"
  }
},
{
  $project: {
    _id: false,
    id: "$_id",
    active: true,
    formattedName: true,
    description: true,
    logo: true,
    highestPlayerRank: true,
    circuitId: "$circuit._id",
    scheduleId: true,
    div: true,
    maxPlayers: "$circuit.maxPlayers",
    "captain.id": "$captain._id" ,
    "captain.userName": true,
    players: {
      $map: {
        input: "$players",
        as: "player",
        in: {
          id: "$$player._id" ,
          userName: "$$player.userName"
        }
      }
    },
    schedule: {
      $map: {
        input: "$games",
        as: "game",
        in: {
          id: "$$game._id",
          homeTeam: {
            id: "$$game.homeTeam.teamId",
            goals: "$$game.homeTeam.goals",
            assists: "$$game.homeTeam.assists",
            saves: "$$game.homeTeam.saves",
            score: "$$game.homeTeam.score",
            winner: "$$game.homeTeam.winner",
          },
          awayTeam: {
            id: "$$game.awayTeam.teamId",
            goals: "$$game.awayTeam.goals",
            assists: "$$game.awayTeam.assists",
            saves: "$$game.awayTeam.saves",
            score: "$$game.awayTeam.score",
            winner: "$$game.awayTeam.winner",
          },
          week: "$$game.week",
          date: "$$game.date"
        }
      }
    }
  }
}]);

这似乎是一个相对简单的$ lookup,但我只是不知道如何完成此操作。任何帮助是极大的赞赏!如果没有任何意义,请发表评论,我会回复。

0 个答案:

没有答案