How to avoid accidentally returning an arbitrary document when using findOne with a non-existing field in Mongoose?

Hi @Florian_Walther,

Thank you for finding and sharing the solution for this default Mongoose behaviour, which is definitely a very unexpected deviation from the normal MongoDB driver behaviour.

It looks like the strictQuery behaviour changed in Mongoose 6 and this has caused some confusion.

The Mongoose maintainer has created an issue to have strictQuery off by default in Mongoose 7 (as it was prior to Mongoose 6), so you may want to watch & upvote Make `strictQuery` `false` by default again · Issue #11861 · Automattic/mongoose · GitHub.

Regards,
Stennie

1 Like