mongoose-string-collection

1.4.0 • Public • Published

mongoose-string-collection

Greenkeeper badge

Travis npm npm npm David David

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 = 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;
// 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.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

  • schema MongooseSchema mongoose schema that use this plugin
  • options 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, if the input is greater than 0, will be treated as a valid input (optional, default -1)
    • options.elementOptions object? collection element options
    • options.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.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

  • query object mongoose query to find out one update target
  • collection array string collection will remove from target document
  • updateOptions

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

  • query object mongoose query to find out batch update target
  • collection array string collection will remove from batch target document
  • updateOptions

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

  • query object mongoose query to find out update target
  • collection array string collection will add to target document
  • updateOptions

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

  • query object mongoose query to find out update target
  • collection array string collection will add to target document
  • updateOptions

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

  • query object mongoose query to find out update target
  • collection array string collection will add to target document
  • updateOptions

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

  • query object mongoose query to find out update target
  • collection array string collection will add to target document
  • updateOptions

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']

Returns Promise<object> mongoose udpate result

Package Sidebar

Install

npm i mongoose-string-collection

Weekly Downloads

1

Version

1.4.0

License

MIT

Last publish

Collaborators

  • playding