我有一个 Node.js 应用程序,我正在使用 Mongoose 与 Compose.io 上的 MongoDB 进行交互。以下是一些应将当前日期和时间存储在我的数据库中的代码:
signup.Volunteer.find({_id : uniqueid.toObjectId()}, function(err, doc){
var volunteer = doc[0];
var date = new Date();
console.log(date.toString());
//volunteer.time_out is an array
//defined in Volunteer Schema as: 'time_in : [Date]'
volunteer.time_in = volunteer.time_in.push(date);
volunteer.save(function(err){
...
});
});
....
如果我将这些日期对象打印到控制台,我就会得到正确的日期。但是,当我将该对象存储在数据库中时,它存储为“1970-01-01T00:00:00.001Z”。知道为什么会发生这种情况吗?
最佳答案
问题在于您将 volunteer.time_in.push
的返回值分配回 volunteer.time_in
。返回值是数组的新长度,而不是数组本身。
因此将该行更改为:
volunteer.time_in.push(date);
关于Node.js 日期对象存储在 MongoDB 中为 "1970-01-01T00:00:00.001Z",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29884986/