繁体   English   中英

删除字符串数组 Mongoose 模式中的所有元素

[英]Remove all elements in string array Mongoose schema

我正在尝试删除与 Mongoose 模式中的“interestedStudents”数组中的匹配项匹配的所有字符串。

我的 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);

我正在尝试使用以下方法删除:

await PostModel.updateMany({ interestedStudents: { $pullAll : [userId]}})

但我收到以下错误:

“CastError: Cast to [string] failed for value "[ { '$pullAll': [ '62854109cf9a6db1fcf0393b' ] } ]" (type string) 在路径 "interestedStudents.0" 因为 "CastError"\n 在 model.Query。执行

我究竟做错了什么? 我的架构设置错了吗? 也许它不是一个字符串数组?

对于其他来到这里的人来说,这很容易:

const { modifiedCount } = await PostModel.updateMany({}, { $pull: { interestedStudents: userId } })

暂无
暂无

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

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