# mongodb-counter

> Auto-increment counters for MongoDB

Latest version **0.0.4** (published 2014-10-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install mongodb-counter
pnpm add mongodb-counter
yarn add mongodb-counter
bun add mongodb-counter
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.4 |
| Published | 2014-10-14 |
| First published | 2014-10-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Like FallingLeaves |
| Maintainers | likefallingleaves |
| Keywords | auto-increment, counters, counter, mongodb |

## Links

- npm: https://www.npmjs.com/package/mongodb-counter
- Repository: https://github.com/like-falling-leaves/mongodb-counter
- Issues: https://github.com/like-falling-leaves/mongodb-counter/issues
- npm.io page: https://npm.io/package/mongodb-counter

## Dependencies (1)

- [mongodb](https://npm.io/package/mongodb.md) >=1.2.0

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 0.0.4 (latest) — 2014-10-14
- 0.0.3 — 2014-10-04
- 0.0.2 — 2014-10-04
- 0.0.1 — 2014-10-04

## README

# mongodb-counter

This is a trivial mongodb auto-increment counter implementation based on [MongoDB Documentation](http://docs.mongodb.org/manual/tutorial/create-an-auto-incrementing-field/).

But this implementation requires Mongo 2.4 because it uses the $setOnInsert operation.  Unlike the sample in the documentation, this will allow creating new counters on the fly without initializing the counters to zero.

[![NPM info](https://nodei.co/npm/mongodb-counter.png?downloads=true)](https://npmjs.org/package/mongodb-counter)

[![Travis build status](https://api.travis-ci.org/Like-Falling-Leaves/mongodb-counter.png?branch=master)](
https://travis-ci.org/Like-Falling-Leaves/mongodb-counter)

## Install

    npm install mongodb-counter

## Initialization

```javascript

   // Initialization via mongo URLs of the form: mongodb://user:password@host:port/database
   var counters = require('mongodb-counter').createCounters({mongoUrl: 'mongodb://user:password@host:port/database'});

   // Specifying collection name instead of defaulting to counters collection
   var counters = require('mongodb-counter').createCounters({
     mongoUrl: 'mongodb://user:password@host:port/database',
     collectionName: 'yoyo'
   });

   // Initialization via a pre-created database collection to use
   var counters = require('mongodb-counter').createCounters({collection: db.counters});

```

## API

```javascript

   counters('requests').increment(); // increments by one, throws away the result
   counters('requests').increment(5); // increments by 5 throws away the result
   counters('requests').increment(5, function (err, currentCount) {
     // currentCount is the updated count.
   });

   // increment by one and get the new value
   counters('requests').increment(function (err, currentCount) {
     // currentCount is the updated count.
   });

   counters('requests').decrement(); // same options with decrement
   counters('requests').get(function (err, val) {
     // fetch the current value
   });
   counters('requests').set(52); // update the value.

   // if you only want to generate unique ids (for URL shortening for example),
   // a much faster method is getNextUniqueId.  It only hits the database every 100
   // calls.  Ofcourse, this has the side effect that the sequence is not strictly
   // monotonically increasing in order and there could be gaps when there are crashes
   // but uniqueness is guaranteed and the ordering will be almost monotonic.  This is
   // suitable for a lot of cases where you just want a small unique ID.

   function getShortUrl(longUrl, done) {
     counters('requests').getNextUniqueId(function (err, uniqueId) {
       if (err) return done(err);
       var shortUrl = '/' + uniqueId.toString(36);
       db.shortUrls.insert(
         {shortUrl: '/' + uniqueId.toString(36), longUrl: longUrl},
         function (err) { return done(err, !err && shortUrl); }
       );
     });
   });
```

---
_Source: https://npm.io/package/mongodb-counter · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
