Description
Do you want to request a feature or report a bug?
Probably bug unless I didn't find the related part of the documentation.
What is the current behavior?
We updated mongoose from 5 to 6. Currently using the latest v6.2.5. The code has been updated according to the migration guide and everything is working fine except find queries that we use on a collection that is used by two models using the model's model.discriminator()
function. We have a base model that basically defines base properties, methods and indexes. The two other child models only define a small number of custom properties, no indexes and some specific methods. Before updating to v6 with the latest v5, a find query that simply takes a search string and tries to find all items by e.g. serial number like{ serialNumber: 'sr1233' }
has worked flawlessly. With v6, the query runs but always returns all items in the collection. I tried to run the same query on database level in 3T Studio and it works just fine. Also, nothing has changed on the models so far during the update process. So this seems to be related to mongoose v6. Is there something that I missed? If not, this is likely a bug.
Reproducible example repository
https://github.com/reskume/mongoose-discrm-findmany-bug
What is the expected behavior?
Running a query like { serialNumber: 'sr1233' }
on a collection that is used by two models that inherit from a base model using
discriminator function always returns all items in the collection.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node 16.13.2, Mongoose 6.2.5, MongoDB 5.0
Activity
reskume commentedon Mar 11, 2022
Added link to repo that shows the current behavior. It seems that for queries on collections where discriminator feature is used, the query conditions
{}
is always used, no matter what. This definitely works with the latest v5 version where the configured filters are used for querying.blavka commentedon Mar 11, 2022
It happens even without inheritance, it is enough that the field is not in the schema and you get {} in the query, it is especially insidious, it is in or.
vkarpov15 commentedon Mar 19, 2022
Unfortunately this is expected behavior, see the
strictQuery
note in the Mongoose 6 migration guide.You can change this behavior to work like it did in Mongoose 5.x using:
reskume commentedon Mar 19, 2022
vkarpov15 commentedon Mar 27, 2022
It affects all models. The inheritance part doesn't matter in this case, because when you execute a query on the base model, fields that are only defined on the child model are not defined on the base model.
strictQuery
false
by default again #11861