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

Avoid storing a separate entry in schema subpaths for every element in an array #13953

Merged
merged 6 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 10 additions & 7 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,7 @@ Schema.prototype.path = function(path, obj) {

// subpaths?
return /\.\d+\.?.*$/.test(path)
? getPositionalPath(this, path)
? getPositionalPath(this, path, cleanPath)
: undefined;
}

Expand Down Expand Up @@ -1634,7 +1634,7 @@ Schema.prototype.pathType = function(path) {
}

if (/\.\d+\.|\.\d+$/.test(path)) {
return getPositionalPathType(this, path);
return getPositionalPathType(this, path, cleanPath);
}
return 'adhocOrUndefined';
};
Expand Down Expand Up @@ -1678,7 +1678,7 @@ Schema.prototype.setupTimestamp = function(timestamps) {
* @api private
*/

function getPositionalPathType(self, path) {
function getPositionalPathType(self, path, cleanPath) {
const subpaths = path.split(/\.(\d+)\.|\.(\d+)$/).filter(Boolean);
if (subpaths.length < 2) {
return self.paths.hasOwnProperty(subpaths[0]) ?
Expand Down Expand Up @@ -1729,7 +1729,7 @@ function getPositionalPathType(self, path) {
val = val.schema.path(subpath);
}

self.subpaths[path] = val;
self.subpaths[cleanPath] = val;
if (val) {
return 'real';
}
Expand All @@ -1744,9 +1744,9 @@ function getPositionalPathType(self, path) {
* ignore
*/

function getPositionalPath(self, path) {
getPositionalPathType(self, path);
return self.subpaths[path];
function getPositionalPath(self, path, cleanPath) {
getPositionalPathType(self, path, cleanPath);
return self.subpaths[cleanPath];
}

/**
Expand Down Expand Up @@ -2638,6 +2638,9 @@ Schema.prototype._getSchema = function(path) {
// Re: gh-5628, because `schema.path()` doesn't take $ into account.
parts[i] = '0';
}
if (/^\d+$/.test(parts[i])) {
vkarpov15 marked this conversation as resolved.
Show resolved Hide resolved
parts[i] = '$';
}
}
return search(parts, _this);
};
Expand Down
30 changes: 30 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12520,6 +12520,36 @@ describe('document', function() {
await doc.save();
assert.strictEqual(attachmentSchemaPreValidateCalls, 1);
});

it('avoids creating separate subpaths entry for every element in array (gh-13874)', async function() {
const tradeSchema = new mongoose.Schema({ tradeId: Number, content: String });

const testSchema = new mongoose.Schema(
{
userId: Number,
tradeMap: {
type: Map,
of: tradeSchema
}
}
);

const TestModel = db.model('Test', testSchema);


const userId = 100;
const user = await TestModel.create({ userId, tradeMap: new Map() });

// add subDoc
for (let id = 1; id <= 10; id++) {
const trade = { tradeId: id, content: 'test' };
user.tradeMap.set(trade.tradeId.toString(), trade);
}
await user.save();
await TestModel.deleteOne({ userId });

assert.equal(Object.keys(TestModel.schema.subpaths).length, 3);
});
});

describe('Check if instance function that is supplied in schema option is availabe', function() {
Expand Down