2015-11-10 51 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