如何在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); } );
- Express.js服务器端渲染 – 请求'/ json / version /
- Node.js Express res.json到html页面?
- Node JS / Express:如何parsing外部API调用的多部分响应
- 重构反应项目,使其从数据库中加载数据
- 如何使用Node-Express统计CSV文件的行数
- 在nodejs express中运行https服务器时,ERR_SSL_PROTOCOL_ERROR浏览器错误消息
- 在Node express应用程序中安装Entrust SSL证书
- 在没有Ajax的情况下在angularjs和express / node之间传递数据
- 路由Express和输出到模板