2017-08-14 6 views
0

以内に条件文を書き、条件付き計算で集計クエリに関連する質問持ってする方法:のMongoDB - 私はMongoのに非常に新しいです集計クエリ

私はレビューのコレクションを持っているが、各文書は感情のスコアが含まれています。私がしたい:

1)項目別グループレビュー

2)は、そのアイテムのすべてのレビューにわたって各項目の平均感情のスコアを取得し、この

3でソート)のレビューの総数を取得します。各項目グループ

4)(各項目のために例えば、感情得点と#レビュー> 75)

5)各項目の負の感情のレビューの総数を取得します(例えば、#をポジティブ感情のレビューの総数を取得します。感情スコアのレビュー75)

これまでのところ、私は以下の1-3をカバーしたクエリが、同様に、ここでは4/5を取得する方法がわからない持っている:

db.reviews.aggregate( 
    {"$group" : 
     {_id: "$item", 
     sentiment: {$avg : "$sentimentScore"}, 
     count: {$sum: 1 } 
     } 
    }, 
    {"$sort": { sentiment: -1 } } 
) 
+0

あなたがサンプルドキュメントに –

+0

こんにちはJanukaを提供する必要があり、文書の唯一の重要な分野を示すものですcode - 0と100の間の#である "sentimentScore"、項目の名前である "item"。私がしたいのは、私が持っているコードに追加することです。2つの新しいフィールドがアイテムのグループごとに返されます。一つは、sentimentScore <75、> 75のグループのアイテム数です。 – jbug123

答えて

0

を、私はあなたがしたいことを想定していますpositive - >75negative - <75の合計で負の感情と総感情の合計数を合計すると、のフィールドはsentimentになります。

db.sentiments.aggregate([ 
    {"$group" : 
     {_id: "$item", 
     sentiment: {$avg : "$sentiment_score"}, 
     postiive_sentiments: {$sum: { $cond: { if: { $gt: [ "$sentiment_score", 75 ] }, then: 1, else: 0 } }}, 
     negative_sentiments: {$sum: { $cond: { if: { $lt: [ "$sentiment_score", 75 ] }, then: 1, else: 0 } }}, 
     count: {$sum: 1 } 
     } 
    }, 
    {"$sort": { sentiment: -1 } } 
]) 

サンプルデータ:

{ "_id" : ObjectId("5991329ea37dbc24842a68be"), "item" : "test1", "sentiment_score" : 50 } 
{ "_id" : ObjectId("599132a2a37dbc24842a68bf"), "item" : "test1", "sentiment_score" : 40 } 
{ "_id" : ObjectId("599132a4a37dbc24842a68c0"), "item" : "test1", "sentiment_score" : 80 } 
{ "_id" : ObjectId("599132aba37dbc24842a68c1"), "item" : "test2", "sentiment_score" : 80 } 
{ "_id" : ObjectId("599132ada37dbc24842a68c2"), "item" : "test2", "sentiment_score" : 30 } 
{ "_id" : ObjectId("599132b0a37dbc24842a68c3"), "item" : "test2", "sentiment_score" : 38 } 
{ "_id" : ObjectId("599132b6a37dbc24842a68c4"), "item" : "test3", "sentiment_score" : 78 } 
{ "_id" : ObjectId("599132b9a37dbc24842a68c5"), "item" : "test3", "sentiment_score" : 88 } 
{ "_id" : ObjectId("599132bba37dbc24842a68c6"), "item" : "test3", "sentiment_score" : 58 } 
{ "_id" : ObjectId("599132c4a37dbc24842a68c7"), "item" : "test3", "sentiment_score" : 98 } 
{ "_id" : ObjectId("599132cba37dbc24842a68c8"), "item" : "test4", "sentiment_score" : 65 } 
{ "_id" : ObjectId("599132d2a37dbc24842a68c9"), "item" : "test4", "sentiment_score" : 30 } 
{ "_id" : ObjectId("599132d6a37dbc24842a68ca"), "item" : "test4", "sentiment_score" : 10 } 

//結果:

{ "_id" : "test3", "sentiment" : 80.5, "negative_sentiments" : 3, "positive_sentiments" : 1, "count" : 4 } 
{ "_id" : "test1", "sentiment" : 56.666666666666664, "negative_sentiments" : 1, "positive_sentiments" : 2, "count" : 3 } 
{ "_id" : "test2", "sentiment" : 49.333333333333336, "negative_sentiments" : 1, "positive_sentiments" : 2, "count" : 3 } 
{ "_id" : "test4", "sentiment" : 35, "negative_sentiments" : 0, "positive_sentiments" : 3, "count" : 3 } 
+0

ありがとう!素晴らしい仕事をした。 – jbug123

関連する問題