{"action":"created","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/3416","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/3416/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/3416/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/3416/events","html_url":"https://github.com/Automattic/mongoose/issues/3416","id":108751859,"number":3416,"title":"sub-doc is not saved when parent object saved","user":{"login":"swrocket","id":13151805,"avatar_url":"https://avatars.githubusercontent.com/u/13151805?v=3","gravatar_id":"","url":"https://api.github.com/users/swrocket","html_url":"https://github.com/swrocket","followers_url":"https://api.github.com/users/swrocket/followers","following_url":"https://api.github.com/users/swrocket/following{/other_user}","gists_url":"https://api.github.com/users/swrocket/gists{/gist_id}","starred_url":"https://api.github.com/users/swrocket/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/swrocket/subscriptions","organizations_url":"https://api.github.com/users/swrocket/orgs","repos_url":"https://api.github.com/users/swrocket/repos","events_url":"https://api.github.com/users/swrocket/events{/privacy}","received_events_url":"https://api.github.com/users/swrocket/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-09-28T21:36:30Z","updated_at":"2015-10-07T18:04:15Z","closed_at":null,"body":"I have a hierarchy that I'm trying to save. The top level object gets saved but the sub object does not get updated.\r\n\r\n    var UserSchema = mongoose.Schema({\r\n        email:String,\r\n        password:String,\r\n        googleId:String,\r\n        facebookId: String,\r\n        twitterId:String,\r\n        displayName:String,\r\n        active:{type:Boolean, default:false},\r\n        infoComplete:{type:Boolean, default:false},\r\n        phone: String,\r\n        move: { type: Schema.Types.ObjectId, ref: 'Move'}\r\n    });\r\n    var moveSchema = mongoose.Schema({\r\n        oldHome: {\r\n            address: { type: String,  default:''},\r\n            suite: { type: String,  default:''},\r\n            city: { type: String, default:''},\r\n            state: { type: String,  default:''},\r\n            zip: { type: String, default:''},\r\n            geo: {\r\n                lat: {type:Number,default:0},\r\n                lon: {type:Number,default:0}\r\n            },\r\n            rentown:  { type: String, enum: ['own', 'rent'], default:'own'}\r\n        },\r\n        newHome: {\r\n            address: { type: String,  default:''},\r\n            suite: { type: String,  default:''},\r\n            city: { type: String,  default:''},\r\n            state: { type: String,  default:''},\r\n            zip: { type: String,  default:''},\r\n            geo: {\r\n                lat: {type:Number,default:0},\r\n                lon: {type:Number,default:0}\r\n            },\r\n            rentown:  { type: String, enum: ['own', 'rent'], default:'own' }\r\n        },\r\n        moveoutDate: {type:Date, default:new moment().add(60, 'days').toDate()},\r\n        moveinDate: {type:Date, default:new moment().add(60, 'days').toDate()},\r\n        createdDate: {type: Date, default:Date.now()},\r\n        activities: [moveActivitySchema]\r\n    });\r\n\r\n     //middleware\r\n    router.use(\"/:userId\", function(req, res, next){\r\n        User.findById(req.params.userId).populate('move').exec(function(err, user){\r\n            if(err){\r\n                res.status(500).send(err);\r\n            }\r\n            else if(user){\r\n                req.user = user;\r\n                next();\r\n            }\r\n            else\r\n                res.status(404).send(\"No time frame found\");\r\n        });\r\n    });\r\n\r\n    router.route('/:userId')\r\n        .get(function(req, res){\r\n            res.json(req.user); //this uses the middleware above\r\n        })\r\n        .put(function(req, res){\r\n            //update the user.\r\n            //req.user is set in the middleware\r\n\r\n            req.user.displayName = req.body.displayName;\r\n            req.user.email = req.body.email;\r\n            req.user.phone = req.body.phone;\r\n            req.user.infoComplete = req.body.infoComplete;\r\n            req.user.move = req.body.move;\r\n\r\n            console.log(req.user.move ); //this show the right data\r\n\r\n            req.user.save(function(err){\r\n                if (err)\r\n                    res.status(500).send(err);\r\n                else\r\n                    res.json(req.user);\r\n            });\r\n        })\r\n        .patch(function(req, res){\r\n            if (req.body._id){\r\n                delete req.body._id;\r\n            }\r\n            for (var p in req.body){\r\n                req.user[p] = req.body[p];\r\n            }\r\n            req.user.save(function(err){\r\n                if (err)\r\n                    res.status(500).send(err);\r\n                else\r\n                    res.json(req.user);\r\n            });\r\n        })\r\n        .delete(function(req, res){\r\n            req.user.remove(function(err){\r\n                if (err)\r\n                    res.status(500).send(err);\r\n                else\r\n                    res.status(204).send(\"Deleted\");\r\n            });\r\n        });\r\n\r\nThis is the debug\r\nMongoose: users.update({ _id: ObjectId(\"56097036db0a99dc2bb42e41\") }) { '$set': { move: ObjectId(\"56097036db0a99dc2bb42e40\") } }\r\n\r\nIf this is a stupid question please forgive me and refer me to the place in the docs where it's addressed. I come for an Entity Framework world and this is new to me.\r\n\r\nTIA,\r\n\r\nAndrew"},"comment":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/comments/146279911","html_url":"https://github.com/Automattic/mongoose/issues/3416#issuecomment-146279911","issue_url":"https://api.github.com/repos/Automattic/mongoose/issues/3416","id":146279911,"user":{"login":"swrocket","id":13151805,"avatar_url":"https://avatars.githubusercontent.com/u/13151805?v=3","gravatar_id":"","url":"https://api.github.com/users/swrocket","html_url":"https://github.com/swrocket","followers_url":"https://api.github.com/users/swrocket/followers","following_url":"https://api.github.com/users/swrocket/following{/other_user}","gists_url":"https://api.github.com/users/swrocket/gists{/gist_id}","starred_url":"https://api.github.com/users/swrocket/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/swrocket/subscriptions","organizations_url":"https://api.github.com/users/swrocket/orgs","repos_url":"https://api.github.com/users/swrocket/repos","events_url":"https://api.github.com/users/swrocket/events{/privacy}","received_events_url":"https://api.github.com/users/swrocket/received_events","type":"User","site_admin":false},"created_at":"2015-10-07T18:04:15Z","updated_at":"2015-10-07T18:04:15Z","body":"Yes, that's what I'm trying to do. I expected, perhaps in error, that I could populate an entire hierarchy and save it all at once."}}