Is there an elseif thing in MongoDB to $cond while aggregating

前端 未结 3 2212
忘了有多久
忘了有多久 2021-02-01 16:19

So I need a custom field calculated in MongoDB as follows

if( field1 ==\"A\")  ->customfield=10
else if(field1 ==\"B\"  )->customfield=20
else (field1 ==\"         


        
3条回答
  •  忘了有多久
    2021-02-01 17:06

    MongoDB 3.4 has a new thing called $switch for this exact thing!

    $switch: {
       branches: [
          { case: , then:  },
          { case: , then:  },
          ...
       ],
       default: 
    }
    

    https://docs.mongodb.com/manual/reference/operator/aggregation/switch/

提交回复
热议问题