{"action":"closed","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/5657","repository_url":"https://api.github.com/repos/Automattic/mongoose","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/5657/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/5657/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/5657/events","html_url":"https://github.com/Automattic/mongoose/issues/5657","id":259507755,"number":5657,"title":"I want to have a method and property with the same name","user":{"login":"aymericbouzy","id":8168981,"avatar_url":"https://avatars1.githubusercontent.com/u/8168981?v=4","gravatar_id":"","url":"https://api.github.com/users/aymericbouzy","html_url":"https://github.com/aymericbouzy","followers_url":"https://api.github.com/users/aymericbouzy/followers","following_url":"https://api.github.com/users/aymericbouzy/following{/other_user}","gists_url":"https://api.github.com/users/aymericbouzy/gists{/gist_id}","starred_url":"https://api.github.com/users/aymericbouzy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aymericbouzy/subscriptions","organizations_url":"https://api.github.com/users/aymericbouzy/orgs","repos_url":"https://api.github.com/users/aymericbouzy/repos","events_url":"https://api.github.com/users/aymericbouzy/events{/privacy}","received_events_url":"https://api.github.com/users/aymericbouzy/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2017-09-21T14:03:43Z","updated_at":"2017-09-26T01:18:42Z","closed_at":"2017-09-26T01:18:42Z","author_association":"NONE","body":"**Do you want to request a *feature* or report a *bug*?**\r\nfeature\r\n**What is the current behavior?**\r\nerror:  You have a method and a property in your schema both named \"worker\".\r\n**Steps to reproduce.**\r\n```js\r\nconst schema = new Schema({\r\n  worker: {\r\n    workerId: String,\r\n    // some more fields\r\n  },\r\n  // some more fields\r\n})\r\n\r\nschema.methods = {\r\n  worker() {\r\n    return this.model(\"Worker\").findOne({ _id: this.worker.workerId })\r\n  },\r\n}\r\n\r\nconst Contract = mongoose.model(\"Contract\", schema)\r\n```\r\n**What is the expected behavior?**\r\n\r\nThe schema of the database was defined before I arrived. The project had not tests. Changing the schema could break everything.\r\n\r\nI want to be able to get the worker associated to the contract by running `contract.worker()`. \r\n\r\nMy suggestion : \r\n```js\r\nconst WorkerData = new Schema({\r\n  workerId: String, \r\n}, {key: \"worker\"})\r\n\r\nconst schema = new Schema({\r\n  workerData: WorkerData,\r\n  // some more fields\r\n})\r\n```\r\n\r\nThat way, I have access to both `workerData` and `worker` names. I can define my schema the way I want it to look, and let Mongoose do the massaging to fit the existing database.\r\n\r\n\r\n**Please mention your node.js, mongoose and MongoDB version.**\r\nnode: 8.4.0\r\nmongoose: 4.11.11\r\nmongoDB: 2.2.31"}}
{"actor":{"display_login":"vkarpov15"}}