{"issue":{"created_at":"2011-11-25T04:37:30Z","state":"open","updated_at":"2011-12-13T02:54:24Z","user":{"gravatar_id":"93b5d76a752e409012d668424a05acca","id":627060,"url":"https://api.github.com/users/jhoff","login":"jhoff","avatar_url":"https://secure.gravatar.com/avatar/93b5d76a752e409012d668424a05acca?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png"},"comments":4,"body":"As of 2.3.13, default values and type options that are specified in the schema are ignored during an upsert operation when the record is being inserted. Updates to existing records work as expected.\r\n\r\n```javascript\r\n//schema declaration\r\n\r\nvar Schema = require('mongoose').Schema;\r\n\r\nvar ip_address = \"0.0.0.0\";\r\n\r\nexports.beta_email = new Schema({\r\n\temail : { type: String, index: true, uppercase: true },\r\n\tip_address : { type: String, match: /[\\d]{1,3}\\.[\\d]{1,3}\\.[\\d]{1,3}\\.[\\d]{1,3}/, 'default': ip_address },\r\n\tfirst_submit : { type: Date, 'default': Date.now },\r\n\ttotal_submits : { type: Number, 'default': 1 },\r\n}, { collection: 'beta_email' });\r\n\r\n//upsert ( does not write fields with default values ( first_submit, ip_address ) and leaves the email in lowercase )\r\n\r\ndb.model('beta_email')\r\n\t.update(\r\n\t\t{ email: req.body.email },\r\n\t\t{ $inc: { total_submits: 1 } },\r\n\t\t{ upsert: true, multi: false },\r\n\t\tfunction(err) {}\r\n\t);\r\n\r\n//produces\r\n\r\n{\r\n   \"_id\": ObjectId(\"4ecf18651b77a21f69254acf\"),\r\n   \"email\": \"upsert@event.com\",\r\n   \"total_submits\": 1\r\n}\r\n\r\n\r\n// save \r\n\r\nvar Beta_Email = db.model('beta_email');\r\nvar be = new Beta_Email();\r\nbe.email = req.body.email;\r\nbe.save( function(err) {} );\r\n\r\n//produces this:\r\n\r\n{\r\n   \"email\": \"SAVE@EVENT.COM\",\r\n   \"_id\": ObjectId(\"4ecf1883f04792b1a5000002\"),\r\n   \"ip_address\": \"0.0.0.0\",\r\n   \"total_submits\": 1,\r\n   \"first_submit\": ISODate(\"2011-11-25T04: 24: 35.707Z\")\r\n} \r\n\r\n```","assignee":null,"id":2346550,"pull_request":{"patch_url":null,"html_url":null,"diff_url":null},"number":624,"labels":[],"title":"defaults are ignored on upsert","html_url":"https://github.com/LearnBoost/mongoose/issues/624","closed_at":null,"url":"https://api.github.com/repos/LearnBoost/mongoose/issues/624","milestone":null},"action":"created","comment":{"created_at":"2011-12-13T02:54:23Z","updated_at":"2011-12-13T02:54:23Z","user":{"gravatar_id":"e2e776d3d6fbc6a6b7e4c692bd080595","id":166834,"url":"https://api.github.com/users/aheckmann","login":"aheckmann","avatar_url":"https://secure.gravatar.com/avatar/e2e776d3d6fbc6a6b7e4c692bd080595?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png"},"body":"the upsert operation actually takes place on the db itself, it isn't a mongoose thing.\r\n\r\nre overwriting data:\r\n","id":3118052,"url":"https://api.github.com/repos/LearnBoost/mongoose/issues/comments/3118052"}}