Mongoose: Allow `schema` as a schema path name

Created on 15 Apr 2020  路  13Comments  路  Source: Automattic/mongoose

Do you want to request a feature or report a bug?
Bug

What is the current behavior?
When i set schema name schema,
image

mongoose throw err
image

mongoose/lib/schema.js line 528

image

If the current behavior is a bug, please provide the steps to reproduce.

const schema = new Schema({ 'schema': { type: String, default: '{}'}, });
What is the expected behavior?
I can set name schema in Schema
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Mongoose version 5.4.7
5.9.9 alse have this problem
https://github.com/Automattic/mongoose/blob/master/lib/schema.js#L645
Nodejs version 12

enhancement good first issue

Most helpful comment

In that case I would rename the schema field into a different field name.
I've always found thesaurus quite helpful in finding synonyms.

All 13 comments

@AbdelrahmanHafez Thanks for you answer, I am working on service migration. If the schema field already exists in the original database, how should I deal with this situation?

In that case I would rename the schema field into a different field name.
I've always found thesaurus quite helpful in finding synonyms.

@AbdelrahmanHafez Maybe another mongo client is a better solution.

I can't make that choice for you, depending on the effort of renaming the field, and the features mongoose provides. See what outweighs what, and make your decision.

Perhaps @vkarpov15 has a better idea.

@AbdelrahmanHafez thanks!

Some path names unfortunately do conflict with existing methods and properties. However, there are ways for us to work around this. I'll keep this open for a future release.

@vkarpov15 Can I take up this issue to work on?

@JatinRanka sure :+1:

Hi, @vkarpov15 I am a newbie to this project and would like to contribute it; do you think you can put me in the right direction, please?

@Edu93Jer sure. The general idea is that several methods in lib/document.js relies on this.schema to get the document's schema, which is why schema isn't allowed as a path name. We would need to refactor lib/document.js to ideally rely on a symbol instead of 'schema', but we could get the same result by changing the name of the 'schema' property on documents to '$__schema' or something similar.

@vkarpov15 when you say "changing the name of the 'schema' property on _documents_ to '$__schema' " you mean in all the documents or just where the property is added, because I just did this, and the issue works well. (Line 592)
Capture12

That's one step. But the reason why reserved.schema is set is that Mongoose documents assume that this.schema is the schema belonging to the model that this document is an instance of. For example, here: https://github.com/Automattic/mongoose/blob/645a45bc6b87b1581149db49532a9366e434db9b/lib/document.js#L104 .

Any change to reserved.schema also means we need to change all the places in lib/document.js where we use this.schema.

Was this page helpful?
0 / 5 - 0 ratings