Skip to content

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

@upirr

Description

@upirr

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions