2017-11-22 5 views
0

ビルドしようとしているMongoDB集約にいくつか問題があります。私は1週間にいくつかのゲームをプレイするのかという統計を持つテーブルを入手したいと思います。私は列と行の週と曜日のゲームでソートされたテーブルにそれを入れなければなりません。セルには、各ゲームが1日にどれだけプレイされたかのパーセンテージが含まれます(つまり、100%はコンクリートの日にプレイされたゲームの合計です)。私は、あるゲームが一週間で演奏されたトータルタイムで並べ替えられた行を持っていたいと思っています。 final tableMongoDB集約のある部分から別の部分に値を渡して結果をソートする

私は割合をカウントする方法についてdicklessから素晴らしいアドバイスを得ました:これは、私はそれが最後に見てみたい方法ですMongoDB aggregation - how to get a percentage value of how many times an event occurred per day of week私の現在の集約は、次のようになります

db.games.aggregate([ 
    { $project: { 
     "_id": 0, 
     "date" : { $dayOfWeek: "$date" }, 
     "title": "$title" 

    } }, 

    { $group: { 
     "_id": { "title": "$title", "date": "$date" }, 
     "total": { $sum: 1 } 

    } }, 

    { $group: { 
     "_id": "$_id.date", 
     "types": { $push: { "title": "$_id.title", total: "$total" } }, 
     "grandTotal": { $sum: "$total" } 

    } }, 

    { $unwind: "$types"}, 

    { $project: { 
     "_id": 0, 
     "title": "$types.title", 
     "percentage": { $divide: [ "$types.total", "$grandTotal" ] }, 
     "day": { $arrayElemAt: [ [ "0", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ], "$_id" ] } 

    } }, 

    { $group: { 
     "_id": "$title", 
     "days": {$push: {"day":"$day", "percentage": "$percentage"} } 

    } } 
]) 

これはそれから取得JSONイムです:

/* 1 */ 
{ 
    "_id" : "Bomberman", 
    "days" : [ 
     { 
      "day" : "Tue", 
      "percentage" : 0.2 
     }, 
     { 
      "day" : "Thu", 
      "percentage" : 0.14285714285714285 
     }, 
     { 
      "day" : "Wed", 
      "percentage" : 0.09090909090909091 
     }, 
     { 
      "day" : "Fri", 
      "percentage" : 0.08333333333333333 
     } 
    ] 
}, 

/* 2 */ 
{ 
    "_id" : "GTA", 
    "days" : [ 
     { 
      "day" : "Tue", 
      "percentage" : 0.4 
     }, 
     { 
      "day" : "Mon", 
      "percentage" : 0.375 
     }, 
     { 
      "day" : "Thu", 
      "percentage" : 0.2857142857142857 
     }, 
     { 
      "day" : "Wed", 
      "percentage" : 0.2727272727272727 
     }, 
     { 
      "day" : "Fri", 
      "percentage" : 0.3333333333333333 
     } 
    ] 
}, 

/* 3 */ 
{ 
    "_id" : "Forza", 
    "days" : [ 
     { 
      "day" : "Tue", 
      "percentage" : 0.1 
     }, 
     { 
      "day" : "Mon", 
      "percentage" : 0.25 
     }, 
     { 
      "day" : "Thu", 
      "percentage" : 0.14285714285714285 
     }, 
     { 
      "day" : "Wed", 
      "percentage" : 0.18181818181818182 
     }, 
     { 
      "day" : "Fri", 
      "percentage" : 0.25 
     } 
    ] 
}, 

/* 4 */ 
{ 
    "_id" : "Pacman", 
    "days" : [ 
     { 
      "day" : "Tue", 
      "percentage" : 0.1 
     }, 
     { 
      "day" : "Mon", 
      "percentage" : 0.125 
     }, 
     { 
      "day" : "Thu", 
      "percentage" : 0.14285714285714285 
     }, 
     { 
      "day" : "Wed", 
      "percentage" : 0.18181818181818182 
     }, 
     { 
      "day" : "Fri", 
      "percentage" : 0.08333333333333333 
     } 
    ] 
}, 

/* 5 */ 
{ 
    "_id" : "BattleField", 
    "days" : [ 
     { 
      "day" : "Tue", 
      "percentage" : 0.2 
     }, 
     { 
      "day" : "Mon", 
      "percentage" : 0.25 
     }, 
     { 
      "day" : "Thu", 
      "percentage" : 0.2857142857142857 
     }, 
     { 
      "day" : "Wed", 
      "percentage" : 0.2727272727272727 
     }, 
     { 
      "day" : "Fri", 
      "percentage" : 0.25 
     } 
    ] 
} 
/* 1 */ 
{ 
    "_id" : "GTA", 
    "days" : [ 
     { 
      "day" : "Mon", 
      "percentage" : 0.375 
     }, 
     { 
      "day" : "Tue", 
      "percentage" : 0.4 
     }, 
     { 
      "day" : "Wed", 
      "percentage" : 0.2727272727272727 
     }, 
     { 
      "day" : "Thu", 
      "percentage" : 0.2857142857142857 
     }, 
     { 
      "day" : "Fri", 
      "percentage" : 0.3333333333333333 
     } 
    ] 
}, 

/* 2 */ 
{ 
    "_id" : "BattleField", 
    "days" : [ 
     { 
      "day" : "Mon", 
      "percentage" : 0.25 
     }, 
     { 
      "day" : "Tue", 
      "percentage" : 0.2 
     }, 
     { 
      "day" : "Wed", 
      "percentage" : 0.2727272727272727 
     }, 
     { 
      "day" : "Thu", 
      "percentage" : 0.2857142857142857 
     }, 
     { 
      "day" : "Fri", 
      "percentage" : 0.25 
     } 
    ] 
} 

/* 3 */ 
{ 
    "_id" : "Forza", 
    "days" : [ 
     { 
      "day" : "Mon", 
      "percentage" : 0.25 
     }, 
     { 
      "day" : "Tue", 
      "percentage" : 0.1 
     }, 
     { 
      "day" : "Wed", 
      "percentage" : 0.18181818181818182 
     }, 
     { 
      "day" : "Thu", 
      "percentage" : 0.14285714285714285 
     }, 
     { 
      "day" : "Fri", 
      "percentage" : 0.25 
     } 
    ] 
}, 

/* 4 */ 
{ 
    "_id" : "Pacman", 
    "days" : [ 
     { 
      "day" : "Mon", 
      "percentage" : 0.125 
     }, 
     { 
      "day" : "Tue", 
      "percentage" : 0.1 
     }, 
     { 
      "day" : "Wed", 
      "percentage" : 0.18181818181818182 
     }, 
     { 
      "day" : "Thu", 
      "percentage" : 0.14285714285714285 
     }, 
     { 
      "day" : "Fri", 
      "percentage" : 0.08333333333333333 
     } 
    ] 
}, 

/* 5 */ 
{ 
    "_id" : "Bomberman", 
    "days" : [ 
     { 
      "day" : "Tue", 
      "percentage" : 0.2 
     }, 
     { 
      "day" : "Wed", 
      "percentage" : 0.09090909090909091 
     }, 
     { 
      "day" : "Thu", 
      "percentage" : 0.14285714285714285 
     }, 
     { 
      "day" : "Fri", 
      "percentage" : 0.08333333333333333 
     } 
    ] 
}, 

DBへのデータイム送り:

[ 
{"title":"GTA","date":"2017-11-13"}, 
{"title":"GTA","date":"2017-11-13"}, 
{"title":"GTA","date":"2017-11-13"}, 
{"title":"Pacman","date":"2017-11-13"}, 
{"title":"BattleField","date":"2017-11-13"}, 
{"title":"BattleField","date":"2017-11-13"}, 
{"title":"Forza","date":"2017-11-13"}, 
{"title":"Forza","date":"2017-11-13"}, 
{"title":"GTA","date":"2017-11-14"}, 
{"title":"GTA","date":"2017-11-14"}, 
{"title":"GTA","date":"2017-11-14"}, 
{"title":"GTA","date":"2017-11-14"}, 
{"title":"BattleField","date":"2017-11-14"}, 
{"title":"BattleField","date":"2017-11-14"}, 
{"title":"Forza","date":"2017-11-14"}, 
{"title":"Pacman","date":"2017-11-14"}, 
{"title":"Bomberman","date":"2017-11-14"}, 
{"title":"Bomberman","date":"2017-11-14"}, 
{"title":"GTA","date":"2017-11-15"}, 
{"title":"GTA","date":"2017-11-15"}, 
{"title":"GTA","date":"2017-11-15"}, 
{"title":"BattleField","date":"2017-11-15"}, 
{"title":"BattleField","date":"2017-11-15"}, 
{"title":"BattleField","date":"2017-11-15"}, 
{"title":"Forza","date":"2017-11-15"}, 
{"title":"Forza","date":"2017-11-15"}, 
{"title":"Pacman","date":"2017-11-15"}, 
{"title":"Pacman","date":"2017-11-15"}, 
{"title":"Bomberman","date":"2017-11-15"}, 
{"title":"GTA","date":"2017-11-16"}, 
{"title":"GTA","date":"2017-11-16"}, 
{"title":"BattleField","date":"2017-11-16"}, 
{"title":"BattleField","date":"2017-11-16"}, 
{"title":"Forza","date":"2017-11-16"}, 
{"title":"Bomberman","date":"2017-11-16"}, 
{"title":"Pacman","date":"2017-11-16"}, 
{"title":"GTA","date":"2017-11-17"}, 
{"title":"GTA","date":"2017-11-17"}, 
{"title":"GTA","date":"2017-11-17"}, 
{"title":"GTA","date":"2017-11-17"}, 
{"title":"BattleField","date":"2017-11-17"}, 
{"title":"BattleField","date":"2017-11-17"}, 
{"title":"BattleField","date":"2017-11-17"}, 
{"title":"Forza","date":"2017-11-17"}, 
{"title":"Forza","date":"2017-11-17"}, 
{"title":"Forza","date":"2017-11-17"}, 
{"title":"Bomberman","date":"2017-11-17"}, 
{"title":"Pacman","date":"2017-11-17"} 
] 

これは(少なくともプレイしたゲームに、合計で最もプレイしたゲームから選別)を取得しようとしているイムJSONであります今私の質問は、週に一度プレイしたゲームで行を並べ替えるにはどうすればいいですか?サブ集計やサブパイプラインを作成する必要があると思います。ゲームごとの合計プレイ数をカウントし、集計の最後にこの結果を渡して並べ替えることはできますが、これを行う方法は見つけられませんでした。

私は本当にMongoDBのnoobですので、これについてのアドバイスをありがとうと思います!

+0

うちuが期待置かれるものにJSONを追加しますか?質問 –

+0

助言をいただきありがとうございます。iveは現在取得中のimの下に期待されるJSONを追加しました – davidM

+1

最後の予測で '' $ types.total "'を公開し、次のグループで合計し、後でソートステージを追加します。 –

答えて

0

私に正しい道を指し示すためのアレックスBlexのおかげで、これは上記の質問を解決する:

