每次执行时,猫鼬5.5.11中的updateOne命令都会得到响应{“ n”:0,“ nModified”:0,“ ok”:1}!怎么了

时间:2019-05-26 11:01:16

标签: mongodb express mongoose

我正在尝试通过mongoose model.updateOne命令更新MongoDB文档,但是每次执行该命令时,它都会响应{ok:1,n:0,nModified:0}

猫鼬模型:

var mongoose = require('mongoose');
var measurepropsSchema = new mongoose.Schema({
  measureTitle: {
    type: String,
    unique: true,
    required: true
  },
  measureDescription: {
      type:String,
      required:false
  },
  measureSymbol: {
      type: String,
      required:false,
  }
});
var collectionName = 'measureParams'
mongoose.model('Mp', measurepropsSchema, collectionName);

路由器:

routerm.put('/editbyid/:id', ctrlMeasProps.edit);

路由配置:

var routesMea = require('./api/routes/measures');
app.use('/mea', routesMea);

并最终编辑功能:

var Mp = mongoose.model('Mp');
module.exports.edit = function (req, res) {
    var measureParams = new Mp();
    console.log(req.body);
    try{
    measureParams.updateOne(
        { _id: req.params.id },
        {
            measureTitle: req.body.title,
            measureDescription: req.body.description,
            measureSymbol: req.body.symbol
        },
        (err, docu) => {
            console.log(measureParams);
            console.log(err);
            console.log('fin');
            if (err) 
                return res.status(500).send(err);
            return res.send(docu);
        }
    )
    }catch(e){
        console.log(e);
    }
};

我请求了以下链接,

http://localhost:3000/mea/editbyid/5ce8e8555647bf5fb1b51803

请求的主体是这样的JSON对象:

{
    "title": "sth",
    "description": "It`s sth",
    "symbol": "$0"
}

,它会回应:

{
    "n": 0,
    "nModified": 0,
    "ok": 1
}

这是集合“ measureParams”数据:

{ "_id" : ObjectId("5ce8e8375647bf5fb1b51802"), "measureTitle" : "AAAA", "measureDescription" : "It`s AAAA", "measureSymbol" : "%$" }
{ "_id" : ObjectId("5ce8e8555647bf5fb1b51803"), "measureTitle" : "BBBB", "measureDescription" : "It`s BBBB", "measureSymbol" : "@@" }
{ "_id" : ObjectId("5ce8f3b96bcf65fade7c5bcf"), "measureTitle" : "CCCC", "measureDescription" : "It`s CCCC", "measureSymbol" : "$$" }
{ "_id" : ObjectId("5ce90bb3b78b6fbd4b9781ed"), "measureTitle" : "DDDD", "measureDescription" : "It`s DDDD", "measureSymbol" : "#$" }
{ "_id" : ObjectId("5ce90ed9de934a056c00a383"), "measureSymbol" : "Or", "measureDescription" : "It`s EEEE", "measureTitle" : "EEEE", "__v" : 0 }
{ "_id" : ObjectId("5ce912bc5a52000e009b6f2e"), "measureSymbol" : "Ol", "measureDescription" : "It`s FFFF", "measureTitle" : "FFFF", "__v" : 0 }
{ "_id" : ObjectId("5ce937907d656012902702cb"), "measureTitle" : "GGGG", "measureDescription" : "It`s GGGG", "measureSymbol" : "QS", "__v" : 0 }

它应该使用json正文中提到的新值来更新ObjectId 5ce8e8555647bf5fb1b51803的集合,但事实并非如此。 我不知道是什么问题? 你有什么主意吗?

1 个答案:

答案 0 :(得分:0)

updateOne使用模型名称,请尝试更新您的编辑功能和模式文件:

将架构文件中的最后一行更新为此:

 var Mp = module.exports = mongoose.model('Mp', measurepropsSchema, collectionName);

编辑功能应如下所示:

var Mp = require('/path/to/Mp/schema/file');
module.exports.edit = function (req, res) {
    console.log(req.body);
    try{
    Mp.updateOne(
        { _id: req.params.id },
        {
            measureTitle: req.body.title,
            measureDescription: req.body.description,
            measureSymbol: req.body.symbol
        },
        (err, docu) => {
           console.log(err);
           console.log('fin');
           if (err) 
               return res.status(500).send(err);
           return res.send(docu);
        }
     )
    }catch(e){
       console.log(e);
   }
};