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)})
And all of my data got deleted
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)