Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Mongodb查询错误_Node.js_Mongodb_Aggregation Framework - Fatal编程技术网

Node.js Mongodb查询错误

Node.js Mongodb查询错误,node.js,mongodb,aggregation-framework,Node.js,Mongodb,Aggregation Framework,我有一个名为Configuration的集合,Configuration有一个osType,我想计算我使用此解决方案的公司的所有osType和group by return Configuration.distinct("osType").then(function (name) { var types = name var groupObj = {"$group": {"_id": "$company.name"}}, projectObj = {"$project": {"

我有一个名为Configuration的集合,Configuration有一个osType,我想计算我使用此解决方案的公司的所有osType和group by

return Configuration.distinct("osType").then(function (name) {
  var types = name

  var groupObj = {"$group": {"_id": "$company.name"}},
    projectObj = {"$project": {"_id": 0, "Company": "$_id.company"}};

  var groupPipeline = types.reduce(function (obj, type) { // set the group pipeline object
    obj["$group"][type + "_count"] = {
      "$sum": {
        "$cond": [{"$eq": ["$type", type]}, 1, 0]
      }
    };
    return obj;
  }, groupObj);

  return Configuration.aggregate([groupPipeline]).then(function (result) {
    return {status: true, code: 200, message: "Configuration count", data: result}
  });
我得到了这个错误

"message": "exception: the group aggregate field name '8.1_count' cannot be used because $group's field names cannot contain '.'",
这是我的样本文件

{
  "_id" : ObjectId("57c97bd6ad85bac155aecafb"),
  "ITBConfigurationId" : 26921,
  "updatedAt" : ISODate("2016-09-02T13:17:10.066Z"),
  "createdAt" : ISODate("2016-09-02T13:17:10.066Z"),
  "id" : "23",
  "name" : "dav20-pc-126.dallas.dav20.thd",
  "type" : "Managed Workstation",
  "locationId" : null,
  "osType" : "7",
  "osInfo" : "Home Premium x64 Edition Service Pack 1 Build 7601",
  "company" : {
    "id" : 19300,
    "identifier" : "DAV20",
    "name" : "Davidson Stewart Morelock Ind Ins Group, LLC"
  },
  "__v" : 0
}
和osType的不同列表

[ '7',
 'Mac OS X',
 '8.1',
 'Vista',
 'XP',
 '2003',
 '2008',
 '2012',
 '8',
 'Linux',
 'Microsoft Windows Server 2012 R2 Datacenter x64',
 'Microsoft Windows Server 2012 R2 Standard x64',
 'Microsoft Windows 7 Professional  x64',
 '',
 '2000',
 'Microsoft Windows 7 Professional ',
 'Microsoft Windows 10 Pro x64',
 'Darwin',
 'Microsoft Windows Server 2012 Standard x64',
 'Microsoft Windows Server 2008 R2 Standard  x64',
 'Microsoft Windows XP Professional',
 'Microsoft Windows 8.1 Pro x64',
 'Microsoft® Windows Vista™ Home Premium  x64',
 'Microsoft Windows Server 2012 Datacenter x64',
 'Microsoft Windows Server 2003 for Small Business Server',
 'Microsoft® Windows Vista™ Business ',
 'Microsoft Windows 7 Home Premium  x64',
 'Microsoft Windows XP Home Edition',
 'Microsoft Windows 8 Pro x64',
 'Microsoft Windows 7 Home Premium ',
 'Microsoft Windows 8.1 x64',
 'Microsoft Windows 10 Home x64',
 'Microsoft Windows 8.1 Pro with Media Center x64',
 'Microsoft Windows 10 Pro' 
]

如错误所述,您不能在
$group
管道字段中使用包含
'.
字符的字段名。由于您是动态生成字段的,因此在生成字段的代码中,将出现的“
”.
字符临时替换为下划线,并建议对任何空格字符也使用下划线

因此,动态返回管道对象的代码应该是:

var groupObj = { "$group": { "_id": "$company.name" } };
var groupPipeline = types.reduce(function(obj, type) { 
    obj["$group"][type.replace(".", "_").replace(/\s+/g,"_") + "_count"] = {
        "$sum": {
            "$cond": [ { "$eq": [ "$osType", type ] }, 1, 0 ]
        }
    };
    return obj;
}, groupObj );

如错误所述,您不能在
$group
管道字段中使用包含
'.
字符的字段名。由于您是动态生成字段的,因此在生成字段的代码中,将出现的“
”.
字符临时替换为下划线,并建议对任何空格字符也使用下划线

因此,动态返回管道对象的代码应该是:

var groupObj = { "$group": { "_id": "$company.name" } };
var groupPipeline = types.reduce(function(obj, type) { 
    obj["$group"][type.replace(".", "_").replace(/\s+/g,"_") + "_count"] = {
        "$sum": {
            "$cond": [ { "$eq": [ "$osType", type ] }, 1, 0 ]
        }
    };
    return obj;
}, groupObj );

@图奎鲁哈桑我很高兴能帮上忙。对于堆栈溢出,我们要感谢您帮助我们。在你勾选的左边答案旁边有一个绿色的复选标记:)@TouqeerUlHassan我很高兴能帮上忙。对于堆栈溢出,我们要感谢您帮助我们。在您勾选的左侧答案旁边有一个绿色复选标记:)