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

Mongoose schema default mutating original object nested properties provided to model constructor #12102

Closed
2 tasks done
eriy opened this issue Jul 13, 2022 · 0 comments
Closed
2 tasks done
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@eriy
Copy link

eriy commented Jul 13, 2022

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.

@IslandRhythms IslandRhythms added the confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. label Jul 13, 2022
lpizzinidev added a commit to lpizzinidev/mongoose that referenced this issue Jul 16, 2022
@vkarpov15 vkarpov15 added this to the 6.4.6 milestone Jul 17, 2022
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