Mongo 没有正确存储 unix 时间戳

Mongo not storing unix timestamp properly

我试图在 Mongo 中的日期字段上存储一个 unix 时间戳。如果我保存类似“1450051408”(2015 年 12 月 13 日)的内容,在 Mongo 中它看起来像这样:“1970-01-17 18:47:31.820Z'

这是我的插页:

Service.user.updateMyDate({myDate: moment().unix()});

在我的Mongoose模型中,指定为myDate: Date

unix()方法returnsseconds自Unix纪元以来,但MongoDB使用JavaScript风格毫秒自纪元以来。

最好只获取 moment 使用 toDate():

包装的 JavaScript Date 对象
Service.user.updateMyDate({myDate: moment().toDate()});