{"id":13398809,"url":"https://github.com/buunguyen/mongoose-deep-populate","last_synced_at":"2025-05-16T18:07:07.440Z","repository":{"id":22800111,"uuid":"26146564","full_name":"buunguyen/mongoose-deep-populate","owner":"buunguyen","description":"Mongoose plugin to enable deep population of nested models ⛺","archived":false,"fork":false,"pushed_at":"2020-07-15T20:10:08.000Z","size":82,"stargazers_count":468,"open_issues_count":11,"forks_count":45,"subscribers_count":14,"default_branch":"master","last_synced_at":"2025-04-12T16:58:06.987Z","etag":null,"topics":["mongoose","mongoose-plugin","population"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/buunguyen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["buunguyen"]}},"created_at":"2014-11-04T01:08:15.000Z","updated_at":"2024-12-22T18:15:13.000Z","dependencies_parsed_at":"2022-09-05T01:20:18.840Z","dependency_job_id":null,"html_url":"https://github.com/buunguyen/mongoose-deep-populate","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/buunguyen%2Fmongoose-deep-populate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/buunguyen%2Fmongoose-deep-populate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/buunguyen%2Fmongoose-deep-populate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/buunguyen%2Fmongoose-deep-populate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/buunguyen","download_url":"https://codeload.github.com/buunguyen/mongoose-deep-populate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254582905,"owners_count":22095518,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mongoose","mongoose-plugin","population"],"created_at":"2024-07-30T19:00:31.811Z","updated_at":"2025-05-16T18:07:07.421Z","avatar_url":"https://github.com/buunguyen.png","language":"JavaScript","funding_links":["https://github.com/sponsors/buunguyen"],"categories":["Podcast","JavaScript","🛠 General Utilities","Persistence"],"sub_categories":["Angular"],"readme":"Mongoose plugin to enable population of nested models at any level of depth. Support **Mongoose 3.8.x** and later. Refer to [changelog](https://github.com/buunguyen/mongoose-deep-populate#changelog) for breaking changes.\n\n[![NPM](https://nodei.co/npm/mongoose-deep-populate.png?downloads=true\u0026downloadRank=true\u0026stars=true)](https://www.npmjs.com/package/mongoose-deep-populate)\n\n[![Build Status](https://travis-ci.org/buunguyen/mongoose-deep-populate.svg?branch=master)](https://travis-ci.org/buunguyen/mongoose-deep-populate)\n[![Code Shelter](https://www.codeshelter.co/static/badges/badge-flat.svg)](https://www.codeshelter.co/)\n\n### Usage\n\nSample usages are based on the following schemas:\n\n```javascript\nvar UserSchema = new Schema({})\n\nvar CommentSchema = new Schema({\n  user  : {type: Number, ref: 'User'}\n})\n\nvar PostSchema = new Schema({\n  user    : {type: Number, ref: 'User'},\n  comments: [{type: Number, ref: 'Comment'}],\n  likes   : [{user: {type: Number, ref: 'User'}}],\n  approved: {status: Boolean, user: {type: Number, ref: 'User'}}\n})\n```\n\n#### Register plugin\n\n```javascript\n// CHANGE from 1.x: need to pass in mongoose instance\nvar deepPopulate = require('mongoose-deep-populate')(mongoose);\nPostSchema.plugin(deepPopulate, options /* more on options below */);\n```\n\n#### Perform population\n\nOn `Post` model:\n\n```javascript\n\n// Use callback\nPost.deepPopulate(posts, 'comments.user', function (err, _posts) {\n  // _posts is the same instance as posts and provided for convenience\n  posts.forEach(function (post) {\n    // post.comments and post.comments.user are fully populated\n  });\n});\n\n// Use promise\nPost.deepPopulate(posts, 'comments.user').then(...);\n```\n\nOn an instance of `Post`:\n\n```javascript\n// Use callback\npost.deepPopulate('comments.user', function (err, _post) {\n  // _post is the same instance as post and provided for convenience\n});\n\n\n// Use promise\npost.deepPopulate('comments.user').then(...);\n```\n\nOn `Query` (returns the same query object to chain):\n\n```javascript\nPost.find().deepPopulate('comments.user').exec(function (err, posts) { ... });\nPost.findOne().deepPopulate('comments.user').exec(function (err, post) { ... });\nPost.findById(id).deepPopulate('comments.user').exec(function (err, post) { ... });\n```\n\n\n#### Populate multiple paths\n\nPass paths in a space- or comma-delimited string:\n\n```javascript\npost.deepPopulate('user comments.user likes.user approved.user', cb);\n```\nOr use an array of strings:\n\n```javascript\npost.deepPopulate(['comments.user', 'user', 'likes.user', 'approved.user'], cb);\n```\n\n#### Specify options\n\nSpecify `whitelist` option to ensure only certain paths can be populated. This is to prevent potential performance and security issues if you allow API clients to supply population paths.\n\n```javascript\nPostSchema.plugin(deepPopulate, {\n  whitelist: [\n    'user',\n    'comments.user'\n  ]\n});\n```\n\nUse the `populate` option to supply paths with corresponding [Mongoose populate options](http://mongoosejs.com/docs/api.html#model_Model.populate).\n\n```javascript\nPostSchema.plugin(deepPopulate, {\n  populate: {\n    'comments.user': {\n      select: 'name',\n      options: {\n        limit: 5\n      }\n    },\n    'approved.user': {\n      select: 'name'\n    }\n  }\n});\n```\n\nUse `rewrite` option to rewrite provided paths as well as paths in `whitelist` and `populate`. This is useful when you allow API clients to supply population paths (e.g. via query string) and want to make these paths more user-friendly. For example:\n\n```javascript\nPostSchema.plugin(deepPopulate, {\n  rewrite: {\n    author: 'user',\n    approver: 'approved.user'\n  }\n});\n\n// assume the query string is: ?populate=author,approver\npost.deepPopulate(req.query.populate, cb);  \n```\n\n##### Overriding options\n\nYou can override the above plugin options when invoking `deepPopulate`.\n\n```javascript\nPost.deepPopulate(posts, paths, {\n  whitelist: [],\n  populate: {},\n  rewrite: {}\n}, cb)\n\npost.deepPopulate(paths, {\n  whitelist: [],\n  populate: {},\n  rewrite: {}\n}, cb);\n\nPost.find({}).deepPopulate(paths, {\n  whitelist: [],\n  populate: {},\n  rewrite: {}\n}).exec(cb)\n```\n\n\n### Test\n\nThe test suite will **drop the database** each run, so only run it against a test database. To run tests, execute this command where `--db` is the connection string.\n\n```\ngulp test --db mongodb://127.0.0.1/mongoose_deep_populate_test_db\n```\n\n### Changelog\n\n#### v3.2.0\n\n* [Bug] Support async callback #70\n\n#### v3.1.1\n\n* [Bug] Fix issue custom Promise not work in Mongoose 5\n\n#### v3.1.0\n\n* [Feature] Support Mongoose 5\n\n#### v3.0.0\n\n* [Feature] Return a promise when invoking `deepPopulate()` on model or model instance.\n\n#### v2.0.3\n\n* [Bug] Fix bug cannot use native ES6 Promise\n\n#### v2.0.2\n\n* [Bug] Fix bug populating when there is a subdocument within a linked document (see #29)\n\n#### v2.0.0\n\n* [Breaking] Need a mongoose instance passed to the function returned by `require`\n* [Feature] Support mongoose promise provider\n\n#### v1.1.0\n\n* [Feature] Make mongoose a peer dependency to enforce supported versions\n\n#### v1.0.2\n\n* [Bug] Fix bug happening when Mongoose#populate does not infer the expected schema\n\n#### v1.0.1\n\n* [Bug] Apply `lean` to populated documents\n\n#### v1.0.0\n\n* [Feature] Apply `rewrites` to `whitelist` and `populate`\n\n#### v0.0.7\n\n* [Feature] Add `deepPopulate` to `Query`\n* [Feature] Support space delimiter in paths\n\n#### v0.0.6\n\n* [Feature] Support populate options\n* [Feature] Override options per call\n* [Bug] Handle null paths and callback\n\n#### v0.0.1\n\n* Initial release\n\n\n### License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbuunguyen%2Fmongoose-deep-populate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbuunguyen%2Fmongoose-deep-populate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbuunguyen%2Fmongoose-deep-populate/lists"}