node.js - 如何获取子文档字段的不同值及其在mongodb集合中的计数?

标签 node.js mongodb

我有一个包含如下文档的集合。我想获取属性子文档名称的所有不同值及其不同值和集合中的计数。

示例:

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"
    }

]

我想得到如下输出:

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}
      ]
  }

如何在 mongodb 中获得此输出?

我可以通过mongodb的聚合框架获得这个输出吗?如果可以,那么如何获得? -- 高优先级

最佳答案

是的,聚合可以做到。

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
});

关于node.js - 如何获取子文档字段的不同值及其在mongodb集合中的计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26426276/

相关文章:

javascript - 2个不同的实例保持相同的值

javascript - Next.js 警告 : Warning: Each child in a list should have a unique "key" prop

javascript - 当一个页面中有多个脚本时,如何禁用下一个脚本标签?

mongodb - meteor 和 MongoDB : Authentication failures

javascript - 仅使用提供的更改更新指定字段,mongodb

html - Canvas 脆皮文字渲染

javascript - 循环二进制索引

c# - MongoDB C# 驱动程序 - 从 Controller 返回字符串类型的 Id

node.js - 蒙哥错误: Econnrefused

mongodb - 有没有办法执行更新操作的 "dry run"?