{"id":14975644,"url":"https://github.com/yidinghan/mongoose-string-collection","last_synced_at":"2026-01-16T12:34:48.055Z","repository":{"id":57302272,"uuid":"88712840","full_name":"yidinghan/mongoose-string-collection","owner":"yidinghan","description":"A mongoose plugin that can help you quickly develop string collection related requirements","archived":false,"fork":false,"pushed_at":"2020-06-02T20:32:32.000Z","size":7085,"stargazers_count":1,"open_issues_count":8,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-09-20T14:56:21.132Z","etag":null,"topics":["mongo","mongodb","mongoose","mongoose-plugin","nodejs","schema"],"latest_commit_sha":null,"homepage":null,"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/yidinghan.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2017-04-19T07:09:09.000Z","updated_at":"2019-03-11T07:55:01.000Z","dependencies_parsed_at":"2022-09-14T22:54:46.487Z","dependency_job_id":null,"html_url":"https://github.com/yidinghan/mongoose-string-collection","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/yidinghan/mongoose-string-collection","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidinghan%2Fmongoose-string-collection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidinghan%2Fmongoose-string-collection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidinghan%2Fmongoose-string-collection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidinghan%2Fmongoose-string-collection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yidinghan","download_url":"https://codeload.github.com/yidinghan/mongoose-string-collection/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidinghan%2Fmongoose-string-collection/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28478683,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-16T11:59:17.896Z","status":"ssl_error","status_checked_at":"2026-01-16T11:55:55.838Z","response_time":107,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["mongo","mongodb","mongoose","mongoose-plugin","nodejs","schema"],"created_at":"2024-09-24T13:52:19.738Z","updated_at":"2026-01-16T12:34:48.021Z","avatar_url":"https://github.com/yidinghan.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mongoose-string-collection\n\n[![Greenkeeper badge](https://badges.greenkeeper.io/yidinghan/mongoose-string-collection.svg)](https://greenkeeper.io/)\n\n[![Travis](https://img.shields.io/travis/yidinghan/mongoose-string-collection.svg?style=flat-square)](\u003c\u003e)\n[![npm](https://img.shields.io/npm/l/mongoose-string-collection.svg?style=flat-square)](\u003c\u003e)\n[![npm](https://img.shields.io/npm/v/mongoose-string-collection.svg?style=flat-square)](\u003c\u003e)\n[![npm](https://img.shields.io/npm/dm/mongoose-string-collection.svg?style=flat-square)](\u003c\u003e)\n[![David](https://img.shields.io/david/yidinghan/mongoose-string-collection.svg?style=flat-square)](\u003c\u003e)\n[![David](https://img.shields.io/david/dev/yidinghan/mongoose-string-collection.svg?style=flat-square)](\u003c\u003e)\n\nA mongoose plugin that can help you quickly develop string collection related requirements\n\n\u003c!-- TOC --\u003e\n\n-   [mongoose-string-collection](#mongoose-string-collection)\n-   [Getting Start](#getting-start)\n    -   [NPM](#npm)\n    -   [Usage](#usage)\n-   [Configuration](#configuration)\n    -   [Different Field Name](#different-field-name)\n    -   [Index Elements/Collection](#index-elementscollection)\n    -   [Unique In Collection](#unique-in-collection)\n-   [JSDoc](#jsdoc)\n    -   [plugin](#plugin)\n    -   [model](#model)\n        -   [get](#get)\n        -   [remove](#remove)\n        -   [batchRemove](#batchremove)\n        -   [add](#add)\n        -   [batchAdd](#batchadd)\n        -   [replace](#replace)\n        -   [batchReplace](#batchreplace)\n\n\u003c!-- /TOC --\u003e\n\n# Getting Start\n\n## NPM\n\nInstallation\n\n```shell\nnpm i -S mongoose-string-collection\n```\n\n## Usage\n\nQuick code snippet\n\n```javascript\nconst stringCollection = require('mongoose-string-collection');\n\nschema.plugin(stringCollection);\n\n// init model, etc.\n\nmodel.addTags({ id: 'thisisid' }, ['thisistag']);\nmodel.getTags({ id: 'thisisid' });\n  .then(console.log) // ['thisistag']\nmodel.addTags({ id: 'thisisid' }, ['thisistagbro']);\nmodel.getTags({ id: 'thisisid' });\n  .then(console.log) // ['thisistag', 'thisistagbro']\n```\n\n# Configuration\n\n### Different Field Name\n\nThe default field [mongoose-string-collection](https://github.com/yidinghan/mongoose-string-collection) would add to schema is `tags`\n\nIf you want to change the field name, you can configuration by change [default options](#pluginschema-options)\n\n```javascript\nschema.plugin(stringCollection, {\n  fieldName: 'dingding'\n});\n\n// init model, etc.\n\nmodel.addDingding({ id: 'thisisid' }, ['thisistag']);\nmodel.getDingding({ id: 'thisisid' });\n  .then(console.log) // ['thisistag']\n```\n\n### Index Elements/Collection\n\nIf want to indexs the field created by [mongoose-string-collection](https://github.com/yidinghan/mongoose-string-collection), you can set `options.isIndex` to `true`\n\n```javascript\nschema.plugin(stringCollection, {\n  isIndex: true\n});\n\n// init model, etc.\n\nconst elementIndex = model.path('tags').caster.options.index;\n// true\n```\n\n### Unique In Collection\n\nSometimes the collection may not be a unique set of elements, but an array.\n\nIf you want an array, you can set `options.isUnique` to `false`.\n\n```javascript\nschema.plugin(stringCollection, {\n  isUnique: true // default also is true\n});\n\n// init model, etc.\n\nmodel.addDingding({ id: 'thisisid' }, ['t', 't1']);\nmodel.getDingding({ id: 'thisisid' });\n  .then(console.log) // ['t', 't1']\nmodel.addDingding({ id: 'thisisid' }, ['t', 't2']);\nmodel.getDingding({ id: 'thisisid' });\n  .then(console.log) // ['t', 't1', 't2]\n\n// set isUnique to false\nschema.plugin(stringCollection, {\n  isUnique: false\n});\n\n// init model, etc.\n\nmodel.addDingding({ id: 'thisisid' }, ['t', 't1']);\nmodel.getDingding({ id: 'thisisid' });\n  .then(console.log) // ['t', 't1']\nmodel.addDingding({ id: 'thisisid' }, ['t', 't2']);\nmodel.getDingding({ id: 'thisisid' });\n  .then(console.log) // ['t', 't1', 't', 't2]\n```\n\n# JSDoc\n\n\u003c!-- Generated by documentation.js. Update this documentation by updating the source code. --\u003e\n\n## plugin\n\na plugin that help schema to build string collection field\nwhich is an array containt batch string\n\n**Parameters**\n\n-   `schema` **MongooseSchema** mongoose schema that use this plugin\n-   `options` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?** plugin configuration (optional, default `{}`)\n    -   `options.fieldName` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** the name place in schema (optional, default `tags`)\n    -   `options.isIndex` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** whether index in target field (optional, default `false`)\n    -   `options.isUnique` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** whether unique the content in the collection (optional, default `true`)\n    -   `options.maxLength` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** The maximum size limit for the collection,\n                         if the input is greater than 0, will be treated as a valid input (optional, default `-1`)\n    -   `options.elementOptions` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?** collection element options\n    -   `options.updateOptions` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)?** collection default update options\n                         for add, replace and get methods.\n                         you can also override when using the specified method\n\n## model\n\n### get\n\nsugar method that get target filed as single result\n\n**Parameters**\n\n-   `query` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** mongoose query that place in this.findOne (optional, default `{}`)\n\n**Examples**\n\n```javascript\nmodel.getTags({ _id: 'targetnotexists' }).then(console.log);\n// undefined\n\nmodel.insert({ _id: 'test', tags: ['test'] });\nmodel.getTags({ _id: 'test' }).then(console.log);\n// ['test]\n```\n\nReturns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)\u0026lt;[array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)\u003e** target field\n\n### remove\n\nremove element array from target field\n\n**Parameters**\n\n-   `query` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** mongoose query to find out one update target\n-   `collection` **[array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)** string collection will remove from target document\n-   `updateOptions`  \n\n**Examples**\n\n```javascript\n// { _id: 'test', tags: ['t1', 't2'] }\nmodel.removeTags({ _id: 'test' }, ['t1']).then(console.log);\n// { _id: 'test', tags: ['t2'] }\nmodel.removeTags({ _id: 'test' }, ['t2']).then(console.log);\n// { _id: 'test', tags: [] }\n```\n\nReturns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)\u0026lt;[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)\u003e** updated target document\n\n### batchRemove\n\nbatch remove element array from target field\n\n**Parameters**\n\n-   `query` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** mongoose query to find out batch update target\n-   `collection` **[array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)** string collection will remove from batch target document\n-   `updateOptions`  \n\n**Examples**\n\n```javascript\n// { _id: 'test0', foo: 'bar', tags: ['t2'] }\n// { _id: 'test1', foo: 'bar', tags: ['t1', 't2'] }\nmodel.removeTags({ foo: 'bar' }, ['t1']).then(console.log);\n// { \"nMatched\" : 2, \"nUpserted\" : 0, \"nModified\" : 1 }\nmodel.removeTags({ foo: 'bar' }, ['t2']).then(console.log);\n// { \"nMatched\" : 2, \"nUpserted\" : 0, \"nModified\" : 2 }\n```\n\nReturns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)\u0026lt;[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)\u003e** mongoose udpate result\n\n### add\n\nadd string array to target field\n\n**Parameters**\n\n-   `query` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** mongoose query to find out update target\n-   `collection` **[array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)** string collection will add to target document\n-   `updateOptions`  \n\n**Examples**\n\n```javascript\nmodel.addTags({ _id: 'test' }, ['t1']).then(console.log);\n// { _id: 'test', tags: ['t1'] }\nmodel.addTags({ _id: 'test' }, ['t2']).then(console.log);\n// { _id: 'test', tags: ['t1', 't2'] }\n```\n\nReturns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)\u0026lt;[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)\u003e** updated target document\n\n### batchAdd\n\nbatch add element to collection\n\n**Parameters**\n\n-   `query` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** mongoose query to find out update target\n-   `collection` **[array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)** string collection will add to target document\n-   `updateOptions`  \n\n**Examples**\n\n```javascript\nmodel.batchAddTags({ _id: { $in: ['id1', 'id2] } }, ['t1', 't2']).then(console.log);\n// { \"nMatched\" : 2, \"nUpserted\" : 0, \"nModified\" : 2 }\nmodel.getTags({ _id: 'id1' }).then(console.log);\n// ['t1', 't2']\nmodel.batchAddTags({ _id: { $in: ['id1', 'id2] } }, ['t2', 't3']).then(console.log);\n// { \"nMatched\" : 1, \"nUpserted\" : 0, \"nModified\" : 1 }\nmodel.getTags({ _id: 'id2' }).then(console.log);\n// ['t1', 't2', 't3']\n```\n\nReturns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)\u0026lt;[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)\u003e** mongoose udpate result\n\n### replace\n\nupdate document's collection filed,\nwhich is first document find out by given query.\nreplace collection field with given collection\n\n**Parameters**\n\n-   `query` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** mongoose query to find out update target\n-   `collection` **[array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)** string collection will add to target document\n-   `updateOptions`  \n\n**Examples**\n\n```javascript\nmodel.replaceTags({ _id: 'test' }, ['t1']).then(console.log);\n// { _id: 'test', tags: ['t1'] }\nmodel.replaceTags({ _id: 'test' }, ['t2', 't3']).then(console.log);\n// { _id: 'test', tags: ['t2', 't3'] }\n```\n\nReturns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)\u0026lt;[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)\u003e** mongoose udpate result\n\n### batchReplace\n\nbatch update documents' collection filed\nby replace it with given collection\n\n**Parameters**\n\n-   `query` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** mongoose query to find out update target\n-   `collection` **[array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)** string collection will add to target document\n-   `updateOptions`  \n\n**Examples**\n\n```javascript\nmodel.batchReplaceTags({ _id: 'test' }, ['t1']).then(console.log);\n// { \"nMatched\" : 1, \"nUpserted\" : 0, \"nModified\" : 1 }\nmodel.getTags({ _id: 'test' }).then(console.log);\n// ['t1']\nmodel.batchReplaceTags({ _id: 'test' }, ['t2', 't3']).then(console.log);\n// { \"nMatched\" : 1, \"nUpserted\" : 0, \"nModified\" : 1 }\nmodel.getTags({ _id: 'test' }).then(console.log);\n// ['t2', 't3']\n```\n\nReturns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)\u0026lt;[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)\u003e** mongoose udpate result\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyidinghan%2Fmongoose-string-collection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyidinghan%2Fmongoose-string-collection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyidinghan%2Fmongoose-string-collection/lists"}