我有以下设置。
me = 是来自请求的传入数据,但由于此示例中未使用它,因此我跳过了该信息。
添加了我正在尝试做的查找和更新。为什么在查找和更新上进行了插入而不是更新?
应用.js
mongoose.connect("mongodb://localhost/time_report");
var reportSchema = new mongoose.Schema({
name: String,
month: String,
date: String,
hours: Number,
dayNumber: Number,
day: String
});
var time = mongoose.model("time", reportSchema);
查询:
app.get("/test", function(req, res) {
var me = new time({
name: info[i + 3],
month: info[i + 1],
date: info[i + 2],
hours: info[i],
dayNumber: info[i + 4],
day: info[i + 5]
});
time.findOneAndUpdate(query, update, options, function(err, data) {
if (err) {
console.log(err);
}
console.log("data: " + data);
found = true;
});
}
.....更多的东西和听众
在MongoDB中:
{ "_id" : ObjectId("598d71b20f47692f48eb7393"), "name" : "jocke", "month" : "2017-08", "date" : "2017-08-01", "hours" : 8, "dayNumber" : 2, "day" : "Tuesday", "__v" : 0 }
猫鼬调试:
[0;36mMongoose: times.findAndModify({ date: '2017-08-31' }, [], { '$set': { hours: null } }, { new: false, upsert: false, fields: {} })
[0;36mMongoose: times.insert({ name: '', month: '2017-08', date: '2017-08-31', hours: null, dayNumber: 4, day: 'Thursday', _id: ObjectId("598d7ef69386343b3035192f"), __v: 0 })
如果您导出了猫鼬模型,只需使用以下代码
var time = require('./models/time');
app.get('/test', function(req, res){
// change the search object as per your need like month=2018/07
time.findOne(req.body, function(err, doc){
if(err) return handleError(err);
res.send(doc)
})
})