我有以下查询:
cursor=self.postCol.aggregate([
{ "$graphLookup" : {
"from": "pCol",
"startWith": "$parent",
"connectFromField": "parent",
"connectToField": "_id",
"as" : "parents"
}
},
{ "$project" : {
"pValue": {
"$cond": [
{ "$ne": [ "$pValue", [] ] },
"$pValue",
"$parents.0.pValue"
]
}
}
}
])
因此给出以下记录:
{"_id": 4, parent: "", "pValue": ["d"], fieldA: 9},
{"_id": 5, parent: 4, "pValue": [], fieldA:2},
{"_id": 6, parent: 4,"pValue": [], fieldA: 9}
我应该得到:
{"_id": 4, "pValue": ["d"]}
{"_id": 5, "pValue": ["d"]}
{"_id": 6, "pValue": ["d"]}
相反,我得到:
{"_id": 4, "pValue": ["d"]}
{"_id": 5, "pValue": []}
{"_id": 6, "pValue": []}
The $parents.0.pValue
由于某种我不明白的原因没有返回正确的值。我该如何选择领域pValue
在父母数组的第一个元素内?
我想用{"$arrayElemAt": ["$parents",0]}
这将为我提供嵌入的文档,但是我怎样才能从中获取要返回的字段呢?