-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
The default value for subdocument fields is not applied when nested projection is used (find/findOne methods) #13720
Labels
confirmed-bug
We've confirmed this is a bug in Mongoose and will fix it.
Milestone
Comments
andrei-mashko
changed the title
The default value for subdocument fields is ignored when nested projection is used (find/findOne methods)
The default value for subdocument fields is not applied when nested projection is used (find/findOne methods)
Aug 11, 2023
Looks like the problem is only relevant to the case when the nested projection is a string. const nestedProjectionDoc = await User.findOne({}, { name: 1, sub: { propertyA: 1 } });
console.log(nestedProjectionDoc.sub.propertyA); // A |
vkarpov15
added
the
has repro script
There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
label
Aug 14, 2023
IslandRhythms
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
Aug 16, 2023
const mongoose = require('mongoose');
const subSchema = new mongoose.Schema({
propertyA: { type: String, default: 'A' },
propertyB: { type: String, default: 'B' },
});
const userSchema = new mongoose.Schema({
name: String,
sub: { type: subSchema, default: () => ({}) },
});
const User = mongoose.model('User', userSchema);
const main = async () => {
await mongoose.connect('mongodb://localhost:27017');
await mongoose.connection.dropDatabase();
await User.insertMany([{ name: 'user' }]);
await User.updateMany({}, { $unset: { 'sub.propertyA': '' } });
const leanDoc = await User.findOne({}, {}, { lean: true });
console.log(leanDoc.sub.propertyA); // undefined
const projectionDoc = await User.findOne({}, { name: 1, sub: 1 });
console.log(projectionDoc.sub.propertyA); // 'A'
const nestedProjectionDoc = await User.findOne({}, { name: 1, 'sub.propertyA': 1, 'sub.propertyB': 1 });
console.log(nestedProjectionDoc.sub.propertyA); // 'A' for =< 6.0.0; undefined for > 6.6.0
await mongoose.connection.close();
};
main(); |
Confirmed that this issue is caused by #12427. Working on a fix. |
vkarpov15
added a commit
that referenced
this issue
Aug 22, 2023
…applying subdocument defaults Fix #13720
vkarpov15
added a commit
that referenced
this issue
Aug 22, 2023
fix(document): correctly handle inclusive/exclusive projections when applying subdocument defaults
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prerequisites
Mongoose version
> 6.6.0
Node.js version
v18.16.1
MongoDB server version
5.0
Typescript version (if applicable)
No response
Description
By default mongoose applies default values for subdocument fields which are missing in mongoDB (find/findOne methods):
But when the nested projection is used (e.g. 'sub.propertyA') it doesn't apply it.
Steps to Reproduce
Expected Behavior
No response
The text was updated successfully, but these errors were encountered: