如何在MongoDB中设置新的Date()?

我已经尝试了很多的组合,并检查了MongoDB 3.2文档,但我仍然没有得到我的数据库中存储新的Date()。 下面的其他一切都正确地存储。 提前感谢您的任何提示!

来自我的app.js文件的代码:

db.collection('users').update({user: req.user.username}, { $push: { "recentPlays": { "quiz": req.session.mostRecentQuiz, "score": score } } },{ $set: { "mostRecentQuiz.quizObj": req.session.mostRecentQuiz, "mostRecentQuiz.time" : new Date() // StackOverflow: this is the part that is not getting stored in the DB. } }, function (err, result) { if (err) throw err; console.log(result); }); 

你的更新方法被调用的方式是错误的,更新操作符$set$push应该在同一个文档中。 重新编写你的方法:

 db.collection('users').update( { "user": req.user.username }, { "$push": { "recentPlays": { "quiz": req.session.mostRecentQuiz, "score": score } } "$set": { "mostRecentQuiz.quizObj": req.session.mostRecentQuiz, "mostRecentQuiz.time" : new Date() } }, function (err, result) { if (err) throw err; console.log(result); } );