首页 > 解决方案 > 在聚合 mongodb 中使用 $lookup

问题描述

已编辑

我现在试图回到我原来的问题。这是我正在做的事情:

var sortedList = Spark.runtimeCollection('matchResults').aggregate(
    {$match: { "playerId": jsonObject.playerId, "leadId": jsonObject.leadId}},
    {$lookup:
       {
         from: "matches",
         localField: "group",
         foreignField: "groupId",
         as: "matchDocs"
       }
    },
    {$project:{ 
        type: "$SortBoard",
        points: { $sum: { $add : [ '$data.leadTotals.stfBonus', '$data.leadTotals.placePoints', '$data.leadTotals.startPoints' ] }},
        matchId: "$matchId",
        matchDocuments: "$matchDocs"
    }},
    {$sort:{"points":-1}}
);

这不会产生任何错误,但 $lookup 不会返回任何内容!?

这是我的回报:

{
  "@class": ".LogEventResponse",
  "scriptData": {
    "@seasonScoresReturn": {
      "scorecards": [
        {
          "_id": {
            "$oid": "5b166c87031f5bc44d50f0ed"
          },
          "matchId": "5b165cba5399c020e3f18dbd",
          "points": 24,
          "matchDocuments": []
        },
        {
          "_id": {
            "$oid": "5b1687b3f798b10f36abae92"
          },
          "matchId": "5b165cba5399c020e3f18dbd",
          "points": 16,
          "matchDocuments": []
        }
      ]
    }
  }
}

我的比赛文件如下所示:

{
  "_id": {
    "$oid": "5b165cba5399c020e3f18dbd"
  },
  "groupId": "5b15958aa2f56b04fe84db2d",
  "updated": {
    "$date": {
      "$numberLong": "1528192186776"
    }
  },
  "data": {
    "type": "MatchData",
    "matchId": "5b165cba5399c020e3f18dbd",
    "groupId": "5b15958aa2f56b04fe84db2d",
    "leaderboardId": "5b165ca15399c020e3f17a75",
    "matchTitle": "",
    "rounds": [],
    "joined": [],
    "finished": true,
    "year": 2018,
    "tieMethod": 0,
    "methodType": 2,
    "pointValue": 1,
    "placeValue": 12,
    "startValue": 0,
    "pointLimit": 25,
    "bosLimit": 25
  }
}

我的 matchResults 文档如下所示:

{
  "_id": {
    "$oid": "5b18e4f7031f5bc44d513fc6"
  },
  "matchId": "5b18e4ada2f56b04fea71fac",
  "playerId": "5b18e305031f5bc44d513fb0",
  "groupId": "5b18e3ada2f56b04fea62139",
  "leadId": "5b18e44ea2f56b04fea6ca45",
  "data": {
    "type": "MatchResult",
    "playerId": "5b18e305031f5bc44d513fb0",
    "rounds": 1,
    "leadTotals": {
      "type": "TotalLeadPoints",
      "stfBonus": 11,
      "placePoints": 12,
      "startPoints": 0,
      "bosPoints": 0
    },
    "scoreTotals": {
      "type": "TotalHoleData",
      "strokes": 76,
      "strokesNet": 72,
      "points": 36,
      "modPoints": 0,
      "toPar": 0,
      "puts": 0,
      "shorts": 0,
      "bx2": 0,
      "gir": 0,
      "hit": 0,
      "sand": 0,
      "outs": 0,
      "ladies": 0,
      "aces": 0,
      "albatros": 0,
      "eagles": 0,
      "birdies": 0,
      "pars": 14,
      "bogeys": 4,
      "bogeysx2": 0,
      "bogeysx3": 0,
      "put0": 18,
      "put1": 0,
      "put2": 0,
      "put3": 0,
      "put4": 0,
      "scramples": 18,
      "last9": 0,
      "last6": 0,
      "last3": 0,
      "last2": 0,
      "last1": 0
    }
  }
}

我现在的问题是......我如何只从匹配项中获取特定文档($match:matchid)。我在文档中没有看到有关如何完成此操作的示例。

另外......为什么我得到一个空数组?

希望有人可以帮助我解决这个问题并提前感谢:-)

标签: mongodbnestedaggregate

解决方案


推荐阅读