npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

mongoose-string-collection

v1.4.0

Published

A mongoose plugin that can help you quickly develop string collection related requirements

Readme

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