4
我有什麼
我有以下證件進入我的匯聚管道:
[
{
"email" : "[email protected]",
"name" : "Organization Name",
"users" : [
{
"lastName" : "Nye",
"firstName" : "Bill",
"email" : "[email protected]"
},
{
"lastName" : "Rogers",
"firstName" : "Mr.",
"email" : "[email protected]"
}
]
},
...
]
我想要什麼
使用$project
我想在每個陣列的$concat
的firstName
和lastName
領域子文件得到以下結果:
{
"email" : "[email protected]",
"name" : "Organization Name",
"users" : [
{
"name" : "Bill Nye",
"email" : "[email protected]"
},
{
"name" : "Mr. Rogers",
"email" : "[email protected]"
}
]
}
我已經試過 - 嘗試#1
我已經試過在聚集:
db.organizations.aggregate([
{
$project: {
email : 1,
name : 1,
users : {
name : { $concat : [ "$users.firstName", " ", "$users.lastName" ] },
email : 1
}
}
}
]);
...但我得到以下錯誤:
$concat only supports strings, not Array
我」什麼已經試過 - 嘗試#2
我也試過以下聚合:
db.organizations.aggregate([
{
$project: {
email : 1,
name : 1,
users : {
name : { $concat : [ "$firstName", " ", "$lastName" ] },
email : 1
}
}
}
]);
...但是name
總是返回null
。
我覺得這應該是一個相對簡單的事情,但是,我完全難住。
如何獲得我正在查找的結果?
雖然這個答案和@ chridam的答案一樣好,並且從另一個角度解決問題,但我最終使用了他的解決方案。謝謝你的時間! – docksteaderluke
@docksteaderluke我知道你會使用這個解決方案;),因爲儘管事實上其他答案是「事實上正確的」,但它絕對不是要走的路。 – styvane
謹慎闡述?爲什麼不走這條路? – docksteaderluke