Skip to content

Commit

Permalink
Merge pull request #13953 from Automattic/vkarpov15/gh-13874
Browse files Browse the repository at this point in the history
Avoid storing a separate entry in schema `subpaths` for every element in an array
  • Loading branch information
vkarpov15 committed Oct 10, 2023
2 parents 660ea80 + 82f2ca8 commit 642abd1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
19 changes: 12 additions & 7 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const isPOJO = utils.isPOJO;

let id = 0;

const numberRE = /^\d+$/;

/**
* Schema constructor.
*
Expand Down Expand Up @@ -1004,7 +1006,7 @@ Schema.prototype.path = function(path, obj) {

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

Expand Down Expand Up @@ -1621,7 +1623,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 @@ -1665,7 +1667,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 @@ -1716,7 +1718,7 @@ function getPositionalPathType(self, path) {
val = val.schema.path(subpath);
}

self.subpaths[path] = val;
self.subpaths[cleanPath] = val;
if (val) {
return 'real';
}
Expand All @@ -1731,9 +1733,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 @@ -2625,6 +2627,9 @@ Schema.prototype._getSchema = function(path) {
// Re: gh-5628, because `schema.path()` doesn't take $ into account.
parts[i] = '0';
}
if (numberRE.test(parts[i])) {
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 @@ -12521,6 +12521,36 @@ describe('document', function() {
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);
});

it('handles embedded discriminators defined using Schema.prototype.discriminator (gh-13898)', async function() {
const baseNestedDiscriminated = new Schema({
type: { type: Number, required: true }
Expand Down

0 comments on commit 642abd1

Please sign in to comment.