-
-
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
6.4.3
Node.js version
16.15.1
MongoDB server version
4
Description
Nested object are mutated in the original object that is provided as document for model constructor when in model schema defaults are declared and the original objects do not include values for this properties.
Steps to Reproduce
const mongoose = require('mongoose');
const Model = mongoose.model("TestModel", {
topLevelField1: {
nestedField1: String,
nestedField2: {type: String, default: 'defValue'}
},
topLevelField2: {type: String, default: 'defValue'}
});
const originalData = {
topLevelField1: {
nestedField1: 'provided'
}
}
const model = new Model(originalData);
console.log(originalData)
Expected Behavior
Expected result (works in mongoose 5.7.0):
originalData === { topLevelField1: { nestedField1: 'provided' } }
Current result (mongoose 6.4.3):
originalData === { topLevelField1: { nestedField1: 'provided', nestedField2: 'defValue' } }
Expect that original object nested fields are not mutated by mongoose schema defaults.
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.