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 does not clean up modified paths of nested properties and array elements within the nested property after save #13609

Closed
2 tasks done
upirr opened this issue Jul 13, 2023 · 1 comment · Fixed by #13644
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@upirr
Copy link

upirr commented Jul 13, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

7.3.3

Node.js version

16.18.1

MongoDB server version

4.4

Typescript version (if applicable)

5.1

Description

Mongoose does not clean up modified paths of nested properties and array elements within the nested property after save. It's similar to #13582, only in this case this is nested property with schema that is misbehaving.

Steps to Reproduce

import mongoose from 'mongoose';
import { expect } from 'chai';
​
const TwoElementSchema = new mongoose.Schema({
	elementName: String,
});
​
const TwoNestedSchema = new mongoose.Schema({
	nestedName: String,
	elements: [TwoElementSchema],
});
​
const TwoDocDefaultSchema = new mongoose.Schema({
	docName: String,
	nested: { type: TwoNestedSchema, default: {} },
});
​
const TwoDocNoDefaultSchema = new mongoose.Schema({
	docName: String,
	nested: { type: TwoNestedSchema },
});
​
const TwoDocDefault = mongoose.model('TwoDocDefault', TwoDocDefaultSchema);
const TwoDocNoDefault = mongoose.model('TwoDocNoDefault', TwoDocNoDefaultSchema);
​
describe('mongoose after doc save', () => {
	before(async () => {
		await mongoose.connect('mongodb://localhost:27017/j2-autotests');
	});
​
	describe('modified paths', () => {
​
		it('should all be cleared for nested element with default value', async () => {
			let doc = new TwoDocDefault({ docName: 'MyDocName' });
			doc.nested.nestedName = 'qdwqwd';
			doc.nested.elements.push({ elementName: 'ElementName1' });
			expect(allPaths(doc)).to.not.deep.equal([]);
			doc = await doc.save();
			expect(allPaths(doc)).to.deep.equal([]);
		});
​
		it('should all be cleared for nested element without default value', async () => {
			let doc = new TwoDocNoDefault({ docName: 'MyDocName', nested: {} });
			doc.nested!.nestedName = 'qdwqwd';
			doc.nested!.elements.push({ elementName: 'ElementName1' });
			expect(allPaths(doc)).to.not.deep.equal([]);
			doc = await doc.save();
			expect(allPaths(doc)).to.deep.equal([]);
		});
	});
});
​
const allPaths = (doc: mongoose.Document) => {
	return [doc, ...doc.$getAllSubdocs()].reduce((ary, d: any) => {
		return ary.concat(d.modifiedPaths().map((p: string) => p));
	}, [] as string[]);
};

Expected Behavior

Nested property modifiedPaths() call properly returns []. Nested property subproperty that is defined as documents array call to modifiedPaths() also properly returns [] as a result.

@vkarpov15 vkarpov15 added this to the 7.4.1 milestone Jul 13, 2023
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Jul 13, 2023
@IslandRhythms 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 Jul 19, 2023
@IslandRhythms
Copy link
Collaborator

const mongoose = require('mongoose');

const TwoElementSchema = new mongoose.Schema({
	elementName: String,
});

const TwoNestedSchema = new mongoose.Schema({
	nestedName: String,
	elements: [TwoElementSchema],
});

const TwoDocDefaultSchema = new mongoose.Schema({
	docName: String,
	nested: { type: TwoNestedSchema, default: {} },
});

const TwoDocNoDefaultSchema = new mongoose.Schema({
	docName: String,
	nested: { type: TwoNestedSchema },
});

const TwoDocDefault = mongoose.model('TwoDocDefault', TwoDocDefaultSchema);
const TwoDocNoDefault = mongoose.model('TwoDocNoDefault', TwoDocNoDefaultSchema);

async function run() {
  await mongoose.connect('mongodb://localhost:27017');
  await mongoose.connection.dropDatabase();

  let doc = new TwoDocDefault({ docName: 'MyDocName' });
  doc.nested.nestedName = 'qdwqwd';
	doc.nested.elements.push({ elementName: 'ElementName1' });
	console.log(allPaths(doc).length == [].length); // should be false
	doc = await doc.save();
	console.log(allPaths(doc) == [].length); // should be true
  doc = new TwoDocNoDefault({ docName: 'MyDocName', nested: {} });
  doc.nested.nestedName = 'qdwqwd';
  doc.nested.elements.push({ elementName: 'ElementName1' });
  console.log(allPaths(doc) == [].length); // false
  doc = await doc.save();
  console.log(allPaths(doc) == [].length); // should be true

}

run();


const allPaths = (doc) => {
	return [doc, ...doc.$getAllSubdocs()].reduce((ary, d) => {
		return ary.concat(d.modifiedPaths().map((p) => p));
	}, []);
};

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