亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

MongoDB中的條件$ sum

MongoDB中的條件$ sum

MMMHUHU 2019-09-03 16:06:14
我在mongodb中的集合類似于SQL中的下表:情感(公司,感悟)現在,我需要執行這樣的查詢:SELECT  Company,   SUM(CASE WHEN Sentiment >0 THEN Sentiment ELSE 0 END) AS SumPosSenti,   SUM(CASE WHEN Sentiment <0 THEN Sentiment ELSE 0 END) AS SumNegSentiFROM SentimentsGROUP BY Company我該怎么做才能在Mongo中編寫這個查詢?我被困在以下查詢中:db.Sentiments.aggregate({ $project: {_id:0, Company:1, Sentiment: 1} },{ $group: {_id: "$Company", SumPosSenti: {$sum: ? }, SumNegSenti: {$sum: ? } } });
查看完整描述

3 回答

?
MM們

TA貢獻1886條經驗 獲得超2個贊

從版本3.4開始,我們可以使用$switch允許在$group階段中進行邏輯條件處理的運算符。當然我們仍然需要使用$sum累加器來返回總和。


db.Sentiments.aggregate(

    [

        { "$group": { 

            "_id": "$Company",  

            "SumPosSenti": { 

                "$sum": { 

                    "$switch": { 

                        "branches": [ 

                            { 

                                "case": { "$gt": [ "$Sentiment", 0 ] }, 

                                "then": "$Sentiment"

                            }

                        ], 

                        "default": 0 

                    }

                }

            }, 

            "SumNegSenti": {

                "$sum": { 

                    "$switch": { 

                        "branches": [ 

                            { 

                                "case": { "$lt": [ "$Sentiment", 0 ] }, 

                                "then": "$Sentiment"

                            }

                        ], 

                        "default": 0 

                    } 

                }

            }

        }}

    ]

)

如果您尚未遷移您mongod到3.4或更高版本,那么請注意,$project在這個階段,答案是多余的,因為$cond運營商返回一個數值,這意味著你可以$group你的文件和應用$sum的$cond表達。


這將改善您的應用程序的性能,尤其是對于大型集合。


db.Sentiments.aggregate(

    [

        { '$group': {

            '_id': '$Company',

            'PosSentiment': { 

                '$sum': {

                    '$cond': [

                        { '$gt': ['$Sentiment', 0]}, 

                        '$Sentiment', 

                        0

                    ]

                }

            },

            'NegSentiment': { 

                '$sum': {

                    '$cond': [

                        { '$lt': ['$Sentiment', 0]}, 

                        '$Sentiment', 

                        0

                    ]

                }

            }

        }}

    ]

)

考慮一個帶有以下文檔的集合Sentiments:


{ "Company": "a", "Sentiment" : 2 }

{ "Company": "a", "Sentiment" : 3 }

{ "Company": "a", "Sentiment" : -1 }

{ "Company": "a", "Sentiment" : -5 }

聚合查詢產生:


{ "_id" : "a", "SumPosSenti" : 5, "SumNegSenti" : -6 }


查看完整回答
反對 回復 2019-09-03
?
幕布斯7119047

TA貢獻1794條經驗 獲得超8個贊

解釋上面使用數組語法的片段:


PosSentiment: {$cond: [{$gt: ['$Sentiment', 0]}, '$Sentiment', 0]}

等于:


PosSentiment: {$cond: { if: {$gt: ['$Sentiment', 0]}, then: '$Sentiment', else: 0} }

數組語法總結了長語法 { $cond: [if, then, else] }


查看完整回答
反對 回復 2019-09-03
  • 3 回答
  • 0 關注
  • 2575 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號