如何引用MongoDB中的對象數組中的對象的每個屬性MapReduce JavaScript查詢?如何引用MongoDB中的對象數組中的對象的每個屬性MapReduce JavaScript查詢?
這裏是我的數據:
{
"_id": ObjectId("544ae3de7a6025f0470041a7"),
"name": "Bundle 4",
"product_groups": [
{
"name": "camera group",
"products": [
{
"$ref": "products",
"$id": ObjectId("531a2fcd26718dbd3200002a"),
"$db": "thisDB"
},
{
"$ref": "products",
"$id": ObjectId("538baf7c26718d0a55000043"),
"$db": "thisDB"
},
{
"$ref": "products",
"$id": ObjectId("538baf7c26718d0a55000045"),
"$db": "thisDB"
}
]
},
{
"name": "lens group",
"products": [
{
"$ref": "products",
"$id": ObjectId("531e3ce926718d0d45000112"),
"$db": "thisDB"
},
{
"$ref": "products",
"$id": ObjectId("531e3ce926718d0d45000113"),
"$db": "thisDB"
}
]
}
]
}
這裏是我的地圖功能:(爲簡單起見,我拿出了降低的選擇,因爲如果地圖不正確的工作也沒關係)
var map = function() { emit(this.product_groups, this.product_groups.products); };
db.instant_rebates.mapReduce(
map,
{
out: "map_reduce_example",
query: {"_id": ObjectId("544ae3de7a6025f0470041a7")}
}
);
但問題是結果中的「值」字段總是以「未定義」的形式出現。爲什麼?爲什麼this.product_groups.products不會返回產品數組?我該如何解決?
此外,我想要做的是發出TWICE,一次爲每個兩個product_groups。但到目前爲止,它只能發射一次。我如何解決這個問題?