【问题标题】:mongodb - how to write conditionals within an aggregate querymongodb - 如何在聚合查询中编写条件
【发布时间】:2017-08-14 13:44:29
【问题描述】:

我是 Mongo 的新手,并且有一个关于使用条件计算聚合查询的问题:

我有一个评论集合,每个文档都包含一个情绪分数。我想:

1) 按项目对评论进行分组

2) 获取该项目所有评论中每个项目的平均情绪得分,并按此排序

3) 获取每个项目组的总评论数

4) 获取每个项目的正面情绪评论总数(例如,情绪得分 > 75 的评论数)

5) 获取每个项目的负面情绪评论的总数(例如,情绪得分

到目前为止,我有以下涵盖 1-3 的查询,但不确定如何在这里获得 4/5:

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

【问题讨论】:

  • 您需要提供样本文件
  • 嗨 Januka,文档中唯一重要的字段是代码中显示的字段 - “sentimentScore”是 0 到 100 之间的 #,“item”是项目的名称。我想要做的就是添加到我拥有的代码中,以便为每组项目返回 2 个新字段,一个是该组中情感评分 75

标签: mongodb mongodb-query


【解决方案1】:

我假设您希望为 sentiment 设置单独的 count 字段,具有给定阈值的负值和正值,即positive - >75negative - <75,即积极情绪的总数和消极情绪的总数连同总的情绪。

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 }

【讨论】:

  • 谢谢!效果很好。
猜你喜欢
  • 1970-01-01
  • 2019-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多