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

Query for models with embedded discriminator is not working #12908

Closed
2 tasks done
pwiebe opened this issue Jan 13, 2023 · 2 comments
Closed
2 tasks done

Query for models with embedded discriminator is not working #12908

pwiebe opened this issue Jan 13, 2023 · 2 comments
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@pwiebe
Copy link

pwiebe commented Jan 13, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

6.8.3

Node.js version

18.12.0

MongoDB server version

4.4

Typescript version (if applicable)

No response

Description

When querying fields in a sub-model that is an embedded discriminator, no filtering is done. I.e. If the ids property is an array of a sub-model that has a discriminator, then all fields are returned, not the one filtered on.

Given the model in the repro steps, the command Testr.find({'ids.email.address': 'test@example.com'}) returns all items in the DB, not the filtered ones.

This was working in 5.13.15. Notice that running the same query directly on the MongoDB connection produces the desired result.

Steps to Reproduce

  1. Create the following model:
const identity = new Schema({ primary: { type: Boolean, default: false } }, { discriminatorKey: 'kind' })

const EmailSchema = new Schema({
    email: {
        address: String,
        verified: Boolean
    }
})

const schema = new Schema({
    name: {
      first: String,
      last: String,
    },
    ids: [identity],
})
  
schema.path('ids').discriminator('emailIdentity', EmailSchema)
const Testr = mongoose.model('testr', schema)
  1. Insert the following data:
 await Testr.create({
        name: { first: 'Frank', last: 'Instein'},
        ids: [{
            email: {address: 'test@example.com', verified: false},
            kind: 'emailIdentity'
        }],
    });

    await Testr.create({
        name: { first: 'Jane', last: 'Doe'},
        ids: [{
            email: {address: 'test1@example.com', verified: false},
            kind: 'emailIdentity'
        }],
    });
  1. Run the following code:
    const t = await Testr.find({'ids.email.address': 'test@example.com'})
    const collection = Testr.$__collection;
    const t1 = await collection.find({'ids.email.address': 'test@example.com'}).toArray()

The expectation is that t and t1 have the same value.

Expected Behavior

Querying on an embedded discriminator sub-model should filter appropriately.

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Jan 18, 2023
@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');

const { Schema } = mongoose;

const identity = new Schema({ primary: { type: Boolean, default: false } }, { discriminatorKey: 'kind' })

const EmailSchema = new Schema({
    email: {
        address: String,
        verified: Boolean
    }
})

const schema = new Schema({
    name: {
      first: String,
      last: String,
    },
    ids: [identity],
})
  
schema.path('ids').discriminator('emailIdentity', EmailSchema)
const Testr = mongoose.model('testr', schema)


async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  await mongoose.connection.dropDatabase();

  await Testr.create({
    name: { first: 'Frank', last: 'Instein'},
    ids: [{
        email: {address: 'test@example.com', verified: false},
        kind: 'emailIdentity'
    }],
});

await Testr.create({
    name: { first: 'Jane', last: 'Doe'},
    ids: [{
        email: {address: 'test1@example.com', verified: false},
        kind: 'emailIdentity'
    }],
});

const t = await Testr.find({'ids.email.address': 'test@example.com'})
const collection = Testr.$__collection;
const t1 = await collection.find({'ids.email.address': 'test@example.com'}).toArray()
console.log('what is t', t, 'what is t1', t1);
}

run();

@vkarpov15 vkarpov15 added this to the 6.8.6 milestone Jan 23, 2023
@vkarpov15
Copy link
Collaborator

This is an issue with strictQuery, run the following when your server starts to get the 5.x behavior:

mongoose.set('strictQuery', false);

We're changing strictQuery to false by default in 7.0 with #11861. Sorry for the trouble!

@vkarpov15 vkarpov15 removed this from the 6.9.1 milestone Feb 4, 2023
@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 Feb 4, 2023
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

3 participants