Node.js 如何在MongoDB中设置新日期()?
我尝试了很多组合并检查了MongoDB 3.2文档,但我仍然没有将新的Date()存储在数据库中。下面的所有内容都已妥善存储。提前感谢您提供的任何提示 my app.js文件中的代码:Node.js 如何在MongoDB中设置新日期()?,node.js,mongodb,express,Node.js,Mongodb,Express,我尝试了很多组合并检查了MongoDB 3.2文档,但我仍然没有将新的Date()存储在数据库中。下面的所有内容都已妥善存储。提前感谢您提供的任何提示 my app.js文件中的代码: db.collection('users').update({user: req.user.username}, { $push: { "recentPlays": {
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);
}
);
尝试存储时间戳而不是对象:“Date.now()”而不是“new Date()”@Nosyara谢谢您的回复。我刚试过Date.now(),但它也不起作用。“new Date()”在我的应用程序中的其他位置有效。