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

Can only omit discriminator field if _id is also selected #13760

Closed
2 tasks done
cg-julian-taylor opened this issue Aug 21, 2023 · 4 comments · Fixed by #13929
Closed
2 tasks done

Can only omit discriminator field if _id is also selected #13760

cg-julian-taylor opened this issue Aug 21, 2023 · 4 comments · Fixed by #13929
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@cg-julian-taylor
Copy link

Prerequisites

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

Mongoose version

7.4.0

Node.js version

18.6.1

MongoDB server version

4.1.3

Typescript version (if applicable)

No response

Description

When using the mongoose.select() method I can only select to omit discriminator fields if _id is also selected for ommision.

For example:
mongoose.select('-_id -__t') removes the __t field.
mongoose.select('-__t') does not.

This has been attempted using both array and string notation.

this may be related to #13679

Steps to Reproduce

  1. call mongoose.select(['-__t'])
  2. console.log the result to see that __t is not removed from results.

Expected Behavior

__t discriminator field should be removed via select method.

@vkarpov15 vkarpov15 added this to the 7.4.5 milestone Aug 22, 2023
@vkarpov15 vkarpov15 added the needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue label Aug 22, 2023
@cg-julian-taylor
Copy link
Author

cg-julian-taylor commented Aug 22, 2023

I don't know if this helps as its not a script as such, but here is an example of what type of code I'm trying to run:

const userSchema = new mongoose.Schema({
  firstName: String
  lastName: String
});

const User = mongoose.model('User', userSchema);

const joeBloggsUser = new User({firstName: 'Joe', lastName: 'Bloggs'});

const users = await User.find().select(['-__t']);

console.log(users[0].__t === undefined);

@vkarpov15 vkarpov15 modified the milestones: 7.4.5, 7.5.1 Aug 25, 2023
@IslandRhythms IslandRhythms added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Aug 29, 2023
@IslandRhythms
Copy link
Collaborator

Modify the script to demonstrate your issue.

const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
  firstName: String,
  lastName: String
});

const User = mongoose.model('User', userSchema);

const accountSchema = User.discriminator('account', new mongoose.Schema({ age: Number }, { discriminatorKey: 'kind' }));

async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  await mongoose.connection.dropDatabase();
  const joeBloggsUser = await User.create({firstName: 'Joe', lastName: 'Bloggs'});

  const users = await User.find().select(['-__t']);

  console.log(users[0].__t === undefined, users[0].__t);
  const test = new accountSchema({ age: 2 });
  console.log(test.__t)
  await test.save();
  const res = await accountSchema.find().select(['-__t']);
  console.log(res[0].__t)
}

run();

@vkarpov15 vkarpov15 removed this from the 7.5.1 milestone Sep 1, 2023
@cg-julian-taylor
Copy link
Author

cg-julian-taylor commented Sep 7, 2023

Hi there, I've managed to replicate the issue. Please find enclosed a script to replicate the issue:

const mongoose = require('mongoose');

async function run(){
  const baseConfig = {
    discriminationKey: '_type',
    collection: 'users'
  };

  const NamesSchema = new mongoose.Schema({
    firstName:   { type: String },
    lastName:  { type: String },
    test: {type: String}
  });

  const User = mongoose.model('User', new mongoose.Schema({}, baseConfig));

  const NamesModel = User.discriminator('names', NamesSchema);

  await mongoose.connect('mongodb://localhost:27017');

  await mongoose.connection.dropDatabase();

  const joeBloggsUser = await NamesModel.create({firstName: 'Joe', lastName: 'Bloggs', test: '123'});

  const userNameResult = await NamesModel.findOne({})
    .select(['-__v', '-__t', '-test'])
    .lean();

  console.log(userNameResult.__t === undefined, userNameResult.__t, userNameResult.__v, userNameResult.test);
}

run();

@vkarpov15 vkarpov15 added this to the 7.5.4 milestone Sep 18, 2023
@vkarpov15 vkarpov15 added has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue and removed can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. labels Sep 18, 2023
@vkarpov15
Copy link
Collaborator

As a workaround, use the following syntax:

  const userNameResult = await NamesModel.findOne({})
    .select({ '__v': 0, '__t': 0, 'test': 0 })
    .lean();

@vkarpov15 vkarpov15 added 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 Oct 3, 2023
vkarpov15 added a commit that referenced this issue Oct 4, 2023
fix(query): allow deselecting discriminator key using `-` syntax
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants