统计集合中有数据的字段数 mongodb

Count number of fields have data in a collection with aggregate mongodb

这是我的示例数据库集合:

{ 
    "_id" : ObjectId("5a9797b480591678e0771190"), 
    "staff_id" : NumberInt(172), 
    "temp_name" : "Regular Employment", 
    "individual_setting" : false, 
    "employment_category" : "Regular Employee", 
    "branch_office" : "Cebu Branch Office", 
    "availability_status" : "Incumbent", 
    "req_working_hours" : "08:00", 
    "fixed_brk_time_from" : "12:00", 
    "fixed_brk_time_to" : "13:00", 
    "sch_time_setting" : [
        {
            "holiday" : [
                "Saturday", 
                "Friday"
            ], 
            "biweekly_odd" : [

            ], 
            "biweekly_even" : [
                "Saturday"
            ], 
            "clock_in_mon" : "08:40", 
            "clock_in_tue" : "08:40", 
            "clock_in_wed" : "08:40", 
            "clock_in_thu" : "08:40", 
            "clock_in_fri" : "08:40", 
            "clock_in_sat" : "08:40", 
            "clock_in_sun" : null, 
            "clock_in_hol" : null, 
            "clock_out_mon" : "18:00", 
            "clock_out_tue" : "18:00", 
            "clock_out_wed" : "18:00", 
            "clock_out_thu" : "18:00", 
            "clock_out_fri" : "18:00", 
            "clock_out_sat" : "18:00", 
            "clock_out_sun" : null, 
            "clock_out_hol" : null, 
            "_id" : ObjectId("5a9797b480591678e077118f")
        }
    ], 
    "date_to_start" : ISODate("2018-03-01T06:03:32.050+0000"), 
    "createdAt" : ISODate("2018-03-01T06:03:32.066+0000"), 
    "updatedAt" : ISODate("2018-03-01T06:03:32.066+0000"), 
    "__v" : NumberInt(0)
}

sch_time_setting字段下的clock_in_monclock_in_hol字段之间,我想统计有多少字段有数据或不为空。因为每个员工的数据都不一样time_setting,所以这些字段在其他员工中的数据可能很少。

预期的计数是:6,因为只有 clock_in_monclock_in_sat 有数据。

我尝试了此处的代码 Count fields in a MongoDB Collection,但我无法做到。

尝试以下聚合:

    db.yourCollectionName.aggregate([
    {
        $project: {
            totalClockIns: {
              $map: {
                   input: "$sch_time_setting",
                   as: "setting",
                   in: {
                      _id: "$$setting._id",
                      kvPairs: {
                        $objectToArray: "$$setting"
                      }
                   }
                }
            }
        }
    },
    {
        $project: {
            totalClockIns: {
                $map: {
                   input: "$totalClockIns",
                   as: "clockIn",
                   in: {
                      _id: "$$clockIn._id",
                      count: { 
                        $size: { $filter: { input: "$$clockIn.kvPairs", as: "pair", cond: { $and: [
                        {$eq: [{ $indexOfBytes: [ "$$pair.k", "clock_in_" ] },0]},{$ne: ["$$pair.v",null]}] } } } }
                    }
                }
            }
        }
    }
])

要分析您的文档,您需要比较键名。为此,您必须使用 $objectToArray operator which transforms and object to a list of key-value pairs. Then you can find those pairs where key name starts with clock_ (using $indexOfBytes) and where value is not equal null. Using $filter you can get rid of all the other pairs and the all you need to do is to use $size 来获取数组的长度。

这会给你以下结果:

{ "_id" : ObjectId("5a9797b480591678e0771190"), "totalClockIns" : [ { "_id" : ObjectId("5a9797b480591678e077118f"), "count" : 12 } ] }