mongoose-string-collection v1.4.0
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-collectionUsage
Quick code snippet
const stringCollection = require('mongoose-string-collection');
schema.plugin(stringCollection);
// init model, etc.
model.addTags({ id: 'thisisid' }, ['thisistag']);
model.getTags({ id: 'thisisid' });
.then(console.log) // ['thisistag']
model.addTags({ id: 'thisisid' }, ['thisistagbro']);
model.getTags({ id: 'thisisid' });
.then(console.log) // ['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.plugin(stringCollection, {
fieldName: 'dingding'
});
// init model, etc.
model.addDingding({ id: 'thisisid' }, ['thisistag']);
model.getDingding({ id: 'thisisid' });
.then(console.log) // ['thisistag']Index Elements/Collection
If want to indexs the field created by mongoose-string-collection, you can set options.isIndex to true
schema.plugin(stringCollection, {
isIndex: true
});
// init model, etc.
const elementIndex = model.path('tags').caster.options.index;
// trueUnique 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.plugin(stringCollection, {
isUnique: true // default also is true
});
// init model, etc.
model.addDingding({ id: 'thisisid' }, ['t', 't1']);
model.getDingding({ id: 'thisisid' });
.then(console.log) // ['t', 't1']
model.addDingding({ id: 'thisisid' }, ['t', 't2']);
model.getDingding({ id: 'thisisid' });
.then(console.log) // ['t', 't1', 't2]
// set isUnique to false
schema.plugin(stringCollection, {
isUnique: false
});
// init model, etc.
model.addDingding({ id: 'thisisid' }, ['t', 't1']);
model.getDingding({ id: 'thisisid' });
.then(console.log) // ['t', 't1']
model.addDingding({ id: 'thisisid' }, ['t', 't2']);
model.getDingding({ id: 'thisisid' });
.then(console.log) // ['t', 't1', 't', 't2]JSDoc
plugin
a plugin that help schema to build string collection field which is an array containt batch string
Parameters
schemaMongooseSchema mongoose schema that use this pluginoptionsobject? plugin configuration (optional, default{})options.fieldNamestring the name place in schema (optional, defaulttags)options.isIndexboolean whether index in target field (optional, defaultfalse)options.isUniqueboolean whether unique the content in the collection (optional, defaulttrue)options.maxLengthnumber 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.elementOptionsobject? collection element optionsoptions.updateOptionsobject? 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
queryobject mongoose query that place in this.findOne (optional, default{})
Examples
model.getTags({ _id: 'targetnotexists' }).then(console.log);
// undefined
model.insert({ _id: 'test', tags: ['test'] });
model.getTags({ _id: 'test' }).then(console.log);
// ['test]Returns Promise<array> target field
remove
remove element array from target field
Parameters
queryobject mongoose query to find out one update targetcollectionarray string collection will remove from target documentupdateOptions
Examples
// { _id: 'test', tags: ['t1', 't2'] }
model.removeTags({ _id: 'test' }, ['t1']).then(console.log);
// { _id: 'test', tags: ['t2'] }
model.removeTags({ _id: 'test' }, ['t2']).then(console.log);
// { _id: 'test', tags: [] }Returns Promise<object> updated target document
batchRemove
batch remove element array from target field
Parameters
queryobject mongoose query to find out batch update targetcollectionarray string collection will remove from batch target documentupdateOptions
Examples
// { _id: 'test0', foo: 'bar', tags: ['t2'] }
// { _id: 'test1', foo: 'bar', tags: ['t1', 't2'] }
model.removeTags({ foo: 'bar' }, ['t1']).then(console.log);
// { "nMatched" : 2, "nUpserted" : 0, "nModified" : 1 }
model.removeTags({ foo: 'bar' }, ['t2']).then(console.log);
// { "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 }Returns Promise<object> mongoose udpate result
add
add string array to target field
Parameters
queryobject mongoose query to find out update targetcollectionarray string collection will add to target documentupdateOptions
Examples
model.addTags({ _id: 'test' }, ['t1']).then(console.log);
// { _id: 'test', tags: ['t1'] }
model.addTags({ _id: 'test' }, ['t2']).then(console.log);
// { _id: 'test', tags: ['t1', 't2'] }Returns Promise<object> updated target document
batchAdd
batch add element to collection
Parameters
queryobject mongoose query to find out update targetcollectionarray string collection will add to target documentupdateOptions
Examples
model.batchAddTags({ _id: { $in: ['id1', 'id2] } }, ['t1', 't2']).then(console.log);
// { "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 }
model.getTags({ _id: 'id1' }).then(console.log);
// ['t1', 't2']
model.batchAddTags({ _id: { $in: ['id1', 'id2] } }, ['t2', 't3']).then(console.log);
// { "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }
model.getTags({ _id: 'id2' }).then(console.log);
// ['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
queryobject mongoose query to find out update targetcollectionarray string collection will add to target documentupdateOptions
Examples
model.replaceTags({ _id: 'test' }, ['t1']).then(console.log);
// { _id: 'test', tags: ['t1'] }
model.replaceTags({ _id: 'test' }, ['t2', 't3']).then(console.log);
// { _id: 'test', tags: ['t2', 't3'] }Returns Promise<object> mongoose udpate result
batchReplace
batch update documents' collection filed by replace it with given collection
Parameters
queryobject mongoose query to find out update targetcollectionarray string collection will add to target documentupdateOptions
Examples
model.batchReplaceTags({ _id: 'test' }, ['t1']).then(console.log);
// { "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }
model.getTags({ _id: 'test' }).then(console.log);
// ['t1']
model.batchReplaceTags({ _id: 'test' }, ['t2', 't3']).then(console.log);
// { "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }
model.getTags({ _id: 'test' }).then(console.log);
// ['t2', 't3']