{
attrib : [
{ k: "color", v: "red" },
{ k: "shape", v: "rectangle" },
{ k: "color", v: "blue" },
{ k: "avail", v: true }
]
}
统计 color 中不同属性个数
db.so.aggregate( [
{ $unwind : '$attrib' },
{ $match: { 'attrib.k' : 'color' } },
{ $group: { _id: '$attrib.v', count: { '$sum': 1 } } }
] );
{
"result" : [
{
"_id" : ObjectId("51eeb9f2812db9ff4412f132"),
"attrib" : {
"k" : "color",
"v" : "red"
}
},
{
"_id" : ObjectId("51eeb9f2812db9ff4412f132"),
"attrib" : {
"k" : "color",
"v" : "blue"
}
}
],
"ok" : 1
}