Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.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_Rest - Fatal编程技术网

Node.js 如何在mongodb的子字段(数组)中进行聚合和分组?

Node.js 如何在mongodb的子字段(数组)中进行聚合和分组?,node.js,mongodb,rest,Node.js,Mongodb,Rest,我正在为考勤系统的员工签入和签出构建一个API。对于每个员工,我需要计算一个月和一年的总工作时间。我尝试使用聚合、项目和组,但我无法获得员工每月或每年的总小时数 我的模式部分如下所示: const EmployeeSchema = new Schema({ _id: Schema.Types.ObjectId, attendance: [{ date: {type: Date, default: Date.now}, entry: {type: Date, required: f

我正在为考勤系统的员工签入和签出构建一个API。对于每个员工,我需要计算一个月和一年的总工作时间。我尝试使用聚合、项目和组,但我无法获得员工每月或每年的总小时数

我的模式部分如下所示:

const EmployeeSchema = new Schema({
_id: Schema.Types.ObjectId,
attendance: [{
    date: {type: Date, default: Date.now},
    entry: {type: Date, required: false},
    exit: { 
        time: {type: Date, required: false}
     },
    totalHours: {type: Number, required: false}
}]
)}

对于聚合,我尝试通过以下方式实现:

exports.aggregation = (req, res) => {
const id = req.params.salespersonId;
DailySalesPerson.aggregate([
      {$unwind: "$attendance"}, 
      {
          $group: {
              _id: {
                  month: {"$month":"$attendance.date"}
              },
              totalHours: {$sum: "totalHours"},
              count: {$sum: 1}
          }
      },


])
.then(result => {
    res.status(200).json({result});
})
})

我没有计算员工每月或每年的总工时

现在我的结果如下:

{
    "result": [
    {
        "_id": "5c5bb13c1d92482b148ed17b",
        "attendance": [
            {
                "date": "2019-02-07T04:30:07.391Z",
                "totalHours": 1,
                "month": 2,
                "year": 2019
            },
            {
                "date": "2019-02-07T04:51:05.359Z",
                "totalHours": 1,
                "month": 2,
                "year": 2019
            },
            {
                "date": "2019-02-07T04:56:21.951Z",
                "totalHours": 1,
                "month": 2,
                "year": 2019
            }
        ]
    },
    {
        "_id": "5c5fdb1781165a1e58115da7",
        "attendance": []
    },
    {
        "_id": "5c5fdd1738ec411b1849ce58",
        "attendance": [
            {
                "date": "2019-02-10T08:14:01.201Z",
                "totalHours": 1,
                "month": 2,
                "year": 2019
            }
        ]
    },
    {
        "_id": "5c5fdf6336e9ea24e0e69795",
        "attendance": [
            {
                "date": "2019-02-10T08:23:14.180Z",
                "totalHours": 1,
                "month": 2,
                "year": 2019
            }
        ]
    }
]
}


我需要汇总每个人每月和每年的总工作时间。现在我的结果如上所示。如何计算每天的总小时数?

您可以使用下面的汇总

db.collection.aggregate([
  { "$project": {
    "totalHoursInThisMonth": {
      { "$let": {
        "vars": { "date": {
          "$filter": {
            "input": "$attendance",
            "as": "attend",
            "cond": {
              "$and": [
                { "$gte": ["$$attend.date", startOfMonth] },
                { "$lte": ["$$attend.date", endOfMonth] }
              ]
            }
          }
        }},
        "in": "$$date.totalHours"
      }}
    }
  }}
])

我使用这个工具并工作:我每月、每年和每天工作的总小时数

exports.aggregationHours = (req, res) => {
let id = req.params.salespersonId;
id= mongoose.Types.ObjectId(id);
DailySalesPerson.aggregate([ 
    {"$match":{
        _id: id
    }},
      {$unwind: "$attendance"}, 
      {
          $group: {
              _id: {
                  month: {"$month":"$attendance.date"},
                  year: {"$year":"$attendance.date"}
              },
              totalHours: {$sum: "$attendance.totalHours"},
                daysworkedCount: {$sum: 1}
          }
      },  
])
.then(result => {
    res.status(200).json({result});
})

})

你能发布样本采集和输出吗?我只获得了在数组中分组的月份和年份,没有总小时数的输出@AnthonyWinzletIt将帮助我们生成输出,如果你发布样本采集和输出output@AnthonyWinzlet我已经编辑了我的结果现在或集合的外观。我需要按月计算出勤率,并计算每月总小时数。