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-autoincrementNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Connects to MongoDB and inserts a document with an auto-incremented _id using the native driver.
Ensure the database user has write access to create collections and documents in the 'counters' collection.
Use MongoDB's findOneAndUpdate with $inc for atomicity, or switch to a more robust solution.
Wrap in a Promise: new Promise((resolve, reject) => autoIncrement.getNextSequence(...)).
Test thoroughly or avoid using setDefaults with field; set options directly in the plugin call.
npm install mongodb-autoincrement
Use const autoIncrement = require('mongodb-autoincrement'); then call autoIncrement.getNextSequence(...).Use findOneAndUpdate with upsert:true to atomically increment the counter, or ensure only one process initializes counters.