Home > Blockchain >  Why did the data get deleted when the condition did not match in the query
Why did the data get deleted when the condition did not match in the query

Time:06-18

I have this schema in my mongo db.

const movieSchema = new mongoose.Schema({
    title: String,
    year: Number,
    score: Number,
    rating: String
})

When I tried to delete collections with year greater than 1999, I have mistakenly put the condition as:

 Movie.deleteMany({yeaer: {$gte: 1999}}).then(res => {console.log(res)})

This happened This happened

And all of my data got deleted Before and after deletion

CodePudding user response:

If you omit the conditional property from deleteMany(), mongoose will delete all documents from the model. The same is true if you misspell the conditional. It's a rather dangerous default behaviour but you can guard yourself by disabling strict querying mode in mongoose:

mongoose.set('strictQuery', false)
  • Related