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

Modified paths does not reset properly on subdocuments after document save #13582

Closed
2 tasks done
upirr opened this issue Jul 5, 2023 · 0 comments · Fixed by #13589
Closed
2 tasks done

Modified paths does not reset properly on subdocuments after document save #13582

upirr opened this issue Jul 5, 2023 · 0 comments · Fixed by #13589
Milestone

Comments

@upirr
Copy link

upirr commented Jul 5, 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

6.9.0, 7.3.1

Node.js version

18.16.0

MongoDB server version

4.4

Typescript version (if applicable)

5.1

Description

Currently when saving a document with multiple subdocuments document.modifiedPaths({ includeChildren: true }) produces [] but every subdocument except for the first one still retain their modifiedPaths() after save() has been already executed. I assume that judging by the fact that first subdocument does gets cleaned up this is a non-expected behavior.

Steps to Reproduce

import mongoose from 'mongoose';
import { expect } from 'chai';

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

const MyDocSchema = new mongoose.Schema({
	docName: String,
	elements: [ElementSchema],
});

const MyDoc = mongoose.model('MyDoc', MyDocSchema);

describe('mongoose after doc save', () => {
	before(async () => {
		await mongoose.connect('mongodb://localhost:27017/autotest');
	});

	describe('modified paths', () => {

		it('should all be cleared after save with multiple elements (non-cloned)', async () => {
			let doc = new MyDoc({ docName: 'MyDocName' });
			doc.elements.push({ elementName: 'ElementName1' });
			doc.elements.push({ elementName: 'ElementName2' });
			doc = await doc.save();
			console.log(doc.elements[1].modifiedPaths())
			expect(allPaths(doc)).to.deep.equal([]);
		});
	});
});

const allPaths = (doc: mongoose.Document) => {
	return [doc, ...doc.$getAllSubdocs()].reduce((ary, d: any) => {
		console.log(d, d.modifiedPaths());
		return ary.concat(d.modifiedPaths().map((p: string) => p));
	}, [] as string[]);
};

Expected Behavior

Both document and subdocuments on any depth should produce [] as a result of modifiedPaths() invocation after document has been saved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants