存储在MongoDB中的Node.jsdate对象为“1970-01-01T00:00:00.001Z”

我有一个Node.js应用程序,我正在使用Mongoose与Compose.io上的MongoDB接口。 这里有一些代码应该将当前date和时间存储在我的数据库中:

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){ ... }); }); .... 

如果我打印这些date对象到控制台,我得到正确的date。 但是,当我将对象存储在我的数据库中时,它将存储为“1970-01-01T00:00:00.001Z”。 有什么想法为什么会发生?

问题在于你将volunteer.time_in.push的返回值分配给了volunteer.time_in 。 返回值是数组的新长度,而不是数组本身。

所以改变这一行只是:

 volunteer.time_in.push(date); 

检查数据库中的date格式。 MongoDB的date格式是YYYY-MM-DD