1
我已經收集在一個文件「健身房」看起來像下面這樣:獲取每個文檔的數組的大小子陣列
"name" : "Testing",
"albums" : [
{
"name" : "Aerobics",
"photos" : [
{
"filepath" : "aerobics.jpg"
}
]
},
{
"name" : "Aqua",
"photos" : [
{
"filepath" : "aqua.jpg"
}
]
},
{
"name" : "Zumba",
"photos" : [
{
"filepath" : "zumba.jpg"
}
]
}
],
我想選擇的照片數量從每張專輯到投影。
所以我的輸出將如下所示:
"name" : "Testing",
"albums" : [
{
"name" : "Aerobics",
"amount_photos" : 1,
"photos" : [
{
"filepath" : "aerobics.jpg"
}
]
},
{
"name" : "Aqua",
"amount_photos" : 1,
"photos" : [
{
"filepath" : "aqua.jpg"
}
]
},
{
"name" : "Zumba",
"amount_photos" : 1,
"photos" : [
{
"filepath" : "zumba.jpg"
}
]
}
]
它應該是具有聚合實現,但我得到以下錯誤,當我嘗試我的查詢:
的參數$大小必須是一個數組,但類型的:字符串
查詢:
db.gyms.aggregate(
[
{
$project: {
'albums.photos' : {
amountPhotos: { $size: "photos" }
}
}
}
]
)
我以爲mongo會有一個更簡單的方法來做到這一點,顯然不是。謝謝! – Jamie
@Jamie這很簡單;) – styvane
更短的方式*像我的查詢。 – Jamie