db.games.aggregate([ 
    { $project: { 
     "_id": 0, 
     "date" : { $dayOfWeek: "$date" }, 
     "title": "$title" 

    } }, 

    { $group: { 
     "_id": { "title": "$title", "date": "$date" }, 
     "total": { $sum: 1 } 

    } }, 

    { $group: { 
     "_id": "$_id.date", 
     "types": { $push: { "title": "$_id.title", total: "$total" } }, 
     "grandTotal": { $sum: "$total" } 

    } }, 

    { $sort: { 
     "_id": 1 

    } }, // This sorts the arrays with days from Monday to Sunday 

    { $unwind: "$types"}, 

    { $project: { 
     "_id": 0, 
     "title": "$types.title", 
     "percentage": { $divide: [ "$types.total", "$grandTotal" ] }, 
     "day": { $arrayElemAt: [ [ "0", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ], "$_id" ] }, 
     "supertotal" : "$types.total" // This exposes the total numbers of times a game was played per particular dayOfWeek 
    } }, 

    { $group: { 
     "_id": "$title", 
     "days": {$push: {"day":"$day", "percentage": "$percentage"} }, 
     "supertotal" : {$sum: "$supertotal"} // This sums the total numbers of times a game was played in a week 

    } }, 

    { $sort: { 
     "supertotal": -1 
    } } // This sorts the documents from the most played game 
]) 

決勝JSON:

/* 1 */ 
{ 
    "_id" : "GTA", 
    "days" : [ 
     { 
      "day" : "Mon", 
      "percentage" : 0.375 
     }, 
     { 
      "day" : "Tue", 
      "percentage" : 0.4 
     }, 
     { 
      "day" : "Wed", 
      "percentage" : 0.2727272727272727 
     }, 
     { 
      "day" : "Thu", 
      "percentage" : 0.2857142857142857 
     }, 
     { 
      "day" : "Fri", 
      "percentage" : 0.3333333333333333 
     } 
    ], 
    "supertotal" : 16.0 
}, 

/* 2 */ 
{ 
    "_id" : "BattleField", 
    "days" : [ 
     { 
      "day" : "Mon", 
      "percentage" : 0.25 
     }, 
     { 
      "day" : "Tue", 
      "percentage" : 0.2 
     }, 
     { 
      "day" : "Wed", 
      "percentage" : 0.2727272727272727 
     }, 
     { 
      "day" : "Thu", 
      "percentage" : 0.2857142857142857 
     }, 
     { 
      "day" : "Fri", 
      "percentage" : 0.25 
     } 
    ], 
    "supertotal" : 12.0 
}, 

/* 3 */ 
{ 
    "_id" : "Forza", 
    "days" : [ 
     { 
      "day" : "Mon", 
      "percentage" : 0.25 
     }, 
     { 
      "day" : "Tue", 
      "percentage" : 0.1 
     }, 
     { 
      "day" : "Wed", 
      "percentage" : 0.18181818181818182 
     }, 
     { 
      "day" : "Thu", 
      "percentage" : 0.14285714285714285 
     }, 
     { 
      "day" : "Fri", 
      "percentage" : 0.25 
     } 
    ], 
    "supertotal" : 9.0 
}, 

/* 4 */ 
{ 
    "_id" : "Pacman", 
    "days" : [ 
     { 
      "day" : "Mon", 
      "percentage" : 0.125 
     }, 
     { 
      "day" : "Tue", 
      "percentage" : 0.1 
     }, 
     { 
      "day" : "Wed", 
      "percentage" : 0.18181818181818182 
     }, 
     { 
      "day" : "Thu", 
      "percentage" : 0.14285714285714285 
     }, 
     { 
      "day" : "Fri", 
      "percentage" : 0.08333333333333333 
     } 
    ], 
    "supertotal" : 6.0 
}, 

/* 5 */ 
{ 
    "_id" : "Bomberman", 
    "days" : [ 
     { 
      "day" : "Tue", 
      "percentage" : 0.2 
     }, 
     { 
      "day" : "Wed", 
      "percentage" : 0.09090909090909091 
     }, 
     { 
      "day" : "Thu", 
      "percentage" : 0.14285714285714285 
     }, 
     { 
      "day" : "Fri", 
      "percentage" : 0.08333333333333333 
     } 
    ], 
    "supertotal" : 5.0 
} 
関連する問題