Registry / database / di-short-mongo-id

di-short-mongo-id

JSON →
library1.0.1jsnpmunverified

Generates short, URL-friendly IDs from MongoDB ObjectIds by extracting timestamp and counter bits (77 bits total). The generated IDs are one-way (cannot be reversed to original ObjectId) but are reasonably unique. Version 1.0.1 is the only version; package is small and has no dependencies. Unlike other short ID libraries (e.g., nanoid, uuid), this is specifically designed for MongoDB ObjectIds and does not generate random IDs.

npm install di-short-mongo-id
INSTALL
IMPORT
SIG · DI-SHORT-MONGO-ID
D
di-short-mongo-id
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.

shortId
import shortId from 'di-short-mongo-id'
const { shortId } = require('di-short-mongo-id')
Default CommonJS export; named import will not work.
shortId
const shortId = require('di-short-mongo-id')
CommonJS require works as default export.
shortId (with uppercase option)
shortId('597b3afabca3cc7f2c331290', true)
Second boolean argument toggles uppercase output.

Shows how to generate lowercase and uppercase short IDs from MongoDB ObjectId strings.

import shortId from 'di-short-mongo-id'; // Generate a short ID from an ObjectId string const id = shortId('597b3ae1bca3cc7f2c33128e'); console.log(id); // "wrxtm4nb" // Generate uppercase version const upperId = shortId('597b3afabca3cc7f2c331290', true); console.log(upperId); // "87QUM4NB"
Debug
Known issues
gotchaThe function is one-way; you cannot recover the original ObjectId from the short ID. Missing machine ID, process ID, and most of the counter.
fix
Ensure you store the original ObjectId if you need to reverse the mapping.
affects: >=1.0.0
gotchaShort IDs are not guaranteed globally unique; collisions are possible if the same timestamp and counter bits are reused across different machines/processes.
fix
Consider combining with a unique prefix if generating IDs across distributed systems.
affects: >=1.0.0
deprecatedPackage has only one version and no recent updates. It may not be actively maintained.
fix
Check for alternatives or consider if this still meets your needs.
affects: 1.0.1
Errors
Common errors & fixes
shortId is not a function
Using named import instead of default import in CommonJS or ESM.
fix
Use `const shortId = require('di-short-mongo-id')` (CommonJS) or `import shortId from 'di-short-mongo-id'` (ESM).
TypeError: shortId(...) is not a function
Destructuring the default export incorrectly.
fix
Do not destructure; the entire module is a function. Use `const shortId = require('di-short-mongo-id')`.
Cannot find module 'di-short-mongo-id'
Package not installed or incorrect import path.
fix
Run `npm install di-short-mongo-id` in your project root.
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
7 hits · last 30 days
node
6
Resources
di-short-mongo-id — npm install di-short-mongo-id · libregistry