{"action":"closed","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/3904","repository_url":"https://api.github.com/repos/Automattic/mongoose","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/3904/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/3904/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/3904/events","html_url":"https://github.com/Automattic/mongoose/issues/3904","id":135522590,"number":3904,"title":"Deep (multiple level) populate returns null if reference schema is not one of the parent schemas.","user":{"login":"JasonCust","id":587847,"avatar_url":"https://avatars.githubusercontent.com/u/587847?v=3","gravatar_id":"","url":"https://api.github.com/users/JasonCust","html_url":"https://github.com/JasonCust","followers_url":"https://api.github.com/users/JasonCust/followers","following_url":"https://api.github.com/users/JasonCust/following{/other_user}","gists_url":"https://api.github.com/users/JasonCust/gists{/gist_id}","starred_url":"https://api.github.com/users/JasonCust/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JasonCust/subscriptions","organizations_url":"https://api.github.com/users/JasonCust/orgs","repos_url":"https://api.github.com/users/JasonCust/repos","events_url":"https://api.github.com/users/JasonCust/events{/privacy}","received_events_url":"https://api.github.com/users/JasonCust/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/Automattic/mongoose/milestones/94","html_url":"https://github.com/Automattic/mongoose/milestones/4.4.7","labels_url":"https://api.github.com/repos/Automattic/mongoose/milestones/94/labels","id":1619149,"number":94,"title":"4.4.7","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":18,"closed_issues":3,"state":"open","created_at":"2016-03-03T00:42:52Z","updated_at":"2016-03-04T17:55:49Z","due_on":"2016-03-09T08:00:00Z","closed_at":null},"comments":2,"created_at":"2016-02-22T19:47:28Z","updated_at":"2016-03-04T17:55:49Z","closed_at":"2016-03-04T17:55:49Z","body":"I checked issue #3507 and this seems to be a somewhat related but not the same. Referencing the current documentation for [`populate`](http://mongoosejs.com/docs/populate.html), specifically section ___Populating across multiple levels___, it doesn't appear that there is a limitation for which schema can be referenced.\r\n\r\nFrom the documentation schema A circular referencing works as intended. Even extending that to Schema B referencing Schema A that then has a circular reference will still work but once you have Schema C referencing Schema B that references Schema A, the population returns `null`.\r\n\r\nTested on node 0.12.9, mongoDB: 3.0.6 and mongoose 4.4.4.\r\n\r\nFor Schema B referencing Schema A which has a circular reference:\r\n```javascript\r\nvar Schema = mongoose.Schema;\r\n\r\nvar personSchema = new Schema({\r\n    name: { type: String },\r\n    friends: [{ type: Schema.Types.ObjectId, ref: 'Person' }]\r\n});\r\n\r\nvar teamSchema = new Schema({\r\n    name: { type: String },\r\n    members: [{ type: Schema.Types.ObjectId, ref: 'Person' }]\r\n});\r\n\r\nvar Team = connection.model('Team', teamSchema);\r\nvar Person = connection.model('Person', personSchema);\r\n\r\nPromise.all([\r\n    Team.create([\r\n        { name: 'All-stars' },\r\n        { name: 'Reserves' }\r\n    ]),\r\n    Person.create([\r\n        {name: 'John'},\r\n        {name: 'Joe'},\r\n        {name: 'Jake'},\r\n        {name: 'Jane'},\r\n        {name: 'Jess'},\r\n        {name: 'Jill'}\r\n    ])\r\n]).then(function(results) {\r\n    var teams = results[0];\r\n    var persons = results[1];\r\n\r\n    teams[0].members = persons.slice(0, 3);\r\n    teams[1].members = persons.slice(3, 6);\r\n\r\n    return Promise.all([\r\n        teams[0].save(),\r\n        teams[1].save()\r\n    ].concat(persons.map(function(person, i) {\r\n        person.friends = persons.filter(function(v, j) {\r\n            return i !== j;\r\n        });\r\n\r\n        return person.save();\r\n    })));\r\n}).then(function(results) {\r\n    return Team.findById(results[0].id)\r\n    .populate({\r\n        path: 'members',\r\n        select: 'name friends',\r\n        populate: { path: 'friends', select: 'name' }\r\n    }).exec();\r\n}).then(function(team) {\r\n    console.log(JSON.stringify(team, null, 2));\r\n}).catch(function(err) {\r\n    console.err(err);\r\n});\r\n```\r\nOutputs:\r\n```javascript\r\n{\r\n  \"_id\": \"56cb633f9f8202382038ab4e\",\r\n  \"name\": \"All-stars\",\r\n  \"__v\": 1,\r\n  \"members\": [\r\n    {\r\n      \"_id\": \"56cb633f9f8202382038ab50\",\r\n      \"name\": \"John\",\r\n      \"friends\": [\r\n        {\r\n          \"_id\": \"56cb633f9f8202382038ab51\",\r\n          \"name\": \"Joe\"\r\n        },\r\n        {\r\n          \"_id\": \"56cb633f9f8202382038ab52\",\r\n          \"name\": \"Jake\"\r\n        },\r\n        {\r\n          \"_id\": \"56cb633f9f8202382038ab53\",\r\n          \"name\": \"Jane\"\r\n        },\r\n        {\r\n          \"_id\": \"56cb633f9f8202382038ab54\",\r\n          \"name\": \"Jess\"\r\n        },\r\n        {\r\n          \"_id\": \"56cb633f9f8202382038ab55\",\r\n          \"name\": \"Jill\"\r\n        }\r\n      ]\r\n    },\r\n    {\r\n      \"_id\": \"56cb633f9f8202382038ab51\",\r\n      \"name\": \"Joe\",\r\n      \"friends\": [\r\n        {\r\n          \"_id\": \"56cb633f9f8202382038ab50\",\r\n          \"name\": \"John\"\r\n        },\r\n        {\r\n          \"_id\": \"56cb633f9f8202382038ab52\",\r\n          \"name\": \"Jake\"\r\n        },\r\n        {\r\n          \"_id\": \"56cb633f9f8202382038ab53\",\r\n          \"name\": \"Jane\"\r\n        },\r\n        {\r\n          \"_id\": \"56cb633f9f8202382038ab54\",\r\n          \"name\": \"Jess\"\r\n        },\r\n        {\r\n          \"_id\": \"56cb633f9f8202382038ab55\",\r\n          \"name\": \"Jill\"\r\n        }\r\n      ]\r\n    },\r\n    {\r\n      \"_id\": \"56cb633f9f8202382038ab52\",\r\n      \"name\": \"Jake\",\r\n      \"friends\": [\r\n        {\r\n          \"_id\": \"56cb633f9f8202382038ab50\",\r\n          \"name\": \"John\"\r\n        },\r\n        {\r\n          \"_id\": \"56cb633f9f8202382038ab51\",\r\n          \"name\": \"Joe\"\r\n        },\r\n        {\r\n          \"_id\": \"56cb633f9f8202382038ab53\",\r\n          \"name\": \"Jane\"\r\n        },\r\n        {\r\n          \"_id\": \"56cb633f9f8202382038ab54\",\r\n          \"name\": \"Jess\"\r\n        },\r\n        {\r\n          \"_id\": \"56cb633f9f8202382038ab55\",\r\n          \"name\": \"Jill\"\r\n        }\r\n      ]\r\n    }\r\n  ]\r\n}\r\n```\r\nThis is the expected outcome.\r\n\r\nHowever now with Schema C referencing Schema B that references Schema A:\r\n```javascript\r\nvar Schema = mongoose.Schema;\r\n\r\nvar personSchema = new Schema({\r\n    name: { type: String },\r\n    friends: [{ type: Schema.Types.ObjectId, ref: 'Person' }]\r\n});\r\n\r\nvar teamSchema = new Schema({\r\n    name: { type: String },\r\n    members: [{ type: Schema.Types.ObjectId, ref: 'Person' }]\r\n});\r\n\r\nvar gameSchema = new Schema({\r\n    team: { type: Schema.Types.ObjectId, ref: 'Team'},\r\n    opponent: { type: Schema.Types.ObjectId, ref: 'Team'}\r\n});\r\n\r\nvar Team = connection.model('Team', teamSchema);\r\nvar Person = connection.model('Person', personSchema);\r\nvar Game = connection.model('Game', gameSchema);\r\n\r\nPromise.all([\r\n    Team.create([\r\n        { name: 'All-stars' },\r\n        { name: 'Reserves' }\r\n    ]),\r\n    Person.create([\r\n        {name: 'John'},\r\n        {name: 'Joe'},\r\n        {name: 'Jake'},\r\n        {name: 'Jane'},\r\n        {name: 'Jess'},\r\n        {name: 'Jill'}\r\n    ])\r\n]).then(function(results) {\r\n    var teams = results[0];\r\n    var persons = results[1];\r\n    var game = new Game({\r\n        team: teams[0],\r\n        opponent: teams[1]\r\n    });\r\n\r\n    teams[0].members = persons.slice(0, 3);\r\n    teams[1].members = persons.slice(3, 6);\r\n\r\n    return Promise.all([\r\n        teams[0].save(),\r\n        teams[1].save(),\r\n        game.save()\r\n    ].concat(persons.map(function(person, i) {\r\n        person.friends = persons.filter(function(v, j) {\r\n            return i !== j;\r\n        });\r\n\r\n        return person.save();\r\n    })));\r\n}).then(function(results) {\r\n    return Game.findById(results[2].id)\r\n    .populate({\r\n        path: 'team',\r\n        select: 'name members'\r\n    })\r\n    //*******************************************\r\n    // BELOW IS THE OFFENDING CODE\r\n    //*******************************************\r\n    .populate({\r\n        path: 'opponent',\r\n        select: 'name members',\r\n        populate: { path: 'members', select: 'name' }\r\n    }).exec();\r\n}).then(function(game) {\r\n    console.log(JSON.stringify(game, null, 2));\r\n}).catch(function(err) {\r\n    console.err(err);\r\n});\r\n```\r\nOutputs:\r\n```javascript\r\n{\r\n  \"_id\": \"56cb645bf896fba4241ce0ac\",\r\n  \"team\": {\r\n    \"_id\": \"56cb645bf896fba4241ce0a4\",\r\n    \"name\": \"All-stars\",\r\n    \"members\": [ // HERE WITHOUT THE DEEP POPULATE WE HAVE IDS\r\n      \"56cb645bf896fba4241ce0a6\",\r\n      \"56cb645bf896fba4241ce0a7\",\r\n      \"56cb645bf896fba4241ce0a8\"\r\n    ]\r\n  },\r\n  \"opponent\": {\r\n    \"_id\": \"56cb645bf896fba4241ce0a5\",\r\n    \"name\": \"Reserves\",\r\n    \"members\": []  // HERE WITH DEEP POPULATE WE HAVE ALL NULLS\r\n  },\r\n  \"__v\": 0\r\n}\r\n```"}}