我有一个文档结构的集合如下:
{
"_id" : ObjectId("551d41bc0474fa4b03e4c072"),
"last_fetch_date" : ISODate("2015-04-02T18:48:52.573Z"),
"specifications" : {
"pattern" : "Striped",
"fit" : "Slim",
"fabric" : "Cotton Linen",
"ideal_for" : "Men's",
"sleeve" : "Full Sleeve"
}
}
我需要聚合数据,以便在集合中的所有文档中找到模式,适合度和结构的不同值。目前我正在为每个属性编写单独的查询。像这样:
db.collection.aggregate(
{
"$group": { "_id": "fabric", "values": {"$addToSet": "$specifications.fabric"} }
}
)
为了更清楚地解释,我希望我的输出是这样的(聚合在所有文档中)
{
"fabric": ["cotton", "satin", "linen"],
"fit": ["slim", "regular"],
"pattern": ["stipes", "checkered", "floral", "solid"]
}
有没有办法一次性计算这个并且效率太高(性能很重要)?