Registry / database / mongodb-autoincrement

mongodb-autoincrement

JSON →
library1.0.1jsnpmunverified

Provides auto-incrementing integer IDs for MongoDB documents, compatible with both the native MongoDB driver and Mongoose. Version 1.0.1 (last updated 2015) is a stable, minimal plugin that relies on a separate 'counters' collection to track sequence values. It supports custom field names, steps, and global Mongoose schema plugin. Unlike MongoDB's built-in ObjectId, this plugin uses sequential integers; it does not support atomic increment guarantees across sharded clusters and cannot set initial values programmatically.

npm install mongodb-autoincrement
INSTALL
IMPORT
SIG · MONGODB-AUTOINCREM
M
mongodb-autoincrement
databasejavascriptv1.0.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

autoIncrement
const autoIncrement = require('mongodb-autoincrement');
import autoIncrement from 'mongodb-autoincrement';
The package does not support ESM; use CommonJS require().
getNextSequence
autoIncrement.getNextSequence(db, collectionName, callback)
const { getNextSequence } = require('mongodb-autoincrement'); getNextSequence(db, collectionName, callback);
getNextSequence is a method on the default export, not a named export.
mongoosePlugin
autoIncrement.mongoosePlugin
const { mongoosePlugin } = require('mongodb-autoincrement');
mongoosePlugin is an attached function, not a standalone named export.

Connects to MongoDB and inserts a document with an auto-incremented _id using the native driver.

const MongoClient = require('mongodb').MongoClient; const autoIncrement = require('mongodb-autoincrement'); const url = process.env.MONGO_URL || 'mongodb://localhost:27017/test'; MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true }, (err, client) => { if (err) throw err; const db = client.db('test'); const collectionName = 'users'; autoIncrement.getNextSequence(db, collectionName, (err, autoIndex) => { if (err) throw err; db.collection(collectionName).insertOne({ _id: autoIndex, name: 'John' }, (err, result) => { if (err) throw err; console.log('Inserted document with _id:', autoIndex); client.close(); }); }); });
Debug
Known issues
breakingThe package expects a 'counters' collection to exist; it will be created automatically but may fail if write permissions are insufficient.
fix
Ensure the database user has write access to create collections and documents in the 'counters' collection.
affects: *
gotchagetNextSequence does not use a transactional increment; concurrent calls may get the same sequence number on MongoDB versions without document-level atomicity.
fix
Use MongoDB's findOneAndUpdate with $inc for atomicity, or switch to a more robust solution.
affects: *
deprecatedThe package uses deprecated MongoDB driver callbacks; newer drivers prefer Promises or async/await.
fix
Wrap in a Promise: new Promise((resolve, reject) => autoIncrement.getNextSequence(...)).
affects: >=1.0.0
gotchaThe 'field' option in setDefaults is not properly documented; it may not work as expected with mongoosePlugin.
fix
Test thoroughly or avoid using setDefaults with field; set options directly in the plugin call.
affects: *
Errors
Common errors & fixes
Cannot find module 'mongodb-autoincrement'
Package not installed.
fix
npm install mongodb-autoincrement
TypeError: autoIncrement.getNextSequence is not a function
Importing the module incorrectly (e.g., named import).
fix
Use const autoIncrement = require('mongodb-autoincrement'); then call autoIncrement.getNextSequence(...).
MongoError: E11000 duplicate key error collection: test.counters index: _id_ dup key: { : "mycollection" }
Two concurrent insert attempts for the same counter document.
fix
Use findOneAndUpdate with upsert:true to atomically increment the counter, or ensure only one process initializes counters.
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies
mongooseoptionalPeer dependency required if using the mongoosePlugin; not needed with native mongodb driver only.
Agent activity
11 hits · last 30 days
node
8
Amazon
2
Resources
mongodb-autoincrement — npm install mongodb-autoincrement · libregistry