mongoDB: Show MinuteBucket in Time value as ending time for the interval

时光怂恿深爱的人放手 提交于 2020-04-16 09:18:47

问题


I have created Mongo PlayGround

Currently dateHour field is dividing time by 15min interval.

For example - if

`"updatedAt": ISODate("2020-03-20T18:16:50.000Z")` 

it shows as :

 "dateHour": ISODate("2020-03-20T18:15:00Z"),

so First 15min bucket is represented as 18:00:000z (this covers minutes from 1 to 15)

second 15min bucket is represented as 18:15:000z (this covers minutes from 15 to 30) and so on....

Expected -

First 15min bucket should be displayed as 18:15:000z (this covers minutes from 1 to 15) second 15min bucket should be displayed as 18:30:000z (this covers minutes from 15 to 30) and so on....

Basically it should show ending time for that interval


回答1:


You're using $trunc which is this case converts the result of $divide into lower minuteBucket, you can use $ceil instead:

minuteBucket: {
    $ceil: {
        $divide: [ { $minute: "$updatedAt"  },  15.0  ]
    }
},

Mongo Playground



来源:https://stackoverflow.com/questions/61163597/mongodb-show-minutebucket-in-time-value-as-ending-time-for-the-interval

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!