首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mongodb -如何在聚合查询中写入条件

mongodb -如何在聚合查询中写入条件
EN

Stack Overflow用户
提问于 2017-08-14 01:46:31
回答 1查看 105关注 0票数 0

我对Mongo非常陌生,我有一个关于有条件计算的聚合查询的问题:

我有一个评论集,每个文档包含一个情感评分。我想:

1)按项目分列的小组审查

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

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

4)获得每个项目的积极情绪评价总数(例如,情绪得分>75的#评价)。

5)获得每一项负面情绪评价的总数(例如,情绪得分<75的#评价)

到目前为止,我有下面的查询,它涉及1-3,但也不确定如何获得4/5:

代码语言:javascript
复制
db.reviews.aggregate( 
    {"$group" : 
        {_id: "$item", 
        sentiment: {$avg : "$sentimentScore"}, 
        count: {$sum: 1 } 
        } 
    }, 
    {"$sort": { sentiment: -1 } } 
)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-14 05:44:29

我假设您希望在给定阈值(即countnegative - <75 )的情况下,为sentiment有单独的负值和正值字段,即积极情绪的总#、消极情绪的总#以及总情感。

代码语言:javascript
复制
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 } } 
])

样本数据:

代码语言:javascript
复制
{ "_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 }

//结果:

代码语言:javascript
复制
{ "_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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45666450

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档