mongoose-string-collection
A mongoose plugin that can help you quickly develop string collection related requirements
Getting Start
NPM
Installation
npm i -S mongoose-string-collection
Usage
Quick code snippet
const stringCollection = ; schema; // init model, etc. model;model; // ['thisistag']model;model; // ['thisistag', 'thisistagbro']
Configuration
Different Field Name
The default field mongoose-string-collection would add to schema is tags
If you want to change the field name, you can configuration by change default options
schema; // init model, etc. model;model; // ['thisistag']
Index Elements/Collection
If want to indexs the field created by mongoose-string-collection, you can set options.isIndex
to true
schema; // init model, etc. const elementIndex = modelcasteroptionsindex;// true
Unique In Collection
Sometimes the collection may not be a unique set of elements, but an array.
If you want an array, you can set options.isUnique
to false
.
schema; // init model, etc. model;model; // ['t', 't1']model;model; // ['t', 't1', 't2] // set isUnique to falseschema; // init model, etc. model;model; // ['t', 't1']model;model; // ['t', 't1', 't', 't2]
JSDoc
plugin
a plugin that help schema to build string collection field which is an array containt batch string
Parameters
schema
MongooseSchema mongoose schema that use this pluginoptions
object? plugin configuration (optional, default{}
)options.fieldName
string the name place in schema (optional, defaulttags
)options.isIndex
boolean whether index in target field (optional, defaultfalse
)options.isUnique
boolean whether unique the content in the collection (optional, defaulttrue
)options.maxLength
number The maximum size limit for the collection, if the input is greater than 0, will be treated as a valid input (optional, default-1
)options.elementOptions
object? collection element optionsoptions.updateOptions
object? collection default update options for add, replace and get methods. you can also override when using the specified method
model
get
sugar method that get target filed as single result
Parameters
query
object mongoose query that place in this.findOne (optional, default{}
)
Examples
model;// undefined model;model;// ['test]
Returns Promise<array> target field
remove
remove element array from target field
Parameters
query
object mongoose query to find out one update targetcollection
array string collection will remove from target documentupdateOptions
Examples
// { _id: 'test', tags: ['t1', 't2'] }model;// { _id: 'test', tags: ['t2'] }model;// { _id: 'test', tags: [] }
Returns Promise<object> updated target document
batchRemove
batch remove element array from target field
Parameters
query
object mongoose query to find out batch update targetcollection
array string collection will remove from batch target documentupdateOptions
Examples
// { _id: 'test0', foo: 'bar', tags: ['t2'] }// { _id: 'test1', foo: 'bar', tags: ['t1', 't2'] }model;// { "nMatched" : 2, "nUpserted" : 0, "nModified" : 1 }model;// { "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 }
Returns Promise<object> mongoose udpate result
add
add string array to target field
Parameters
query
object mongoose query to find out update targetcollection
array string collection will add to target documentupdateOptions
Examples
model;// { _id: 'test', tags: ['t1'] }model;// { _id: 'test', tags: ['t1', 't2'] }
Returns Promise<object> updated target document
batchAdd
batch add element to collection
Parameters
query
object mongoose query to find out update targetcollection
array string collection will add to target documentupdateOptions
Examples
model;// { "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }model;// ['t1', 't2', 't3']
Returns Promise<object> mongoose udpate result
replace
update document's collection filed, which is first document find out by given query. replace collection field with given collection
Parameters
query
object mongoose query to find out update targetcollection
array string collection will add to target documentupdateOptions
Examples
model;// { _id: 'test', tags: ['t1'] }model;// { _id: 'test', tags: ['t2', 't3'] }
Returns Promise<object> mongoose udpate result
batchReplace
batch update documents' collection filed by replace it with given collection
Parameters
query
object mongoose query to find out update targetcollection
array string collection will add to target documentupdateOptions
Examples
model;// { "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }model;// ['t1']model;// { "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }model;// ['t2', 't3']