Registry / database / mongo-objectid

mongo-objectid

JSON →
library1.2.2jsnpmunverified

A MongoDB specification compliant ObjectID implementation for Node.js, version 1.2.2. Provides a robust alternative to native MongoDB drivers for ObjectID generation and validation. Designed to prevent duplicate IDs under high-frequency generation, a known issue with other implementations. Requires Node >= 12. No added dependencies. Features include customizable machine ID and timestamp generation.

npm install mongo-objectid
INSTALL
IMPORT
SIG · MONGO-OBJECTID
M
mongo-objectid
databasejavascriptv1.2.2
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.

ObjectId
const ObjectId = require('mongo-objectid');
import ObjectId from 'mongo-objectid';
The package does not support ESM; use CommonJS require.
default
const ObjectId = require('mongo-objectid');
const { ObjectId } = require('mongo-objectid');
The module exports a single constructor, not a named export.
ObjectId
const ObjectId = require('mongo-objectid').default;
import { default as ObjectId } from 'mongo-objectid';
Only CommonJS require is supported; ESM import may fail.

Demonstrates requiring the module, generating ObjectIds, validation, and custom machine ID.

const ObjectId = require('mongo-objectid'); // Generate a new ObjectId const id = new ObjectId(); console.log(id.toString()); // e.g., '5f3e1c9c8f1e2b3c4d5e6f7a' // Generate with specific timestamp (in milliseconds) const idWithTime = new ObjectId('2020-01-01T00:00:00Z'); console.log(idWithTime.toString()); // ObjectId based on that time // Validate an ObjectId string console.log(ObjectId.isValid('5f3e1c9c8f1e2b3c4d5e6f7a')); // true console.log(ObjectId.isValid('invalid-id')); // false // Get timestamp from ObjectId const timestamp = id.getTimestamp(); console.log(timestamp); // number (ms) // Set custom machine ID const customId = new ObjectId(); customId.setMachineId('123456'); // arbitrary 3-byte hex
Debug
Known issues
breakingPackage does not support ES modules (ESM). Using `import` will cause a runtime error.
fix
Use CommonJS `require` instead of `import`.
affects: >=1.0.0
deprecated`setMachineId()` method is available but has no effect in some versions; behavior is inconsistent.
fix
Avoid using setMachineId() or verify behavior with your version.
affects: >=1.0.0
gotchaConstructor accepts a hex string but does not validate it; passing an invalid string will produce a malformed ObjectId.
fix
Always validate hex strings using `ObjectId.isValid()` before constructing.
affects: >=1.0.0
gotchaThis package does not integrate with MongoDB drivers and cannot replace the `mongodb.ObjectId` class in database operations.
fix
Use native MongoDB driver's ObjectId for driver interactions.
affects: >=1.0.0
Errors
Common errors & fixes
ReferenceError: ObjectId is not defined
Forgot to import/require the module.
fix
Add `const ObjectId = require('mongo-objectid');` at the top.
TypeError: ObjectId is not a constructor
Using a destructured import like `const { ObjectId } = require('mongo-objectid');` which yields undefined.
fix
Use `const ObjectId = require('mongo-objectid');`.
SyntaxError: Cannot use import statement outside a module
Trying to use ES `import` syntax without 'type': 'module' in package.json.
fix
Switch to CommonJS require or add 'type': 'module' and use dynamic import (not recommended for this package).
Error: Cannot find module 'mongo-objectid'
Package not installed.
fix
Run `npm install mongo-objectid`.
Upgrade
Version history
1.2.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
mongo-objectid — npm install mongo-objectid · libregistry