'set' can no longer be used in a schema? #1939

Closed
nathanpeck opened this Issue Feb 25, 2014 · 5 comments

Comments

Projects
None yet
4 participants

After upgrading to Mongoose 3.8.8 from 3.8.5 we were surprised to find that our server began crashing with the following message:

`set` may not be used as a schema pathname
Error: `set` may not be used as a schema pathname
    at Schema.path (/srv/api/node_modules/mongoose/lib/schema.js:282:11)
    at Schema.add (/srv/api/node_modules/mongoose/lib/schema.js:217:12)
    at new Schema (/srv/api/node_modules/mongoose/lib/schema.js:73:10)
    at Object.<anonymous> (/srv/api/src/models/template.js:6:14)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)

To be clear our schema defines a property called "set" (which is the ID of the set that a particular item belongs to). This has been used in our MongoDB documents for a long time. There was nothing wrong with having having a document property called "set" in 3.8.5, and it is a valid MongoDB document property.

I'm not sure what changed within Mongoose that is causing Mongoose to reject it now (perhaps it thinks it will collide with the "$set" MongoDB command?) I consider this to a bug, and it will prevent me from upgrading to the latest Mongoose it is fixed. I'll check into the Mongoose code to see if I find anything obviously wrong.

Edit: So I checked into the Mongoose code and it looks like Mongoose is rejecting the "set" property because it would override one of Mongoose' methods. I understand the reason for this, but I consider this to be indicative of a fundamentally flawed object architecture in Mongoose.

Document level properties should not be living at the same level as Mongoose's methods, and if there is a collision between the two I consider that to be an issue with the Mongoose object model. Users of Mongoose should never be forced to avoid certain property names which are valid MongoDB property names simply because Mongoose wants to use them for methods.

nolanamy commented Jul 1, 2016

+1

@vkarpov15 vkarpov15 added this to the 5.0 milestone Jul 1, 2016

Collaborator

vkarpov15 commented Jul 1, 2016

Would love to change this but it will be horribly backwards breaking. .set() is a very fundamental method for how mongoose docs work, so right now we can't support schema paths named set(). I agree it's flawed, will fix in a future release, but for now this is something that you need to work around.

Hi @vkarpov15
I've received the same error, while integrated mongoose with existing DB.

Any plans when v5.0 going to be released? It's a bit frustrating that I can't use valid name for field.

There is an option typeKey, introduced for the same reason. Isn't there a way to provide something similar for set/get methods for options?

OK, providing option is impossible, I guess. Can you advice anything?

Collaborator

vkarpov15 commented Jul 28, 2017

We don't have any concrete timeline for 5.0 just yet. Will look into adding an option to support this for next minor release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment