Skip to content

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

Closed
@reskume

Description

@reskume

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

reskume commented on Mar 11, 2022

@reskume
Author

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.

added
has repro scriptThere is a repro script, the Mongoose devs need to confirm that it reproduces the issue
confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.
and removed
has repro scriptThere is a repro script, the Mongoose devs need to confirm that it reproduces the issue
on Mar 11, 2022
blavka

blavka commented on Mar 11, 2022

@blavka

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.

added this to the 6.2.8 milestone on Mar 14, 2022
vkarpov15

vkarpov15 commented on Mar 19, 2022

@vkarpov15
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');
added
helpThis issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
and removed
confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.
on Mar 19, 2022
removed this from the 6.2.8 milestone on Mar 19, 2022
reskume

reskume commented on Mar 19, 2022

@reskume
Author
vkarpov15

vkarpov15 commented on Mar 27, 2022

@vkarpov15
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    helpThis issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @vkarpov15@reskume@blavka@IslandRhythms

        Issue actions

          Find query on collection with two models using inheritance (discriminator) always returns all items in collection · Issue #11514 · Automattic/mongoose