0

にカウント:グループによってこれは私のデータベースで約10万行のうち一つでありアレイMongoDBの

{ 
    "_id" : ObjectId("58f569159f809c49ffc5cdbb"), 
    "date" : ISODate("2017-03-21T09:10:07.686Z"), 
    "p1" : { 
     "_id" : 1765906, 
     "deck" : [ 
      33, 
      25, 
      59, 
      38, 
      62, 
      3, 
      33, 
      57 
     ], 
     "crowns" : 3 
    }, 
    "p2" : { 
     "_id" : 2520156, 
     "deck" : [ 
      25, 
      69, 
      86, 
      8, 
      44, 
      61, 
      69, 
      50 
     ], 
     "crowns" : 2 
    } 
} 

それはゲームの戦いのログです。デッキは8種類のカードからなる配列です。最も勝っているカードを見つけようとしています。クラウンを比較することで勝者を決定することができます(私は、プレーヤー1が勝ったすべての文書を選択することができました)。

私が達成しようとしています何

  1. は、残念ながら、私は私が(ほとんどの勝利のカード)を探しています何を返していましグループクエリを実行することができませんでした。

  2. 私はまた、最も成功したデッキ(勝利の量が十分である - その配列で指定されたカードの順番は無視されるべきである)を見つけようとしています。私は勝利のその量(のみplayer1勝利を考慮)

    によってグループ化されたすべてのデッキの組み合わせを取得することが期待

    db.battle_logs.aggregate([ 
        { 
         $addFields: { 
          "player1won": { "$cmp": [ "$p1.crowns", "$p2.crowns" ] } 
         } 
        }, 
        { $match: { "player1won": 1 }}, 
        { 
         $group: 
         { 
          _id: "$p1.deck", 
          count: { $sum: 1 } 
         } 
        } 
    ], { 
        allowDiskUse:true, 
        cursor:{} 
    }) 
    

    :私がしようとしたが、いくつかの時間後に、空のエラーを返してきた何

+0

ドキュメントのために期待される出力を追加することができ、それは、非常に明確ではありませんあなたは提供しましたか? – felix

+0

@felix 2)これはデッキ(intからなる配列)で、勝利の回数/カウントとなります(私はplayer1Wonを既に指定されたクエリでフィルタリングしていますので、デッキでグループ化してカウントするだけですそれは結果です)。私は、カードの順序を無視するようにint配列(p1.deck)をソートする必要があると仮定します – kentor

答えて

0

グループクエリで配列p1.deck_idとして使用しているので、配列をソートする必要があります。そうしないと、カードの順序によって新しいグループが作成されます。ここで

は、あなたのケースのために働くかもしれない何かである:

1)

db.battle_logs.aggregate([ 
    { 
     $addFields: { 
      "player1won": { "$cmp": [ "$p1.crowns", "$p2.crowns" ] } 
     } 
    }, 
    //Fiter out ties 
    { 
     $match: { 
      "$or" : [ 
       {"player1won": 1}, //Player 1 won 
       {"player1won" : -1} //Player 2 won 
      ] 
     } 
    }, 
    // Get the winning deck 
    { 
     $project: { 
      "deck": { $cond : [ { $gte : [ "$p1.crowns", "$p2.crowns" ]}, "$p1.deck", "$p2.deck" ]} 
     } 
    }, 
    //Unwind the deck to get every card 
    { 
     $unwind : "$deck" 
    }, 
    //Get count of each card 
    { 
     $group : { 
      "_id" : "$deck" , 
      "count" : {"$sum" : 1} 
     } 
    }, 
    // Sort on count 
    { 
     $sort: {"count" : -1} 

    }, 
    //Get the card with highest count 
    { 
     $limit: 1 
    } 
], { 
    allowDiskUse:true, 
    cursor:{} 
}) 

2)

db.battle_logs.aggregate([ 
    { 
     $addFields: { 
      "player1won": { "$cmp": [ "$p1.crowns", "$p2.crowns" ] } 
     } 
    }, 
    //Fiter out ties 
    { 
     $match: { 
      "$or" : [ 
       {"player1won": 1}, //Player 1 won 
       {"player1won" : -1} //Player 2 won 
      ] 
     } 
    }, 
    // Get the winning deck 
    { 
     $project: { 
      "deck": { $cond : [ { $gte : [ "$p1.crowns", "$p2.crowns" ]}, "$p1.deck", "$p2.deck" ]} 
     } 
    }, 
    //Unwind the deck to get every card 
    { 
     $unwind : "$deck" 
    }, 
    //Sort the cards 
    { 
     $sort : {"deck": 1} 
    }, 
    //Group sorted cards back in deck 
    { 
     $group : { 
      "_id" : "$_id" , 
      "deck" : {"$push" : "$deck"} 
     } 
    }, 
    { 
     $group: 
     { 
      _id: "$deck", 
      count: { $sum: 1 } 
     } 
    } 
], { 
    allowDiskUse:true, 
    cursor:{} 
}) 
関連する問題