Registry / database / mongodb-short-id

mongodb-short-id

JSON →
library0.3.3jsnpmunverified

Convert MongoDB ObjectIDs to shorter URL-friendly strings and reverse them back. Version 0.3.3 is the latest stable release, last updated in 2014. It uses base64 encoding to produce compact IDs. Differentiators: simple API, supports both string and ObjectID conversions. However, it is an unmaintained package with no updates since its release, and it depends on the deprecated MongoDB native driver's ObjectID. Consider using uuid-based approaches or modern MongoDB drivers for active support.

npm install mongodb-short-id
INSTALL
IMPORT
SIG · MONGODB-SHORT-ID
M
mongodb-short-id
databasejavascriptv0.3.3
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.

default
const shortID = require('mongodb-short-id');
import shortID from 'mongodb-short-id';
Package uses CommonJS; no ESM exports. Use require() or wrap with createRequire.
longToShort
shortID.longToShort('507f1f77bcf86cd799439011');
shortID.longToShort(new ObjectID('...'));
Expects a hex string (24 chars), not an ObjectID instance.
shortToObjectID
shortID.shortToObjectID('UH8fd7z4bNeZQ5AR');
shortID.shortToObjectID(shortID);
Returns a MongoDB ObjectID instance (requires mongodb package).

Shows basic conversion: long hex string to short URL-safe string and back, plus ObjectID conversion.

const shortID = require('mongodb-short-id'); const longId = '507f1f77bcf86cd799439011'; const short = shortID.longToShort(longId); console.log(short); // => 'UH8fd7z4bNeZQ5AR' const back = shortID.shortToLong(short); console.log(back); // => '507f1f77bcf86cd799439011' const obj = shortID.shortToObjectID(short); console.log(obj instanceof require('mongodb').ObjectID); // true
Debug
Known issues
deprecatedPackage has not been updated since 2014 and depends on an outdated mongodb driver version.
fix
Consider using a modern alternative like generating UUIDs or using nanoid for short IDs.
affects: >=0
gotchalongToShort expects a 24-character hex string; passing an ObjectID instance will cause errors.
fix
Convert ObjectID to hex string first: objectID.toHexString().
affects: >=0
Errors
Common errors & fixes
TypeError: shortID.longToShort is not a function
Using ESM import syntax on a CommonJS module without default export.
fix
Use `const shortID = require('mongodb-short-id');` or use dynamic import with createRequire.
Error: Cannot find module 'mongodb'
The package requires 'mongodb' as a peer dependency but it's not installed.
fix
Run `npm install mongodb` to install the core MongoDB driver.
Upgrade
Version history
0.3.3latest on npm
Audit
Dependencies
mongodbrequiredUses ObjectID from the core MongoDB driver
Agent activity
2 hits · last 30 days
node
2
Resources
mongodb-short-id — npm install mongodb-short-id · libregistry