按字段和合并字段分组文档

时间:2014-07-04 11:49:37

标签: mongodb aggregation-framework

我有这个文档结构

{
"_id" : ObjectId("63b6757dfa46345e268b7ba5"),
  "name" : "Blue Skinny Jeans",
  "sizes" : [45,46],
  "textile" : "Wool",
  "merchantCategory" : "Jeans",
  "tags" : ["Tag 1","Tag 2"],
  "sku" : "12161-2753930-5053177208071",
  "manufacturer" : "Hugo Boss"
}

/* 1 */
{
  "_id" : ObjectId("53b6757dfa46345e268b7ba4"),
  "name" : "Blue Skinny Jeans",
  "sizes" : [40,41],
  "textile" : "Polyester",
  "merchantCategory" : "Jeans",
  "tags" : ["Tag 2","Tag 3"],
  "sku" : "12161-2753930-5053177208071",
  "manufacturer" : "Hugo Boss"
}

是否可以构建查询 " groups"文件按名称和合并字段 不等于(尺寸,标签,商品分类,纺织品)

1 个答案:

答案 0 :(得分:1)

这可能有效,

coll.aggregate({"$group":{"_id":"$name",
                          "sizes":{"$addToSet":"$sizes"},
                          "tags":{"$addToSet":"$tags"},
                         ..
                         ..
                })