【发布时间】:2020-07-23 12:33:36
【问题描述】:
我已经为这个问题准备了 mongoplayground 示例。
https://mongoplayground.net/p/m_G-yBuX6uk
目前它在某些字段上显示基于 15 分钟时间间隔的数据聚合。 有一个名为“presenceStatus”的字段,其值为 0 或 1。
当前结果
{
"accountId": ObjectId("5e1fe45cd05bfb0cc549297d"),
"applicationNumber": 30,
"area": "area2",
"avgZoneCountNumber": 0,
"avgZoneCountNumberInstant": 0,
"buildingId": ObjectId("5e1fe5e3d05bfb0cc5494146"),
"createdAt": ISODate("1970-01-01T00:00:00Z"),
"dateHour": "2020-03-19T18:15:00.000Z",
"deviceId": ObjectId("5e1fe7a9d05bfb0cc5495cdf"),
"gatewayId": ObjectId("5e1fe651d05bfb0cc54947f0"),
"serialNumber": "129300000012",
"spaceType": null
},
预期: 我需要找到每个时间间隔的 occupancyRate。
对于 15 分钟间隔,公式是 count presenceStatus = 1 并除以该时间范围内的总记录数。 occupancyRate 字段的预期值应为 % 格式,如下所示。
预期结果:
{
"accountId": ObjectId("5e1fe45cd05bfb0cc549297d"),
"applicationNumber": 30,
"area": "area2",
"avgZoneCountNumber": 0,
"avgZoneCountNumberInstant": 0,
"buildingId": ObjectId("5e1fe5e3d05bfb0cc5494146"),
"createdAt": ISODate("1970-01-01T00:00:00Z"),
"dateHour": "2020-03-19T18:15:00.000Z",
"deviceId": ObjectId("5e1fe7a9d05bfb0cc5495cdf"),
"gatewayId": ObjectId("5e1fe651d05bfb0cc54947f0"),
"serialNumber": "129300000012",
"spaceType": null,
"occupancyRate": 80%
},
在 Mongo 操场示例(totalPresent 字段)中,我尝试显示存在状态 = 1 的计数,但它显示为 0。
感谢您的帮助。
【问题讨论】:
-
您能否发布一个您所期望的示例
-
我已经用当前和预期的结果更新了问题
标签: mongodb mongoose aggregation-framework