简体   繁体   中英

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?

Can i get this output by aggregate framework of mongodb, if yes, then how? -- 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
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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