2015-11-10 50 views
1

我使用$纂操作:

{ $redact: { 
    $cond: { 
     "if": { 
      "$lt": [ "$number1", "$number2" ], 
     }, 
     "then": "$$KEEP", 
     "else": "$$PRUNE" 
    } 
}} 

我想$and: [{$lt:["$number1, $number2"]}, {$exists:[$number3, 1]}],但我不能得到$存在,或者檢查空。

+0

只需使用'$ match' – Rohmer

回答

1

你想$ifNull,因爲它是一個「之類的」同等學歷,返回的替代值的附加功能,其中場不存在:

{ "$redact": { 
    "$cond": { 
     "if": { 
      "$and": [ 
       { "$lt": [ "$number1", "$number2" ] }, 
       { "$ifNull": [ "$number3", false ] } 
      ] 
     }, 
     "then": "$$KEEP", 
     "else": "$$PRUNE" 
    } 
}} 

那麼,該領域存在的價值該字段被返回,並且這基本上與說true(除非它的值實際上是false,在這種情況下,換行爲$cond)基本相同,或者返回已經作爲false提供的替換。

+0

不存在什麼?如果我希望它返回true,如果$ eq爲null,如果它存在,則返回false。 – CSharpAtl

+0

@CSharpAtl這就是'$ ifNull'的功能。正如我所說,如果該值不是來自正返回值的「truthy」,那麼將其用'$ eq'封裝到'$ cond'中,以便返回等於null的值發出'true'。 –

+0

我能夠只是$而不是它。謝謝。 – CSharpAtl