我正在做aggregation
,project
和cond
。事情是這樣的......
假設的文件:
{outter:{
inner1:"blah",
innerOther:"somethingelse"
}}
聚合:
db.collection.aggregate([{
$project : {
"outter.inner1":1,
"outter.inner2":{
$cond : if:{"outter.innerOther":{$exists:true}},
then:{"blah":"blah"},
else:{"blah":"blah"}
}
}
}])
當我運行此我得到一個錯誤:exception: dotted field names are only allowed at the top level
在if
條件語句(我試過更換如果與if:true
和聚合工作)。
這是一個錯誤?有沒有做一個解決方法,而不是做一個完整的其他項目來簡單地讓字段沒有點?
編輯
所以,我發現了一個解決方法,但還是想看看這是預期的行爲。解決方法是通過$let
在投影中使用變量。此外,看起來$exists
不是有效的expression
,所以不得不做出以下更改。
db.collection.aggregate([{
$project : {
"outter.inner1":1,
"outter.inner2":{
$let :{
vars:{innerOther:{$ifNull:["$outter.innerOther", "absent"]}},
}
$cond : if:{$eq:["$$innerOther","absent"]},
then:{"blah":"blah"},
else:{"blah":"blah"}
}
}
}])
是 「outter」 的陣列?請你能顯示你的文件嗎? – styvane
Outter是一個子文檔。 –