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

Discriminators causing everything to be "unselected" when using "select" #5859

Closed
wlingke opened this issue Nov 29, 2017 · 5 comments
Closed
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@wlingke
Copy link
Contributor

wlingke commented Nov 29, 2017

I am attempting to use .select in a query with a discriminator model. The example below uses the projection to pull a textScore (meant for use with a $text query) but the bug is not specifically related to this use case.

When making a find call, the returned objects do NOT have trackField or eventField and they are selected out automatically without me specifying it.

See Repro Script:

const mongoose = require('./');
mongoose.set('debug', true);

const Schema = mongoose.Schema;

mongoose.connect('mongodb://127.0.0.1/test');
const db = mongoose.connection;

db.once('open', async () => {
  try {
    const eventSchema = new Schema({
      eventField: String
    }, { id: false });

    const Event = mongoose.model('Event', eventSchema);

    const trackSchema = new Schema({
      trackField: String,
    });

    const Track = Event.discriminator('Track', trackSchema);

    const trackedItem = new Track({
      trackField: 'trackField',
      eventField: 'eventField',
    });
    await trackedItem.save();

    const events = await Event.find({}).select({ score: { $meta: 'textScore' } }).exec();
    const tracks = await Track.find({}).select({ score: { $meta: 'textScore' } }).exec();
    console.log(events); // no trackField or eventField
    console.log(tracks); // no trackField or eventField

    db.close();
  } catch (e) {
    console.log(e);
  }
});

When you attach the debugger, it shows this query:
events.find({}, { fields: { score: { '$meta': 'textScore' }, __t: 1 } })

I imagine that the __t: 1 is causing this.

If I run await Event.find({}).exec(), the mongoose debugger shows
events.find({}, { fields: {} }) which is why normally everything is selected.

The problem that this causes is that you can't run a $text query sorting by textScore on anything that has a discriminator without specifically projecting every single field in the schema.

Is there any way to override the __t: 1? I tried setting {__t: null} but that didn't work. Or is there a mongodb projection operator that I'm not aware that might solve this problem?

@vkarpov15 vkarpov15 added this to the 4.13.7 milestone Dec 7, 2017
@vkarpov15 vkarpov15 added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Dec 7, 2017
@vkarpov15
Copy link
Collaborator

Thanks for the detailed repro, will fix. The __t: 1 getting added is a bug, mongoose should only add it if the user-provided projection is inclusive. { score: { $meta: 'textScore' } } is not an inclusive projection, so I imagine we're checking for truthiness somewhere as opposed to looking for $meta and $slice.

@wlingke
Copy link
Contributor Author

wlingke commented Dec 8, 2017

awesome thanks so much

@dimaip
Copy link

dimaip commented Feb 22, 2019

The __t: 1 getting added is a bug

Is it a different bug then? I have the same problem, I have a BaseUser model and User model discriminated on top of it.
When I do query like this BaseUser.findById(req.user._id, '+email') I'm only getting the email field in response. This happens because __t gets included in the query, and so it yields exclude = false in applyPaths.

This problem happens on mongoos 4.13.18, on 5.x it seems to work correctly.

@vkarpov15
Copy link
Collaborator

@dimaip looks like this bug was fixed in 5.x then. Is there something that prevents you from upgrading?

@dimaip
Copy link

dimaip commented Feb 28, 2019

@vkarpov15 I already did, the upgrade process went smooth.

@Automattic Automattic locked as resolved and limited conversation to collaborators Mar 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

3 participants