【发布时间】:2021-07-27 04:10:18
【问题描述】:
{
"_id": "608c3d353f94ae40aff1dec4",
"userId": "608425c08a3f8db8845bee84",
"experiences": [
{
"designation": "Manager",
"_id": "609197056bd0ea09eee9429c"
},
{
"designation": "Asst. Manager",
"_id": "608c530de8ade5221b0e6d4e"
},
{
"designation": "Sr. Manager",
"_id": "608c534be8ade5221b0e6d4f"
},
]
}
我想删除 id 为 608c530de8ade5221b0e6d4e 的数组中的对象,这是我的代码,但这给了我错误。
这是控制器:
const userId = req.userData.userId;
const id = req.params.id;
Experience.findOneAndUpdate({ userId: userId }, { $pull: { 'experiences': { '_id': id } }}, { multi:true }, function(err, obj) {
// //do something here
});
这是模型:
const newExpSchema = new mongoose.Schema({
designation: { type: String, default: ""},
});
const experienceSchema = new mongoose.Schema({
userId: { type: String, required: true },
experiences: [newExpSchema],
});
export default model("experience", experienceSchema);
我在 { $pull: { 'experiences': { '_id': id } }} 遇到错误 错误:
No overload matches this call.
Overload 1 of 3 .........
.......
The expected type comes from property '$pull' which is declared here on type 'UpdateQuery<Document<any, {}>>'
【问题讨论】:
标签: node.js typescript express mongoose