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
Comments
Added link to repo that shows the current behavior. It seems that for queries on collections where discriminator feature is used, the query conditions |
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. |
Unfortunately this is expected behavior, see the 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'); |
Thanks for the clarification. I read the migration notes and the part with
the `strictQuery` option. The example is clear since it uses only a single
model. From the example, I didn't realize this affects models using
inheritance. Maybe the docs should be updated with this specific behaviour?
Valeri Karpov ***@***.***> schrieb am Sa., 19. März 2022,
17:14:
… Unfortunately this is expected behavior, see the strictQuery note in the
Mongoose 6 migration guide
<https://mongoosejs.com/docs/migrating_to_6.html#strictquery-is-removed-and-replaced-by-strict>
.
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');
—
Reply to this email directly, view it on GitHub
<#11514 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABROSQALENZZPMNR3HFRV4LVAX4O5ANCNFSM5QNONWJQ>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
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. |
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 usingdiscriminator 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
The text was updated successfully, but these errors were encountered: