npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

oid-utils

v0.2.1

Published

Simple utilities for working with MongoDB ObjectIds

Downloads

23

Readme

oid-utils

Simple utilities for working with MongoDB ObjectIds

oid-utils is designed for development convenience and to prevent app crashes from unexpected exceptions. Most functions return safe fallback values or false instead of throwing errors. For example, isAfter returns false when comparing invalid data instead of throwing an exception.

This means you can safely use these functions without try-catch blocks, but be aware that silent failures may occur. Always check the return values and handle fallback cases appropriately in your application logic.

Installation

This package requires bson as a peer dependency. You need to install it separately:

npm install oid-utils bson

BSON Version Compatibility

This package is compatible with bson version 4.0.0 and above. Here's the compatibility matrix with MongoDB Node.js Driver:

| MongoDB Driver | bson 4.x | bson 5.x | bson 6.x | |----------------|-----------|-----------|-----------| | [email protected] | ❌ N/A | ❌ N/A | ✅ ✓ | | [email protected] | ❌ N/A | ✅ ✓ | ❌ N/A | | [email protected] | ✅ ✓ | ❌ N/A | ❌ N/A | | [email protected] | ❌ N/A | ❌ N/A | ❌ N/A |

Note: For detailed BSON documentation and API reference, visit the official BSON documentation.

Usage

const ou = require('oid-utils');

// Create new ObjectId (no args)
const oid = ou.newOid();

// Create ObjectId from value (safe by default)
const oidFromString = ou.newOid('507f1f77bcf86cd799439011');

// Fallback mode (default): invalid or falsy(undefined|null|'') returns fallback
const maybeOid = ou.newOid('invalid', 'fallback-value'); // => 'fallback-value'
const maybeOid2 = ou.newOid(undefined); // => null (default fallback)

// Check if a value can be converted to ObjectId
const can = ou.canBeOid('507f1f77bcf86cd799439011');

// Check if a value is an ObjectId instance
const isOid = ou.isOid(oid);

// Check if two ObjectIds have the same value
const isSame = ou.isSameOid(oid1, oid2);

// Convert ObjectId to Date / string
const date = ou.toDate(oid, 'fallback-date'); // with fallback
const str = ou.toStr(oid); // template interpolation

// Compare (returns false on invalid inputs)
const isAfter = ou.isAfter(oid, ou.newOid());
const isBefore = ou.isBefore(oid, ou.newOid());

// Get next/previous ObjectId (increment/decrement counter or timestamp)
const next = ou.nextOid(oid, 'fallback'); // next ObjectId
const prev = ou.prevOid(oid, 'fallback'); // previous ObjectId

// Range queries: get ObjectId for date range
const startDate = new Date('2020-01-01');
const startOid = ou.startOid(startDate); // minimum ObjectId for date (counter=0)
const endOid = ou.endOid(startDate); // next timestamp ObjectId (counter=0, for $lt queries)

// MongoDB range query example
// db.collection.find({ _id: { $gte: startOid, $lt: endOid } });

// Batch create (validOnly=true by default)
const many = ou.newOids(['507f1f77bcf86cd799439011', 'invalid']); // => [ObjectId] (skips invalid)
const manyStrict = ou.newOids(['507f1f77bcf86cd799439011', 'invalid'], false); // throws

// Unique ObjectIds (filters non-ObjectIds, preserves first occurrence order)
const unique = ou.uniqueOids([ou.newOid('507f1f77bcf86cd799439011'), ou.newOid('507f1f77bcf86cd799439011')]);

API Reference

| Function | Parameters | Returns | Description | |----------|------------|---------|-------------| | newOid | [value], [fallback=null] | ObjectId\|any | Create ObjectId; returns fallback for invalid/falsy inputs | | newOids | values=[], validOnly=true | ObjectId[] | Create multiple ObjectIds; skip invalid by default; throws when validOnly=false | | uniqueOids | oids=[] | ObjectId[] | Remove duplicate ObjectIds; filters out non-ObjectId values | | canBeOid | value | boolean | Check if a value can be converted to ObjectId | | isOid | value | boolean | Check if a value is an ObjectId instance | | isSameOid | value1, value2 | boolean | Check if two ObjectIds have the same value; returns false for non-ObjectIds | | toDate | value, [fallback=null] | Date\|any | Convert ObjectId to Date; returns fallback for non-ObjectId | | toStr | value | string | Convert any value to string using template interpolation | | isAfter | value1, value2 | boolean | Check if first value is after second; returns false on invalid inputs | | isBefore | value1, value2 | boolean | Check if first value is before second; returns false on invalid inputs | | nextOid | value, [fallback=null] | ObjectId\|any | Get next ObjectId (increment counter or timestamp); returns fallback for non-ObjectId | | prevOid | value, [fallback=null] | ObjectId\|any | Get previous ObjectId (decrement counter or timestamp); returns fallback for non-ObjectId | | startOid | date, [fallback=null] | ObjectId\|any | Get minimum ObjectId for date (counter=0); returns fallback for non-Date or Invalid Date | | endOid | date, [fallback=null] | ObjectId\|any | Get ObjectId for range query end (timestamp+1s, counter=0); returns fallback for non-Date or Invalid Date |

Examples

See the examples folder for more detailed usage examples.