这是我的样本数据库集合:
{
"_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
字段下的字段clock_in_hol
到sch_time_setting
之间,我想计算有多少字段有数据或不为空。因为每个员工都有不同的time_setting,所以这些字段在其他员工中可能只有很少的数据。
此预期的计数为:6,因为只有clock_in_mon
到clock_in_sat
才有数据。
我在这里尝试了代码Count fields in a MongoDB Collection,但我不能在我的案例中做到。
答案 0 :(得分:0)
尝试以下聚合:
<!DOCTYPE html>
<html lang="en">
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<!-- script src="script.js"></script -->
</head>
<body class="no-skin" ng-app="myapp" ng-controller="ctrl">
<table ng-table="tableParams" class="display table table-striped table-bordered">
<thead>
<tr>
<th width="2%">Sr.No</th>
<th width="10%">Category Type <span class="error">*</span></th>
<th width="10%">Product <span class="error">*</span></th>
<th width="10%">Sub Category</th>
</tr>
</thead>
<tr ng-repeat="cap in data">
<td>{{$index+1}}</td>
<td data-title="'Category Type'" sortable="Category Type">
<select class="form-control" id="tags" type="text" ng-model="cap.categoryid" placeholder="Category Type" name="categoryid" required="" ng-init="loadcat()" ng-change="GetProduct(cap.categoryid);GetSubCategory(cap.categoryid,cap.productname)">
<option value="">Select</option>
<option ng-repeat="cat in CategoryArray" value="{{cat.Category_Id}}">{{cat.Category_Name}}</option>
</select>
</td>
<td data-title="'Product'" sortable="Product">
<select class="form-control" type="text" name="productname" ng-model="cap.productname" placeholder="Enter Product Name" required ng-change="GetSubCategory(cap.categoryid,cap.productname)" />
<option value="">select</option>
<option ng-repeat="pro in ProductsArray" value="{{pro.ProductId}}">{{pro.ProductlName}}</option>
</select>
</td>
<td data-title="'Sub Category1'" sortable="Sub Category1">
<select class="form-control" type="text" name="subcategoryid" ng-model="cap.subcategoryid" id="subcategoryid">
<option value="">Select</option>
<option ng-repeat="subcatid in subcatids" value="{{subcatid.Sub_category_Id}}">{{subcatid.Sub_Category_Name}} </option>
</select>
</td>
<td>
<div class="pull-right">
<a ng-click="addFormField()" ng-if="data.length==$index+1" class='btn btn-default btn-xs' autofocus data-dismiss="modal"> <i class="glyphicon glyphicon-plus"></i></a>
<a ng-click="remove_Row($index)" class='btn btn-primary btn-xs' autofocus data-dismiss="modal"> <i class="glyphicon glyphicon-remove"></i></a>
</div>
</td>
</tr>
</table>
</body>
</html>
要分析您的文档,您需要比较键名称。要做到这一点,你必须使用$objectToArray运算符转换并反对键值对列表。然后,您可以找到 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]}] } } } }
}
}
}
}
}
])
名称以key
开头(使用$indexOfBytes)并且值不等于clock_
的那些对。使用$filter你可以摆脱所有其他对,你需要做的就是使用$size来获得数组的长度。
这将为您提供以下结果:
null