[英]Remove all elements in string array Mongoose schema
I'm trying to remove all of strings that match occurrences in the array of 'interestedStudents' in a Mongoose schema.我正在尝试删除与 Mongoose 模式中的“interestedStudents”数组中的匹配项匹配的所有字符串。
My Mongoose schema looks like this:我的 Mongoose 架构如下所示:
// Create a schema.
const schema = new mongoose.Schema<Post>({
interestedStudents: {
type: [{
type: String,
unique: true
}],
required: false,
},
})
//Create model
export const PostModel = model<Post>('Post', schema);
I'm trying to remove by using:我正在尝试使用以下方法删除:
await PostModel.updateMany({ interestedStudents: { $pullAll : [userId]}})
But I'm getting the following error:但我收到以下错误:
"CastError: Cast to [string] failed for value "[ { '$pullAll': [ '62854109cf9a6db1fcf0393b' ] } ]" (type string) at path "interestedStudents.0" because of "CastError"\n at model.Query.exec “CastError: Cast to [string] failed for value "[ { '$pullAll': [ '62854109cf9a6db1fcf0393b' ] } ]" (type string) 在路径 "interestedStudents.0" 因为 "CastError"\n 在 model.Query。执行
What am I doing wrong?我究竟做错了什么? Is my Schema set up wrong?我的架构设置错了吗? Maybe it's not an array of string?也许它不是一个字符串数组?
对于其他来到这里的人来说,这很容易:
const { modifiedCount } = await PostModel.updateMany({}, { $pull: { interestedStudents: userId } })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.