2015-11-30 33 views
0

我在MongoDB中此聚集查詢:

db.questions.aggregate([ 
{ $project:{question:1,detail:1, choices:1, answer:1, 
    percent_false:{ 
    $multiply:[100,{$divide:["$answear_false",{$add:["$answear_false","$answear_true"]}]}]}, 
    percent_true:{ 
    $multiply:[100,{$divide:["$answear_true",{$add:["$answear_false","$answear_true"]}]}]} }}, {$match:{status:'active'} } 
]).pretty() 

我想用$比賽2個計算領域 「percent_true」 和 「percent_false」 像這樣

$match : {percent_true:{$gte:20}} 

我能怎麼做 ?

+0

{$ match:{percent_true:{$ gte:20},anotherField:{$ lte:10},yetAnotherField:12345}},no? – joao

+1

您的代碼塊是真的很難看。您能否再投入更多精力在下一次縮進? –

回答

0

辛格框架階段工作的聚合,可以處理計算領域,就好像他們是正常的字段,因爲從$match的角度來看,他們是正常的。

{ $project:{ 
     question:1,detail:1, choices:1, answer:1, 
     percent_false:{ 
     $multiply:[100,{$divide:["$answear_false",{$add:["$answear_false","$answear_true"]}]}] 
     }, 
     percent_true:{ 
     $multiply:[100,{$divide:["$answear_true",{$add:["$answear_false","$answear_true"]}]}]}  
    } 
}, 
{$match:{ 
    status:'active', 
    percent_true:{$gte:20} 
    //When documents get fed to match they already have a percent_true field, so you can match on them as normal 
    } 
} 
+0

太謝謝你了@大衛格林貝格:d –

+0

@KhánhĐàm請給予好評和接受,如果答案幫助你。 –