Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find query on collection with two models using inheritance (discriminator) always returns all items in collection #11514

Closed
reskume opened this issue Mar 10, 2022 · 5 comments
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@reskume
Copy link

reskume commented Mar 10, 2022

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

@reskume
Copy link
Author

reskume commented 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.

@IslandRhythms IslandRhythms added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Mar 11, 2022
@blavka
Copy link

blavka commented 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 vkarpov15 added this to the 6.2.8 milestone Mar 14, 2022
@vkarpov15
Copy link
Collaborator

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:

const mongoose = require('mongoose');

mongoose.set('strictQuery', false); // <-- add this line before compiling your models

const Atm = require('./src/atm-model');
const Ats = require('./src/ats-model');
const Ats3 = require('./src/ats3-model');
const Ats4 = require('./src/ats4-model');

@vkarpov15 vkarpov15 added help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary and removed confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. labels Mar 19, 2022
@vkarpov15 vkarpov15 removed this from the 6.2.8 milestone Mar 19, 2022
@reskume
Copy link
Author

reskume commented Mar 19, 2022 via email

@vkarpov15
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

4 participants