鍍金池/ 問答/Java  數(shù)據(jù)庫/ spring boot怎么使用mongodb進行ifelse操作

spring boot怎么使用mongodb進行ifelse操作

數(shù)據(jù)存放了一個字段type(當(dāng)作枚舉),查詢的時候,需要通過使用ifelse或者switch另作處理,比如1就*5,2就轉(zhuǎn)化為正常。我用mongodb語句已經(jīng)實現(xiàn),但是怎么在springboot 框架中處理呢?sql語句如下:

ifelse:

db.getCollection('Historys').aggregate(
    [
    {
        $project:{
            "count":{
                $cond:{
                        if:{$eq:["$type",1]},then:1*8,else:{
                        $cond:{if:{$eq:["$type",2]},then:2,else:{
                            $cond:[{$eq:["$type",3]},5,0]
                            }}
                        }
                    }
                }
            }
        }
    ]
    )

switch:

db.getCollection('Historys').aggregate([{
    $project:{"type":1,"count":
        {$switch:{
            branches:[
            {case:{$eq:["$type",1]},then:1*9},
            {case:{$eq:["$type",2]},then:"2"},
            {case:{$eq:["$type",3]},then:"3"}
            ],
            default:0
            }}
        }
    }])
回答
編輯回答
來守候

把你的語句拼成mongodb的Document@@

2017年12月19日 12:47