这是我的查询,
db.product.aggregate([
{ $match : {categoryID : 4 } },
{ "$group" : { "_id" : { "productID": "$productID",
"articleID": "$articleID", "colour":"$colour",
"set&size": { "sku" : "$skuID", "size" : "$size" },
},
}
},
{ "$group" : { "_id" : { "productID": "$_id.productID", "colour":"$_id.colour" },
"size": { "$addToSet" : { "sku" : "$_id.set&size.sku",
"size" : "$_id.set&size.size" }
},
}
},
{"$project":{
"_id":0,
"productID": "$_id.productID",
"colour":"$_id.colour",
"size":"$size",
}
},
]);
Run Code Online (Sandbox Code Playgroud)
通过在mongo shell上执行此查询,我得到了完美的输出.
产量
{
"_id": {
"productID": "PRD1523",
"colour": "GREEN"
}, …Run Code Online (Sandbox Code Playgroud)