【问题标题】:Count number of fields have data in a collection with aggregate mongodb统计字段数在具有聚合 mongodb 的集合中具有数据
【发布时间】:2018-03-01 06:10:43
【问题描述】:

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

{ 
    "_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_mon 字段sch_time_setting 字段下的clock_in_hol 之间,我想计算有多少字段有数据或不为空。因为每个员工都有不同的time_setting,所以这些字段在其他员工中可能很少有数据。

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

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

【问题讨论】:

  • 显示您当前的尝试

标签: mongodb count field aggregate


【解决方案1】:

尝试以下聚合:

    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 } ] }

【讨论】:

  • 谢谢 mickl,但我只想计算从 'clock_in_mon' 到 'clock_in_hol' 的字段。不包括打卡。所以预期的计数必须是 6。
  • @noyruto88,修改了我的答案
  • 谢谢@mickl。我现在明白了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-08
  • 2015-12-18
  • 1970-01-01
  • 2020-08-14
  • 2014-12-12
  • 1970-01-01
相关资源
最近更新 更多