Very-very wrong behavior of mongoose version 6 function deleteMany

Such situation.

There is a collection with documents in the database,
we created Schema and created Model.
At the same time, not all fields of documents are described in the schema
i.e. some documents have fields not described in the schema.
Now we execute the model.deleteMany({x:}) where “x” is not in the schema
This call removes ALL documents in the collection!
This behavior was not in version 5

This behavior is not described in Mongoose v6.5.4: Migrating to Mongoose 6
and it looks like it’s just an error.

Welcome to the MongoDB community @Vadim_Shumilin !

I suspect this is another consequence of Mongoose 6 changing the strictQuery behaviour so query filter properties that are not in the schema will now be filtered out by default :scream:.

This is unexpected behaviour that is inconsistent with the official MongoDB Node.js driver behaviour (related discussion: How to avoid accidentally returning an arbitrary document when using findOne with a non-existing field in Mongoose? - #7 by Stennie).

Mongoose is an open source community project, so the best channel to search or create Mongoose bug reports is the GitHub issue queue: Issues · Automattic/mongoose · GitHub.

I see you have already created Mongoose #12389 for this deleteMany() issue. I think a key upstream issue to watch/upvote is Mongoose #11861: Make strictQuery false by default again.


1 Like

I only recently work with mongoose, and when I came across this error - I was very surprised by this behavior, and began to look for where to report it. I found this community , but after two days I did not find the reaction, and further search led to GitHub issue.
Now the situation is clear to me, I still think that such behavior is very dangerous, and at least it should have been written about separately in Migrating to Mongoose 6 document.