【问题标题】:updateOne command in mongoose 5.5.11 every time I execute, I get response { "n": 0, "nModified": 0, "ok": 1 } ! what's going wrong?mongoose 5.5.11 中的 updateOne 命令每次执行时都会得到响应 { "n": 0, "nModified": 0, "ok": 1 } !怎么了?
【发布时间】:2019-10-12 06:03:48
【问题描述】:

我正在尝试通过 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 的集合,但它没有。 我不知道是什么问题? 你有什么想法吗?

【问题讨论】:

  • 你可以试试... measureParams.updateOne({ _id: req.params.id }, { $set: { measureTitle: req.body.title, measureDescription: req.body.description, measureSymbol: req.body.symbol }} ... 吗?
  • 是的。我之前已经做过,但结果是一样的。

标签: mongodb express mongoose


【解决方案1】:

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

【讨论】:

  • @AliMohammadi 如果您喜欢这个解决方案,请点赞。
猜你喜欢
  • 2019-07-15
  • 2019-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-03
  • 2017-11-28
  • 2022-11-14
相关资源
最近更新 更多