简体   繁体   English

如何使用过滤器 _id 找到一条记录并使用 nodejs 和 mongodb,mongoose 更新该记录

[英]how can i find one record with the filter _id and update that record using the nodejs and mongodb,mongoose

I was trying to update a student record to mongo db using nodejs.我试图使用 nodejs 将学生记录更新到 mongo db。 I used the method find One And Update my filter is studentId, student is my schema instance which have the doc for updating.我使用方法 find One And Update 我的过滤器是 studentId,student 是我的模式实例,它有用于更新的文档。 I tried the below code but got error as typeerror student.findOneAndUpdate is not a function.我尝试了以下代码,但由于 typeerror student.findOneAndUpdate is not a function 而出现错误。

var student = new Student();
student._id = mongoose.Types.ObjectId(_eId);
student.name = name;
student.studentId = id;
student.status = status;
updateToDb(student);

function updateToDb(student){ 
    console.log(student._id+'      studentId')
    var studentId = student._id;
    var filter = { _id: studentId };
    student.findOneAndUpdate(filter,(err,student)=> {
       if(!err){
        console.log('insertion to db sucess')  
       }
       else{
          console.log('insertion failed '+err);
       }
    })
}

Issue with your code is you're trying to club two different functionalities given in Mongoose .save() which works on mongoose model's instances ie;您的代码的问题是您正在尝试结合 Mongoose .save()中给出的两种不同功能,该功能适用于 mongoose 模型的实例,即; documents & .findOneAndUpdate() which works directly on mongoose models.文档和.findOneAndUpdate()直接在 mongoose 型号上工作。

So if you've all the filters & changes you would directly use .findOneAndUpdate() function on mongoose model to update an existing document:因此,如果您拥有所有过滤器和更改,您将直接在 mongoose model 上使用.findOneAndUpdate() function 来更新现有文档:

let student = {
  _id: mongoose.Types.ObjectId(_eId),
  name: name,
  studentId: id,
  status: status,
};

updateToDb(student);

/** passing in .Js Object as input */
function updateToDb(studentInput) {
  console.log(studentInput._id + " studentId");
  var studentId = studentInput._id;
  var filter = { _id: studentId };
  delete studentInput._id; // Deleting `_id` from studentInput as it shouldn't be there in update object
  Student.findOneAndUpdate(filter, studentInput, (err, student) => {
    if (!err) {
      console.log("insertion to db sucess");
    } else {
      console.log("insertion failed " + err);
    }
  });
}

So with .findOneAndUpdate() we'll pass { multi: true } option to insert a new document if no document has matched with filter part.因此,使用.findOneAndUpdate()如果没有文档与过滤器部分匹配,我们将通过{ multi: true }选项插入新文档。 But with .save() we can actually use it for new insert & updates as well, If _id exists in input request & a document in DB matches with it then .save() will be considered as update operation otherwise it will treated as insert operation, But it doesn't work on mongoose models it will only work on instances of mongoose models.但是使用.save()我们实际上也可以将其用于新的插入和更新,如果输入请求中存在_id并且数据库中的文档与之匹配,则.save()将被视为更新操作,否则将被视为插入操作,但它不适用于 mongoose 型号,它仅适用于 mongoose 型号的实例。

/** Here since you're passing in instance of mongoose model then use .save() */
var student = new Student();
student._id = mongoose.Types.ObjectId(_eId);
student.name = name;
student.studentId = id;
student.status = status;
updateToDb(student);

function updateToDb(student) {
  console.log(student._id + "      studentId");
  student.save((err, student) => {
    if (!err) {
      console.log("insertion to db sucess");
    } else {
      console.log("insertion failed " + err);
    }
  });

Ideally .save() is used on updates when you first read a document from DB & then make necessary changes, as mongoose tracks changes occurring to document at the end all you need to do is to apply .save() on that doc.理想情况下,当您第一次从 DB 读取文档然后进行必要的更改时使用.save()进行更新,因为 mongoose 会在最后跟踪文档发生的更改,您只需在该文档上应用.save()即可。

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

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