$millisecond(聚合)
定义
$millisecond
返回日期的毫秒部分,作为介于0到999之间的整数。
表达式
$millisecond
表达式具有以下操作表达式语法:{ $millisecond: <dateExpression> } 参数可以是
行为
示例 | 结果 | ||||
---|---|---|---|---|---|
| 0 | ||||
| 0 | ||||
| 0 | ||||
| 0 | ||||
| 0 | ||||
| 错误 | ||||
| 错误 | ||||
| 错误 |
注意
毫秒不能接受字符串作为参数。
示例
考虑一个具有以下文档的 sales
集合
{ "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-01-01T08:15:39.736Z") }
以下聚合使用了 $millisecond
和其他日期运算符来分解 date
字段
db.sales.aggregate( [ { $project: { year: { $year: "$date" }, month: { $month: "$date" }, day: { $dayOfMonth: "$date" }, hour: { $hour: "$date" }, minutes: { $minute: "$date" }, seconds: { $second: "$date" }, milliseconds: { $millisecond: "$date" }, dayOfYear: { $dayOfYear: "$date" }, dayOfWeek: { $dayOfWeek: "$date" }, week: { $week: "$date" } } } ] )
操作返回以下结果
{ "_id" : 1, "year" : 2014, "month" : 1, "day" : 1, "hour" : 8, "minutes" : 15, "seconds" : 39, "milliseconds" : 736, "dayOfYear" : 1, "dayOfWeek" : 4, "week" : 0 }