我已经尝试了相当多的组合并检查了 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);
}
);
关于node.js - 如何在 MongoDB 中 $set new Date() ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40028977/