{"action":"closed","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/2988","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/2988/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/2988/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/2988/events","html_url":"https://github.com/Automattic/mongoose/issues/2988","id":76229201,"number":2988,"title":"Sub document refs cannot be retrieved","user":{"login":"doomdagger","id":6743894,"avatar_url":"https://avatars.githubusercontent.com/u/6743894?v=3","gravatar_id":"","url":"https://api.github.com/users/doomdagger","html_url":"https://github.com/doomdagger","followers_url":"https://api.github.com/users/doomdagger/followers","following_url":"https://api.github.com/users/doomdagger/following{/other_user}","gists_url":"https://api.github.com/users/doomdagger/gists{/gist_id}","starred_url":"https://api.github.com/users/doomdagger/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/doomdagger/subscriptions","organizations_url":"https://api.github.com/users/doomdagger/orgs","repos_url":"https://api.github.com/users/doomdagger/repos","events_url":"https://api.github.com/users/doomdagger/events{/privacy}","received_events_url":"https://api.github.com/users/doomdagger/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/Automattic/mongoose/milestones/41","html_url":"https://github.com/Automattic/mongoose/milestones/4.0.5","labels_url":"https://api.github.com/repos/Automattic/mongoose/milestones/41/labels","id":1103714,"number":41,"title":"4.0.5","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":17,"closed_issues":7,"state":"open","created_at":"2015-05-10T16:14:13Z","updated_at":"2015-06-03T19:43:51Z","due_on":"2015-06-05T04:00:00Z","closed_at":null},"comments":3,"created_at":"2015-05-14T04:42:41Z","updated_at":"2015-06-03T19:43:51Z","closed_at":"2015-06-03T19:43:51Z","body":"#2951 Now I am sure it's a bug in mongoose 4.0.2.\r\n\r\nHere is my code:\r\n```javascript\r\nvar blogSchema = new mongoose.Schema({\r\n    name: String,\r\n    title: String,\r\n    pet: {type: String, ref: 'Pet'}\r\n});\r\n\r\nvar petSchema = new mongoose.Schema({\r\n    name: String\r\n});\r\n\r\nvar Blog = mongoose.model(\"Blog\", blogSchema);\r\nvar Pet = mongoose.model(\"Pet\", petSchema);\r\n```\r\n\r\nIf I do the following, I will have this issue:\r\n```javascript\r\nvar pet = new Pet({name: \"huhu\"});\r\nvar blog = new Blog({name: \"haha\", title: \"gulu\", pet: pet});\r\n\r\nblog.save().then(function (doc) {\r\n    console.log(doc);\r\n    // { __v: 0, pet: { name: 'huhu', _id: 555424f98b8b32081cf1ab88 }, name: 'haha', title: 'gulu', _id: 555424f98b8b32081cf1ab89 }\r\n    \r\n    Blog.findOne().then(function (found) {\r\n         console.log(found);\r\n         // I cannot get the path `pet`\r\n         // { _id: 555424f98b8b32081cf1ab89 , name: 'haha', title: 'gulu', __v: 0 }\r\n    });\r\n});\r\n```\r\n\r\nI found out that even my blogSchema defined that I want use String on path pet, the database uses ObjectId.\r\n![image](https://cloud.githubusercontent.com/assets/6743894/7626160/eed77256-fa35-11e4-821c-6fb6a15bffd7.png)\r\n\r\nBut if I try this, this issue won't come out, and I can retrieve the pet path:\r\n```javascript\r\nvar blog = new Blog({name: \"haha\", title: \"gulu\", pet: pet._id});\r\n```\r\n`pet._id` will be cast into String, and then saved into the database. I think it is due to the type mismatch caused the sub-document cannot be retrieved. And this logic can be accepted to some degree. However, when inserting the document with sub document model, mongoose may forget to cast the ObjectId into String according to user's schema definition. \r\n"}}