简体   繁体   中英

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

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这发生过

And all of my data got deleted删除前后

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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