node.js - 无法通过 id 找到文档
问题描述
所以我正在尝试更新文档,但它不适用于 _id 但如果我使用“名称”过滤它也可以使用 FindByID 并使用 mongoose 版本 5.0.18 返回 null
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/mongo-exercises')
.then(() => console.log('connected to database :)'))
.catch(reason => console.log('Can not connect to data base',reason));
const courseSchema = mongoose.Schema({
name: String,
author: String,
tags: [String],
Date: { type:Date,default:Date.now },
isPublished: Boolean,
price: Number
});
const Course = mongoose.model('courses',courseSchema);
async function updateCourse(id){
let result = await Course.update({_id: id},{
$set:{
author: "some random dude",
isPublished: true
}
});
console.log('result: ',result);
}
updateCourse('5a68fde3f09ad7646ddec17e');
解决方案
尝试使用
//make sure you import Course module
Course.findByIdAndUpdate(id)
.then(response=>{
})
.catch(err=>{
})
推荐阅读
- javascript - 谷歌标签管理器帮助选择标签和触发器
- vert.x - 当 Verticles 被分配给事件循环线程时,那么事件循环在内部的工作是什么?
- javascript - 使用正则表达式表达路由器以匹配多个路径
- c++ - 带有 C++ 控制台程序的 USB 条码扫描器
- javascript - 将两个变量与数值相乘时函数不会返回整数(JS)
- python - 使用python迭代器__next__读取文本,但在读取第一行后停止
- python - python解释器是否隐式使用中国剩余定理?
- amazon-web-services - AWS CDK 是否支持为 AWS Step Functions 指定 ResultSelector?
- python - python “scpy.io.savemat” 将自动嵌套列表
- python - pygame中的屏幕不刷新