简体   繁体   English

如何在mongodb中使用集合中的计数获得子文档字段的独特价值?

[英]How to get distinct value of sub document field with their count in collection in mongodb?

I have a collection with documents like below.I want to get the all distinct value of name of attributes sub-document with their distinct value and count in collection. 我有一个包含如下文档的集合,我想获取属性子文档名称的所有不同值及其各自的值和集合中的计数。

Example : 范例:

var records  = [
     {
        "attributes": [
            {
                "name": "color",
                "value": "black",
                "_id": "5441103a0348ebc91ee75b33"
            }
        ],
        "name": "ddd"
    },
    {
        "attributes": [
            {
                "name": "color",
                "value": "red",
                "_id": "5441091393450f1619be99af"
            },
            {
                "name": "size",
                "value": "L",
                "_id": "5441091393450f1619be99b0"
            }
        ],
        "name": "one"
    },
    {

        "attributes": [
            {
                "name": "color",
                "value": "black",
                "_id": "5441092593450f1619be99b1"
            },
            {
                "name": "size",
                "value": "L",
                "_id": "5441092593450f1619be99b2"
            }
        ],

        "name": "sdfsda"
    },
    {
        "attributes": [
            {
                "name": "color",
                "value": "green",
                "_id": "5441093d93450f1619be99b3"
            },
            {
                "name": "size",
                "value": "S",
                "_id": "5441093d93450f1619be99b4"
            }
        ],
        "name": "threee"
    },
    {
        "attributes": [
            {
                "name": "color",
                "value": "green",
                "_id": "5441095793450f1619be99b5"
            },
            {
                "name": "size",
                "value": "M",
                "_id": "5441095793450f1619be99b6"
            }
        ],
        "name": "one"
    }

]

I want to get output like : 我想得到像这样的输出:

var output =  
   {
     "color" : [
           {value : 'red', count : 1}
           {value : 'black', count : 2}
           {value : 'green', count : 2}
         ],
     "size" : [
          {value : 'S', count : 2}
          {value : 'L', count : 1}
          {value : 'M', count : 1}
      ]
  }

how can i get this output in mongodb? 我如何在mongodb中获得此输出?

Can i get this output by aggregate framework of mongodb, if yes, then how? 我可以通过mongodb的聚合框架获取此输出,如果是,那么如何? -- high priority -高优先级

Yes, aggregate can make it. 是的, 聚合可以成功。

var output = {};

db.c.aggregate([{
    $unwind : "$attributes"
}, {
    $group : {
        _id : {
            name : "$name",
            value : "$value"
        },
        count : {
            $sum : 1
        }
    }           // the output after this stage such as 
                // {_id:{name:"color", value:"green"}, count:2}
                // {_id:{name:"size", value:"S"}, count:2}
}, {
    $group : {
        _id : "$_id.name",
        contents : {
            $push : {
                value : "$_id.value",
                count : "$count"
            }
        }
    }           // the output after this stage such as 
                // {_id:"color", contents:[{value:"green", count:2}]}
                // {_id:"size", contents:[{value : 'S', count : 2}]}
}]).forEach(function(doc) {
    output[doc._id] = doc.contens;  // just convert to the format as expected
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM