-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Closed
Labels
confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.We've confirmed this is a bug in Mongoose and will fix it.
Milestone
Description
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Mongoose version
8.5.4
Node.js version
20.17.0
MongoDB server version
7.0.11
Typescript version (if applicable)
No response
Description
In versions 8.5.0 and prior, a call to Document.prototype.toObject() would run getters defined in schemas for subdocuments of arbitrary depth. In 8.5.1 and subsequent versions, toObject() will not run getters for subdocuments nested more than 1 level deep.
Getters should be run on all subdocument fields.
Steps to Reproduce
The following code will run the getters on field1 and field2 in mongoose versions 8.5.0 and prior, and will not run the getter for field2 in 8.5.1 and subsequent versions:
const embed2Schema = new mongoose.Schema({
field2: {
type: Date,
get: (v) => v.toISOString().slice(0, 10),
},
}, {_id: false, toObject: {getters: true}});
const embed1Schema = new mongoose.Schema({
subdoc: embed2Schema,
field1: {
type: Date,
get: (v) => v.toISOString().slice(0, 10),
},
}, {_id: false, toObject: {getters: true}});
const testSchema = new mongoose.Schema({
testArray: [embed1Schema],
});
const TestModel = mongoose.model('Test', testSchema);
const d = TestModel.hydrate({
testArray: [{field1: '2022-02-01', subdoc: {field2: '2022-02-01'}}],
});
const o = d.toObject();
const {field1} = o.testArray[0];
const {field2} = o.testArray[0].subdoc;
if (field1 !== field2) throw new Error(`field1: ${field1} field2: ${field2}`);
The above test will succeed on 8.5.0 and previous, and fail on 8.5.1 and subsequent versions.
Expected Behavior
All defined getters should be run for nested subdocuments.
Metadata
Metadata
Assignees
Labels
confirmed-bugWe've confirmed this is a bug in Mongoose and will fix it.We've confirmed this is a bug in Mongoose and will fix it.