{"id":14975650,"url":"https://github.com/gberger/mongoose-named-scopes","last_synced_at":"2025-10-27T14:31:06.090Z","repository":{"id":57302140,"uuid":"61657408","full_name":"gberger/mongoose-named-scopes","owner":"gberger","description":":four_leaf_clover: Define custom, chainable Mongoose queries","archived":false,"fork":false,"pushed_at":"2016-07-02T14:37:06.000Z","size":17,"stargazers_count":58,"open_issues_count":2,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-09-28T20:42:50.566Z","etag":null,"topics":["chainable-mongoose-queries","mongoose-plugin"],"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/gberger.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-06-21T18:31:28.000Z","updated_at":"2024-09-18T08:28:22.000Z","dependencies_parsed_at":"2022-09-20T20:00:24.963Z","dependency_job_id":null,"html_url":"https://github.com/gberger/mongoose-named-scopes","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gberger%2Fmongoose-named-scopes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gberger%2Fmongoose-named-scopes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gberger%2Fmongoose-named-scopes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gberger%2Fmongoose-named-scopes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gberger","download_url":"https://codeload.github.com/gberger/mongoose-named-scopes/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219861083,"owners_count":16556007,"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":["chainable-mongoose-queries","mongoose-plugin"],"created_at":"2024-09-24T13:52:20.257Z","updated_at":"2025-10-27T14:31:05.777Z","avatar_url":"https://github.com/gberger.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mongoose-named-scopes\n\n:four_leaf_clover: Define custom, chainable Mongoose queries\n\n```javascript\n// Definition (placed in Scheme)\nProductSchema.scope('available').where('available').equals(true);\nProductSchema.scope('mostRecent', function(count) {\n  return this.sort('-updatedAt').limit(count);\n});\nProductSchema.scope('category', function(cat) {\n  return this.where('category').equals(cat);\n};\n// etc\n\n// Usage (called from anywhere)\nProduct.category('men').available().mostRecent(10);\nUser.male().olderThan(18).sortByAge().populateProfile();\nTask.assignedTo(john).highPriority().project('mongoose').limit(5);\n```\n\n\n## Usage\n\nInstall it with npm:\n\n```bash\nnpm install --save mongoose-named-scopes\n```\n\nFirst, you need to register the plugin into the schemas that you want to use it:\n\n```javascript\nvar namedScopesPlugin = require('mongoose-named-scopes');\n\n// For one Schema\nUserSchema.plugin(namedScopesPlugin);\n\n// For all Schemas at once\nmongoose.plugin(namedScopesPlugin);\n```\n\nThen, use `schema.scope` (or `schema.namedScope`) to define your scopes:\n\n```javascript\n// You can define scopes by chaining operator calls\nUserSchema.scope('male').where('gender').equals('male');\n\n// Or you can pass a function, for when you want to have parameters\n// or need to use other statements\nUserSchema.scope('olderThan', function (age) {\n  // Be sure to return `this`!\n  return this.where('age').gt(age);\n});\n\nUserSchema.scope('youngerThan', function (age) {\n  return this.where('age').lt(age);\n});\n\n// Scopes can make use of other scopes!\nUserSchema.scope('twenties').olderThan(19).youngerThan(30);\n\n// Heads up! We need to implement this by passing a function so that the\n// date parameter gets evaluated when you actually call the scope\nUserSchema.scope('active', function () {\n  const yesterday = +new Date() - 24*60*60*1000;\n  return this.where('lastLogin').gte(yesterday);\n});\n```\n\nNow, use the named scopes as if they were query functions:\n\n```javascript\n// You can specify more operators\nUser.populate('children').olderThan(50).sort('age'); // ...\n\n// Returning array results\nUser.olderThan(20).exec().then((users) =\u003e {}).catch(err);\n\n// Returning single results\nUser.olderThan(100).findOne().exec().then((users) =\u003e {}).catch(err);\n```\n\nEnjoy!\n\n\n## Contributing\n\nContributions are greatly appreciated!\n\nThis is a very new library that extends Mongoose in some unusual ways.\nPlease report bugs in the Issues.\n\nFeel free to develop additional features or fix bugs and send them over\nas Pull Requests.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgberger%2Fmongoose-named-scopes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgberger%2Fmongoose-named-scopes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgberger%2Fmongoose-named-scopes/lists"}