将mongodb聚合嵌套数组的值传递到把手文件

时间:2019-05-27 05:12:20

标签: node.js mongodb express handlebars.js

我需要将通过汇总mongodb值创建的嵌套数组结果值传递到车把模板并显示在表中

Mongodb,nodejs,handlesbar js,expressjs

mongodb查询控制台的汇总输出为

 {
"_id" : "Safety",
"c_group" : [
    {
        "c_group" : "Electrical Safety & Control of Hazardous Energy",
        "c_name" : [
            "Electrical Safety for Ordinary Electrical Persons",
            "Electrical Safety for Qualified Electrical Persons",

        ]
    },
    {
        "c_group" : "Vestas Basic Safety",
        "c_name" : [
            "Safety Introduction for Technicians"
        ]
    },
    {
        "c_group" : "Others",
        "c_name" : [
            "GWO First Aid",
            "Energy Control Coordinator"
        ]
    },
    {
        "c_group" : "GWO Basic Safety",
        "c_name" : [
            "GWO Fire awareness",
            "Working at heights",
            "GWO Manual Handling"
        ]
     }
 ],
  "count" : 13
}
{
 "_id" : "D Level",
 "c_group" : [
    {
        "c_group" : "Installation",
        "c_name" : [
            "Basic installation"
        ]
    },
    {
        "c_group" : "D-Add On-Service",
        "c_name" : [
            "AVANTI Operation Service lift",
            "Bolt tightening" 
        ]
    },
    {
        "c_group" : "Service",
        "c_name" : [
            "Basic Service"
        ]
    },
    {
        "c_group" : "Basic Turbine Operation",
        "c_name" : [
            "Anchor Cage Assembly",
            "VMP 3500" 
        ]
    },
    {
        "c_group" : "GWO - Basic Technical Training",
        "c_name" : [
            "BTT Electrical",
            "BTT Hydraulic" 
        ]
    }
 ],
 "count" : 18
 }
  {
 "_id" : "C Level",
 "c_group" : [
     {
        "c_group" : "Installation",
        "c_name" : [
            "V80-V100 2MW",
            "2MW MK10",
            "V112/V117/V126 3.3MW",
            "V112 On shore",
            "Installation_C_All"
        ]
    },
    {
        "c_group" : "Service",
        "c_name" : [
            "V39/V47",
            "TAC II",
            "VMP 5000" 
        ]
    },
    {
        "c_group" : "C-Add on",
        "c_name" : [
            "Power Climber_SE_INS",
            "Inspection of Anchor Points" 
        ]
    },
    {
        "c_group" : "Turbine Operation(TOC)",
        "c_name" : [
            "TAC II",
            "VMP 5000",
            "VMP Global",
            "VMP 6000",
            "VMP Global Mk10 & Mk11",
            "V112 and Gridstreamer",
            "V112 Mk3 Cubepower",
            "GE 1.5 ESS",
            "GE 1.5MW non-ESS",
            "GE 2.5/2.75 MW",
            "G5x",
            "G8x/G9x",
            "Nordex N90 2.5MW",
            "Siemens SWT 2.3MW",
            "Suzlon 88-2.1MW"
        ]
    }
   ],
   "count" : 45
   }
  {
 "_id" : "A Level",
 "c_group" : [
     {
        "c_group" : "Troubleshooting Tools",
        "c_name" : [
            "VMP Global"
        ]
    }
 ],
 "count" : 1
  }
   {
   "_id" : "B Level",
   "c_group" : [
    {
        "c_group" : "Troubleshooting",
        "c_name" : [
            "VMP 3500",
            "VMP 5000",
            "TAC II",

            "Siemens SWT 2.3MW",
            "Suzlon 88-2.1MW"
        ]
    },
    {
        "c_group" : "",
        "c_name" : [
            "G 1.8-2MW",
            "G8x/9x"
        ]
    }
],
"count" : 18
 }
 {
 "_id" : "Special Courses",
 "c_group" : [
    {
        "c_group" : "Blade",
        "c_name" : [
            "Blade Inspection (Level C)",
            "Blade Repair (Level C)",
            "Advanced Blade Repair (Level B)"
        ]
    },
    {
        "c_group" : "Others",
        "c_name" : [
            "Mount mobile anchor point - V82",
            "Turbine Operation" 
        ]
    },

    {
        "c_group" : "Transportation",
        "c_name" : [
            "Transport-Road transportation",
            "Transport-Sea and Stevedoring"
        ]
    }
 ],
"count" : 34
}

这是我的渲染代码,用于将值传递到车把模板

router.get('/catalogue', function(req, res) {   

     var catalogd = {};
    Ct_course.aggregate([ 
        { $match: { "c_cust": false }},
          {
        $group: {       
          _id: {c_level: "$c_level", c_group: "$c_group" },
          c_name: {$push: "$c_name"},

          bookCount: {$sum: 1}}
      },
      {
        $group: {_id: "$_id.c_level", c_group: { $push: {c_group: "$_id.c_group", c_name: "$c_name"} },
          count: {$sum: "$bookCount"}
        }
      }
    ],
function(err, results){  
var a= results.length;  
for (i=0; i < a; i++)
{
catalogd = results[i];
console.log(catalogd); 
}
    res.render("course/catalogue",{course:catalogd});
});

});

在控制台上,我没有得到如mongodb查询窗口中那样的确切结果。我需要显示嵌套数组,并将所有值传递到车把文件。我获得的控制台值是

 { _id: 'Safety',
    c_group:
    [ { c_group: 'Electrical Safety & Control of Hazardous Energy',
     c_name: [Array] },
    { c_group: 'Vestas Basic Safety', c_name: [Array] },
    { c_group: 'Others', c_name: [Array] },
    { c_group: 'GWO Basic Safety', c_name: [Array] } ],
   count: 13 }
{ _id: 'D Level',
 c_group:
  [ { c_group: 'Installation', c_name: [Array] },
   { c_group: 'D-Add On-Service', c_name: [Array] },
  { c_group: 'Service', c_name: [Array] },
  { c_group: 'Basic Turbine Operation', c_name: [Array] },
  { c_group: 'GWO - Basic Technical Training', c_name: [Array] } ],
 count: 18 }
 { _id: 'C Level',
 c_group:
 [ { c_group: 'Installation', c_name: [Array] },
 { c_group: 'Service', c_name: [Array] },
 { c_group: 'C-Add on', c_name: [Array] },
 { c_group: 'Turbine Operation(TOC)', c_name: [Array] } ],
 count: 45 }
{ _id: 'A Level',
 c_group: [ { c_group: 'Troubleshooting Tools', c_name: [Array] } ],
 count: 1 }
{ _id: 'B Level',
 c_group:
  [ { c_group: 'Troubleshooting', c_name: [Array] },
 { c_group: '', c_name: [Array] } ],
  count: 18 }
  { _id: 'Special Courses',
  c_group:
 [ { c_group: 'Blade', c_name: [Array] },
 { c_group: 'Others', c_name: [Array] },
 { c_group: 'Software Upload', c_name: [Array] },

 count: 34 }

我不确定如何解析数组值并将其发送到车把模板文件。

从nodejs渲染到车把模板后的异常输出

使用每个循环在车把文件中最终输出,车把帮助器js文件将采用表格格式

  Safety

 -Electrical Safety & Control of Hazardous Energy

   -Electrical Safety for Ordinary Electrical Persons
    -Electrical Safety for Qualified Electrical Persons

我对将嵌套数组值传递到车把模板很困惑。

0 个答案:

没有答案