mongodb聚合对象键以查找不同的值
我有一个文档结构如下的集合:mongodb聚合对象键以查找不同的值,mongodb,mapreduce,aggregation-framework,Mongodb,Mapreduce,Aggregation Framework,我有一个文档结构如下的集合: { "_id" : ObjectId("551d41bc0474fa4b03e4c072"), "last_fetch_date" : ISODate("2015-04-02T18:48:52.573Z"), "specifications" : { "pattern" : "Striped", "fit" : "Slim", "fabric" : "Cotton Linen",
{
"_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"]
}
是否有任何方法可以一次计算出这一点,而高效的性能非常重要?您需要每个字段的不同值还是这些字段的现有组合的不同值?我需要每个字段的不同值的可能重复。但我事先知道我的收藏中存在哪些字段。某些字段可能存在于一个文档中,但不存在于其他文档中,因此每个文档中的字段不同doc@yogesh:此问题与您发布链接的问题大不相同。那件事很琐碎