繁体   English   中英

计算具有聚集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)
}

外地之间clock_in_monclock_in_holsch_time_setting领域,我想看看有多少领域有数据或不为空。 由于每个员工都有不同的时间设置,因此这些字段在其他员工中可能没有多少数据。

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

我从这里尝试了代码,然后在MongoDB集合中对字段进行计数,但就我而言无法做到。

尝试以下聚合:

    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运算符,该运算符会将和转换为键值对列表。 然后,您可以找到key名称以clock_ (使用$ indexOfBytes )并且值不等于null那些对。 使用$ filter可以消除所有其他对,而您所需要做的就是使用$ size获取数组的长度。

这将为您带来以下结果:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM