Skip to content

Commit 157823c

Browse files
committed
fix(document): avoid triggering setter when initializing Model.prototype.collection to allow defining collection as a schema path name
Fix #13956
1 parent a1d2bf9 commit 157823c

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

lib/model.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4759,8 +4759,6 @@ Model.compile = function compile(name, schema, collectionName, connection, base)
47594759

47604760
schema._preCompile();
47614761

4762-
model.prototype.$__setSchema(schema);
4763-
47644762
const _userProvidedOptions = schema._userProvidedOptions || {};
47654763

47664764
const collectionOptions = {
@@ -4773,13 +4771,16 @@ Model.compile = function compile(name, schema, collectionName, connection, base)
47734771
collectionOptions.autoCreate = schema.options.autoCreate;
47744772
}
47754773

4776-
model.prototype.collection = connection.collection(
4774+
const collection = connection.collection(
47774775
collectionName,
47784776
collectionOptions
47794777
);
47804778

4781-
model.prototype.$collection = model.prototype.collection;
4782-
model.prototype[modelCollectionSymbol] = model.prototype.collection;
4779+
model.prototype.collection = collection;
4780+
model.prototype.$collection = collection;
4781+
model.prototype[modelCollectionSymbol] = collection;
4782+
4783+
model.prototype.$__setSchema(schema);
47834784

47844785
// apply methods and statics
47854786
applyMethods(model, schema);
@@ -4788,8 +4789,8 @@ Model.compile = function compile(name, schema, collectionName, connection, base)
47884789
applyStaticHooks(model, schema.s.hooks, schema.statics);
47894790

47904791
model.schema = model.prototype.$__schema;
4791-
model.collection = model.prototype.collection;
4792-
model.$__collection = model.collection;
4792+
model.collection = collection;
4793+
model.$__collection = collection;
47934794

47944795
// Create custom query constructor
47954796
model.Query = function() {

test/document.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12600,6 +12600,18 @@ describe('document', function() {
1260012600
['I am NumberTyped', 'I am StringTyped']
1260112601
);
1260212602
});
12603+
12604+
it('can use `collection` as schema name (gh-13956)', async function() {
12605+
const schema = new mongoose.Schema({ name: String, collection: String });
12606+
const Test = db.model('Test', schema);
12607+
12608+
const doc = await Test.create({ name: 'foo', collection: 'bar' });
12609+
assert.strictEqual(doc.collection, 'bar');
12610+
doc.collection = 'baz';
12611+
await doc.save();
12612+
const { collection } = await Test.findById(doc);
12613+
assert.strictEqual(collection, 'baz');
12614+
});
1260312615
});
1260412616

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

0 commit comments

Comments
 (0)