This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example usage: | |
// deepPopulate(blogPost, "comments comments._creator comments._creator.blogposts", {sort:{title:-1}}, callback); | |
// Note that the options get passed at *every* level! | |
// Also note that you must populate the shallower documents before the deeper ones. | |
function deepPopulate(doc, pathListString, options, callback) { | |
var listOfPathsToPopulate = pathListString.split(" "); | |
function doNext() { | |
if (listOfPathsToPopulate.length == 0) { | |
// Now all the things underneath the original doc should be populated. Thanks mongoose! | |
callback(null,doc); |