Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
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.

My Mongoose schema looks like this:

// 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

What am I doing wrong? Is my Schema set up wrong? Maybe it's not an array of string?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It was as easy as this for anyone else coming here:

const { modifiedCount } = await PostModel.updateMany({}, { $pull: { interestedStudents: userId } })
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!