【问题标题】:How to update document in mongodb with node js如何使用节点 js 更新 mongodb 中的文档
【发布时间】:2021-07-30 17:19:57
【问题描述】:

我正在尝试使用节点 js 更新 mangodb 数据库中的一些数据,但文档不会更新。我收到此日志消息{ n: 0, nModified: 0, ok: 1 } 这是我的代码:

我成功连接到数据

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/cinemas')
        .then(()=> console.log('connected to mongoDB....'))
        .catch(err => console.log('Could not connect to mongoDB because',err));

这是架构

const moviesSchema = new mongoose.Schema({
  title: String,
  acotrs:[String],
  genre: [String],
  date: {type: Date, default: Date.now},
  onCinema :Boolean
});

模型连接

const Movie = mongoose.model('Movie',movieSchema);

我可以毫无问题地搜索数据库,唯一的问题是更新功能不会更新数据库。这是函数:

async function updateMovie(id){
  const result = await Movie.update({_id: id},{
    $set:{
      title: 'New Movie',
      onCinema: false
    }
  });
  console.log(result);
}
updateMovie('5a68fdf95db93f6477053ddd'); 

我在控制台{ n: 0, nModified: 0, ok: 1 } 上收到此日志,告诉我没有更新。请帮忙。我做错了什么?

【问题讨论】:

  • { n: 0 } 表示数据库没有为您的查询找到任何匹配项,您确定存在以5a68fdf95db93f6477053ddd 为id 的文档吗?
  • @AsafAviv 是的,有一个具有该 ID 的文档
  • 尝试运行const result = await Movie.findById('5a68fdf95db93f6477053ddd'); console.log(result); 是否找到文档?
  • @AsafAviv 我没有从控制台获得日志,只有连接到数据库的消息
  • 该文档是否存在于movies 集合中?

标签: javascript node.js mongodb mongoose


【解决方案1】:
const result = await Movie.update({_id: id},{
    $set:{
      title: 'New Movie',
      onCinema: false
    }
  });

更新函数给出这样的输出。

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

尝试使用 findByIdAndUpdate

const result = await Movie.findByIdAndUpdate(id,{
    $set:{
      title: 'New Movie',
      onCinema: false
    }
  });

它将以 json 格式给出准确的结果。

【讨论】:

    【解决方案2】:

    您提供的 id 是字符串类型,_id 是 ObjectId,因此添加 mongoose.Types.ObjectId(id) 应该可以解决问题。

        async function updateMovie(id){
      const result = await Movie.update({_id: mongoose.Types.ObjectId(id)},{
        $set:{
          "title": 'New Movie',
          "onCinema": false
        }
      });
      console.log(result);
    }
    updateMovie('5a68fdf95db93f6477053ddd');
    

    【讨论】:

    • 我做了但仍然有相同的输出{ n: 0, nModified: 0, ok: 1 }
    • 我刚刚编辑了答案你试过这样吗? $set:{ "title": '新电影', "onCinema": false }
    【解决方案3】:

    试试这个:

    `MongoClient.connect(url, { useNewUrlParser: true }, (err, client) => { 
         let queryId = { _id: this.id }; //Get the ID of the object
         let myObj = { 
             $set: { 
               name: 'Somename' //Whatever you want to change for that ID
             }
         var db = client.db("database_name");
         db.collection("collection_name").updateOne(queryId, myObj, (err, res) => {
             if (err) {
                 console.log("not Updated");
             }
             else{
                console.log("Updated");
             }
         });
    }`
    

    如果您有任何其他问题,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-15
      • 2020-02-06
      • 2015-09-10
      • 2019-09-15
      • 2021-11-11
      • 1970-01-01
      • 2018-07-21
      • 2017-01-06
      相关资源
      最近更新 更多