A mongoose plugin that can help you quickly develop string collection related requirements
MIT License
A mongoose plugin that can help you quickly develop string collection related requirements
Installation
npm i -S mongoose-string-collection
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']
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']
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;
// true
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]
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, default tags
)options.isIndex
boolean whether index in target field (optional, default false
)options.isUnique
boolean whether unique the content in the collection (optional, default true
)options.maxLength
number The maximum size limit for the collection,-1
)options.elementOptions
Object? collection element optionsoptions.updateOptions
Object? collection default update optionssugar method that get target filed as single result
Parameters
query
Object 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 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.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
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.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 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.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
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.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
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.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
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.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']