{"action":"created","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/3169","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/3169/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/3169/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/3169/events","html_url":"https://github.com/Automattic/mongoose/issues/3169","id":95464195,"number":3169,"title":"Under certain circumstances undefined values are recorded in database as null values","user":{"login":"jlchereau","id":2556751,"avatar_url":"https://avatars.githubusercontent.com/u/2556751?v=3","gravatar_id":"","url":"https://api.github.com/users/jlchereau","html_url":"https://github.com/jlchereau","followers_url":"https://api.github.com/users/jlchereau/followers","following_url":"https://api.github.com/users/jlchereau/following{/other_user}","gists_url":"https://api.github.com/users/jlchereau/gists{/gist_id}","starred_url":"https://api.github.com/users/jlchereau/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jlchereau/subscriptions","organizations_url":"https://api.github.com/users/jlchereau/orgs","repos_url":"https://api.github.com/users/jlchereau/repos","events_url":"https://api.github.com/users/jlchereau/events{/privacy}","received_events_url":"https://api.github.com/users/jlchereau/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/Automattic/mongoose/milestones/48","html_url":"https://github.com/Automattic/mongoose/milestones/4.0.8","labels_url":"https://api.github.com/repos/Automattic/mongoose/milestones/48/labels","id":1182593,"number":48,"title":"4.0.8","description":"","creator":{"login":"vkarpov15","id":1620265,"avatar_url":"https://avatars.githubusercontent.com/u/1620265?v=3","gravatar_id":"","url":"https://api.github.com/users/vkarpov15","html_url":"https://github.com/vkarpov15","followers_url":"https://api.github.com/users/vkarpov15/followers","following_url":"https://api.github.com/users/vkarpov15/following{/other_user}","gists_url":"https://api.github.com/users/vkarpov15/gists{/gist_id}","starred_url":"https://api.github.com/users/vkarpov15/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vkarpov15/subscriptions","organizations_url":"https://api.github.com/users/vkarpov15/orgs","repos_url":"https://api.github.com/users/vkarpov15/repos","events_url":"https://api.github.com/users/vkarpov15/events{/privacy}","received_events_url":"https://api.github.com/users/vkarpov15/received_events","type":"User","site_admin":false},"open_issues":23,"closed_issues":5,"state":"open","created_at":"2015-06-26T13:17:52Z","updated_at":"2015-07-17T14:59:18Z","due_on":"2015-07-17T07:00:00Z","closed_at":null},"comments":4,"created_at":"2015-07-16T15:48:23Z","updated_at":"2015-07-17T15:41:58Z","closed_at":null,"body":"To reproduce (with mongoose 3.8.33 and 4.07 + mongodb 3.0.4 and nodejs 0.12.7 on windows 8.1):\r\n\r\n```\r\nvar mongoose = require('mongoose');\r\n\r\nmongoose.connect(\"mongodb://localhost/TestDB\", { server: { auto_reconnect: true, poolSize: 10 } });\r\n\r\nvar dummySchema = new mongoose.Schema({\r\n    alpha             : { type: String },\r\n    more              : {\r\n        beta              : { type: String },\r\n        gamma             : { type: Number },\r\n        delta             : { type: String }\r\n    }\r\n});\r\nmongoose.model('Dummy', dummySchema);\r\n\r\nvar dummy = new mongoose.models.Dummy({ alpha: 'blabla', more: { beta: 'blabla' } });\r\ndummy.save(function(error, saved) {\r\n    saved.alpha = 'blabla2';\r\n    saved['more'] = {\r\n        beta              : 'blabla2',\r\n        gamma             : undefined,\r\n        delta             : 'blabla2'\r\n    };\r\n    saved.save(function(error, updated) {\r\n        console.log('done');\r\n    });\r\n});\r\n```\r\n\r\n**As you can see below gamma is null instead of being discarded**\r\n\r\n![image](https://cloud.githubusercontent.com/assets/2556751/8727932/52d78b1c-2bda-11e5-92c6-0c554c66bcee.png)"},"comment":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/comments/122317879","html_url":"https://github.com/Automattic/mongoose/issues/3169#issuecomment-122317879","issue_url":"https://api.github.com/repos/Automattic/mongoose/issues/3169","id":122317879,"user":{"login":"jlchereau","id":2556751,"avatar_url":"https://avatars.githubusercontent.com/u/2556751?v=3","gravatar_id":"","url":"https://api.github.com/users/jlchereau","html_url":"https://github.com/jlchereau","followers_url":"https://api.github.com/users/jlchereau/followers","following_url":"https://api.github.com/users/jlchereau/following{/other_user}","gists_url":"https://api.github.com/users/jlchereau/gists{/gist_id}","starred_url":"https://api.github.com/users/jlchereau/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jlchereau/subscriptions","organizations_url":"https://api.github.com/users/jlchereau/orgs","repos_url":"https://api.github.com/users/jlchereau/repos","events_url":"https://api.github.com/users/jlchereau/events{/privacy}","received_events_url":"https://api.github.com/users/jlchereau/received_events","type":"User","site_admin":false},"created_at":"2015-07-17T15:41:58Z","updated_at":"2015-07-17T15:41:58Z","body":"There is a matter of consistency:\r\n\r\nThe BSON serializer doe not treat gamma = undefined as gamma = null with:\r\n\r\n```\r\nvar dummySchema = new mongoose.Schema({\r\n    alpha            : { type: String },\r\n    beta              : { type: String },\r\n    gamma         : { type: Number },\r\n    delta             : { type: String }\r\n});\r\n```\r\n\r\nor with\r\n\r\n```\r\nvar dummySchema = new mongoose.Schema({\r\n    alpha             : { type: String },\r\n    more              : {\r\n        beta              : { type: String },\r\n        gamma             : { type: Number }\r\n        // removed delta\r\n    }\r\n});\r\n```\r\n\r\nand in the case reported, it only occurs i updates, not in inserts.\r\n\r\nGenerally, mongoose does not record undefined in database, and I think it is the right thing to do.\r\n"}}