使用Mongoose / MongoDB的findOne不返回行

时间:2017-08-11 09:26:30

标签: node.js mongodb mongoose

我有以下设置。

me =是来自请求的传入数据,但由于在此示例中未使用,我正在跳过信息。

添加了我想要的findAndUpdate。为什么在findAndUpdate上进行插入而不是更新?

app.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 }

Mongoose调试:

[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 })

1 个答案:

答案 0 :(得分:0)

如果您已导出mongoose模型,请使用以下代码

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)
  })

})