简体   繁体   English

如何使用节点js更新mongodb中的文档

[英]How to update document in mongodb with node js

Am trying to update some data in the mangodb database using node js but the document won't update.我正在尝试使用 node js 更新 mangodb 数据库中的一些数据,但文档不会更新。 I get this log message { n: 0, nModified: 0, ok: 1 } Here is my code:我收到此日志消息{ n: 0, nModified: 0, ok: 1 }这是我的代码:

I connect successfully to the data我成功连接到数据

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

Here is the schema这是架构

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

Model connection模型连接

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

I can search the database with no issues, the only issue is the update function which does not update the database.我可以毫无问题地搜索数据库,唯一的问题是不更新数据库的更新功能。 Here is the function:这是函数:

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

I get this log on the console { n: 0, nModified: 0, ok: 1 } which tells me nothing was updated.我在控制台上得到这个日志{ n: 0, nModified: 0, ok: 1 }这告诉我没有任何更新。 Please help.请帮忙。 What am I doing wrong?我究竟做错了什么?

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

The update function gives output like this.更新函数给出这样的输出。

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

try with the findByIdAndUpdate尝试使用findByIdAndUpdate

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

it will give the exact results in json format.它将以 json 格式给出确切的结果。

The id you have given is in string type and _id is an ObjectId so adding mongoose.Types.ObjectId(id) should solve the issue.您提供的 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');

Try This:试试这个:

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

Let me know if you have any other questions.如果您有任何其他问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM