Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.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
Arrays 如何在mongo db聚合中以数组形式获取数据_Arrays_Database_Mongodb_Aggregation Framework_Aggregate Functions - Fatal编程技术网

Arrays 如何在mongo db聚合中以数组形式获取数据

Arrays 如何在mongo db聚合中以数组形式获取数据,arrays,database,mongodb,aggregation-framework,aggregate-functions,Arrays,Database,Mongodb,Aggregation Framework,Aggregate Functions,假设这是我的数据模式 { "_id" : ObjectId("5f90ed2954d61feed1720603"), "date" : ISODate("2020-10-22T00:00:00Z"), "worker_full_name" : "JOHN DOE", "worker_department" : "Wo

假设这是我的数据模式

{
    "_id" : ObjectId("5f90ed2954d61feed1720603"),
    "date" : ISODate("2020-10-22T00:00:00Z"),
    "worker_full_name" : "JOHN DOE",
    "worker_department" : "Worker Department",
    "type" : "Worker",
    "site_name" : "DL LIMITED",
    "in_time" : ISODate("2020-10-22T07:53:35Z"),
    "attendance_points" : 2,
    "out_time" : ISODate("2020-10-22T22:03:41Z"),
    "duration" : 14,
    "attendance_count" : 2,
    "wage" : 580,
    "in_location" : "Countryside Avenue",
    "worker_aadhar_card_number" : "xxxxxxxxxxxx",
    "out_location" : "Golf Drive",
    "worker_id" : ObjectId("5f12ea794fdb64e82ce68fac")
}
{
    "_id" : ObjectId("5f90ed2754d61feed17205fe"),
    "date" : ISODate("2020-10-22T00:00:00Z"),
    "worker_full_name" : "JOHN DOE 2",
    "worker_department" : "Worker Department",
    "type" : "Worker",
    "site_name" : "DL LIMITED",
    "in_time" : ISODate("2020-10-22T07:53:34Z"),
    "attendance_points" : 2,
    "out_time" : ISODate("2020-10-22T22:24:02Z"),
    "duration" : 14,
    "attendance_count" : 2,
    "wage" : 0,
    "in_location" : "Countryside Avenue",
    "worker_aadhar_card_number" : "xxxxxxxxxxxx",
    "out_location" : "Countryside Avenue",
    "worker_id" : ObjectId("5f688cf2df29927bfb8531eb")
}
我正在做以下工作:

my_collection.aggregate([{'$match': {'date': {"$gte": start_date, "$lte": end_date},
                                                   'worker_full_name': {"$exists": 'true'},
                                                   "site_name": site_name
                                                   }},
                                       {"$group": {'_id': {
                                           'worker_id': '$worker_id',
                                           'worker_full_name': '$worker_full_name'
                                       },
                                           'present_days': {'$sum': 1},
                                           'total_shift_points': {'$sum': '$attendance_points'}
                                       }}
])
通过这样做,我能够实现以下输出:

{'_id': {'worker_id': ObjectId('5f688cf2df29927bfb8531d6'), 'worker_full_name': 'JOHN DOE'}, 'present_days': 22, 'total_shift_points': 38.25}
{'_id': {'worker_id': ObjectId('5f66130f94c75522f314dbc0'), 'worker_full_name': 'JOHN DOE 2'}, 'present_days': 19, 'total_shift_points': 35.25}
{'_id': {'worker_id': ObjectId('5f66130e94c75522f314db99'), 'worker_full_name': 'JOHN DOE 3'}, 'present_days': 23, 'total_shift_points': 42.75}
{'_id': {'worker_id': ObjectId('5f27b678749921225e5df98c'), 'worker_full_name': 'JOHN DOE 4 '}, 'present_days': 22, 'total_shift_points': 38.25}
{'_id': {'worker_id': ObjectId('5f6f2ac0b112533f081c3bae'), 'worker_full_name': 'JOHN DOE 5'}, 'present_days': 21, 'total_shift_points': 36.75}
但有什么方法可以得到这样一个数组中的所有点吗?下面是我想要的输出,其中我单个查询返回一个每日出勤点数组及其相应的日期:

 {'_id': {'worker_id': ObjectId('5f688cf2df29927bfb8531d6'),
  'worker_full_name': 'JOHN DOE'},
  'present_days': 22,
  'total_shift_points': 38.25 ,
  'daily_points_stats':[
                {ISODate("2020-10-01T00:00:00Z"):2},
                {ISODate("2020-10-02T00:00:00Z"):1.5}
                {ISODate("2020-10-03T00:00:00Z"):1}
                {ISODate("2020-10-04T00:00:00Z"):1.25}
                ..
                ..for all the days

 ]}
或者像这样:

{'_id': {'worker_id': ObjectId('5f688cf2df29927bfb8531d6'),
      'worker_full_name': 'JOHN DOE'},
      'present_days': 22,
      'total_shift_points': 38.25 ,
      'daily_points_stats':[
                    {"date":ISODate("2020-10-01T00:00:00Z"),"points":2},
                    {"date":ISODate("2020-10-02T00:00:00Z"),"points":1.5},
                    {"date":ISODate("2020-10-03T00:00:00Z"),"points":1},
                    {"date":ISODate("2020-10-04T00:00:00Z"),"points":1.25},
                    ..
                    ..for all the days

     ]}

您可以使用
$push
$group
内创建数组

  {
    "$group": {
      "_id": {
        "worker_id": "$worker_id",
        "worker_full_name": "$worker_full_name"
      },
      // like this
      daily_points_stats: {
        $push: {
          date: "$date",
          points: "$attendance_count"
        }
      },
      "present_days": { "$sum": 1 },
      "total_shift_points": { "$sum": "$attendance_points" }
    }
  }


如果你想要一个对象,你可以试试

  • $push k(键)和v(值),将键转换为字符串类型,因为其日期类型使用
    $toString
  • $addFields
    使用
    $arrayToObject

  {
    "$group": {
      "_id": {
        "worker_id": "$worker_id",
        "worker_full_name": "$worker_full_name"
      },
      daily_points_stats: {
        $push: {
          k: { $toString: "$date" },
          v: "$attendance_count"
        }
      },
      "present_days": { "$sum": 1 },
      "total_shift_points": { "$sum": "$attendance_points" }
    }
  },
  {
    $addFields: {
      daily_points_stats: {
        $arrayToObject: "$daily_points_stats"
      }
    }
  }