{"action":"created","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/5785","repository_url":"https://api.github.com/repos/Automattic/mongoose","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/5785/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/5785/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/5785/events","html_url":"https://github.com/Automattic/mongoose/issues/5785","id":270920919,"number":5785,"title":"Schema.Type.Mixed fields do not update when type coercion is in question","user":{"login":"zlatinejc","id":10756198,"avatar_url":"https://avatars3.githubusercontent.com/u/10756198?v=4","gravatar_id":"","url":"https://api.github.com/users/zlatinejc","html_url":"https://github.com/zlatinejc","followers_url":"https://api.github.com/users/zlatinejc/followers","following_url":"https://api.github.com/users/zlatinejc/following{/other_user}","gists_url":"https://api.github.com/users/zlatinejc/gists{/gist_id}","starred_url":"https://api.github.com/users/zlatinejc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zlatinejc/subscriptions","organizations_url":"https://api.github.com/users/zlatinejc/orgs","repos_url":"https://api.github.com/users/zlatinejc/repos","events_url":"https://api.github.com/users/zlatinejc/events{/privacy}","received_events_url":"https://api.github.com/users/zlatinejc/received_events","type":"User","site_admin":false},"labels":[{"id":335758689,"url":"https://api.github.com/repos/Automattic/mongoose/labels/needs%20clarification","name":"needs clarification","color":"EB219B","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2017-11-03T09:39:54Z","updated_at":"2017-11-07T08:31:28Z","closed_at":null,"author_association":"NONE","body":"<!-- *Before creating an issue please make sure you are using the latest version of mongoose -->\r\nVersion 4.13.0\r\n\r\n**Do you want to request a *feature* or report a *bug*?**\r\nBUG\r\n\r\n**What is the current behavior?**\r\nMongoose fails to save correct (new values of different type) when type coercion is executed on a updating field that is of type Schema.Types.Mixed.\r\n\r\n**If the current behavior is a bug, please provide the steps to reproduce.**\r\n<!-- If you can, provide a standalone script / gist to reproduce your issue -->\r\n1. Create a simple model with an attribute of Schema.Type.Mixed\r\n2. Insert a record with a type of an empty string \"\" to an attribute that is of type from the 1. step\r\n3. Update a record from step 2. on a field which is of type from step 1. with a value 0 that is of type Integer\r\n4. The field of the updating record is not updated with the new value\r\n5. You can reproduce this error updating the same field with other values of different types eg: false, undefined ... basicly with any value that touches the infamous Type Coercion field of JavaScript.\r\n  \r\n**What is the expected behavior?**\r\nJust save the value I pass to a specific field that is of type Schema.Types.Mixed -> it is the reason why I use this type in the first place :)\r\n\r\n**Please mention your node.js, mongoose and MongoDB version.**\r\nNode 4.8.4, MongoDB 3.4.6"},"comment":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/comments/342410591","html_url":"https://github.com/Automattic/mongoose/issues/5785#issuecomment-342410591","issue_url":"https://api.github.com/repos/Automattic/mongoose/issues/5785","id":342410591,"user":{"login":"zlatinejc","id":10756198,"avatar_url":"https://avatars3.githubusercontent.com/u/10756198?v=4","gravatar_id":"","url":"https://api.github.com/users/zlatinejc","html_url":"https://github.com/zlatinejc","followers_url":"https://api.github.com/users/zlatinejc/followers","following_url":"https://api.github.com/users/zlatinejc/following{/other_user}","gists_url":"https://api.github.com/users/zlatinejc/gists{/gist_id}","starred_url":"https://api.github.com/users/zlatinejc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zlatinejc/subscriptions","organizations_url":"https://api.github.com/users/zlatinejc/orgs","repos_url":"https://api.github.com/users/zlatinejc/repos","events_url":"https://api.github.com/users/zlatinejc/events{/privacy}","received_events_url":"https://api.github.com/users/zlatinejc/received_events","type":"User","site_admin":false},"created_at":"2017-11-07T08:31:28Z","updated_at":"2017-11-07T08:31:28Z","author_association":"NONE","body":"OK, here we go:\r\n\r\n1. Creating a simple model\r\n```javascript\r\nconst mongoose = require('mongoose'),\r\nSampleSchema = new Schema({\r\n  value: {\r\n    type: Schema.Types.Mixed,\r\n    required: 'missing_value'\r\n});\r\nmongoose.model('SampleSchema ', SampleSchema );\r\n```\r\n\r\n2. Inserting initial record\r\n```javascript\r\nconst initialRecord = new SampleSchema({ value : '' });\r\ninitialRecord.save(); // save OK, value has been set to ''\r\n```\r\n\r\n3. Updating existing record with new value of 0 (type Number)\r\n```javascript\r\ninitialRecord.value = 0;\r\ninitialRecord.save(); // update OK, value has not been set to ''\r\n```\r\n4. The record's value is not updated to value of 0.\r\n```javascript\r\nconsole.log(initialRecord.value); // the value is still an empty string ''\r\n```\r\n\r\n5. The workaround I currently use, although I would prefer if would not to, is:\r\n```\r\nSampleSchema.pre('validate', (next) => {\r\n  this.markModified('value');\r\n  next();\r\n});\r\n\r\nPS: handling node's asynchrony is intentionally left out for the purpose of simplicity."}}
{"actor":{"display_login":"zlatinejc"}}