我如何使用过滤器 _id 找到一条记录并使用 nodejs 和 mongodb,mongoose 更新该记录
how can i find one record with the filter _id and update that record using the nodejs and mongodb,mongoose
我正在尝试使用 nodejs 将学生记录更新到 mongo 数据库。我使用了方法 find One And Update 我的过滤器是 studentId,student 是我的模式实例,它有用于更新的文档。我尝试了以下代码,但出现错误,因为 typeerror student.findOneAndUpdate 不是函数。
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);
}
})
}
您的代码存在问题,您正在尝试将 Mongoose .save() which works on mongoose model's instances i.e; documents & .findOneAndUpdate() 中提供的两种不同功能结合起来,这两种功能可直接在 Mongoose 模型上运行。
因此,如果您拥有所有过滤器和更改,您将直接在 mongoose 模型上使用 .findOneAndUpdate()
函数来更新现有文档:
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);
}
});
}
所以对于 .findOneAndUpdate()
我们将传递 { multi : true }
选项以在没有文档与过滤器部分匹配时插入新文档。但是使用 .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);
}
});
理想情况下,当您首先从数据库读取文档然后进行必要的更改时,.save()
用于更新,因为猫鼬会跟踪文档最后发生的更改,您需要做的就是应用 .save()
在该文档上。
我正在尝试使用 nodejs 将学生记录更新到 mongo 数据库。我使用了方法 find One And Update 我的过滤器是 studentId,student 是我的模式实例,它有用于更新的文档。我尝试了以下代码,但出现错误,因为 typeerror student.findOneAndUpdate 不是函数。
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);
}
})
}
您的代码存在问题,您正在尝试将 Mongoose .save() which works on mongoose model's instances i.e; documents & .findOneAndUpdate() 中提供的两种不同功能结合起来,这两种功能可直接在 Mongoose 模型上运行。
因此,如果您拥有所有过滤器和更改,您将直接在 mongoose 模型上使用 .findOneAndUpdate()
函数来更新现有文档:
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);
}
});
}
所以对于 .findOneAndUpdate()
我们将传递 { multi : true }
选项以在没有文档与过滤器部分匹配时插入新文档。但是使用 .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);
}
});
理想情况下,当您首先从数据库读取文档然后进行必要的更改时,.save()
用于更新,因为猫鼬会跟踪文档最后发生的更改,您需要做的就是应用 .save()
在该文档上。