-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Labels
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
Milestone
Comments
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
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); |
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
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(); |
Hi there, I've managed to replicate the issue. Please find enclosed a script to replicate the issue:
|
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
As a workaround, use the following syntax: const userNameResult = await NamesModel.findOne({})
.select({ '__v': 0, '__t': 0, 'test': 0 })
.lean(); |
vkarpov15
added a commit
that referenced
this issue
Oct 3, 2023
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
vkarpov15
added a commit
that referenced
this issue
Dec 5, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prerequisites
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
mongoose.select(['-__t'])
__t
is not removed from results.Expected Behavior
__t
discriminator field should be removed via select method.The text was updated successfully, but these errors were encountered: