uuid.lol

ObjectID Generator

Press c to copy or r for a new one

Generate in bulk

What is an ObjectID?

An ObjectID is the 12-byte value MongoDB puts in the _id field of any document that arrives without one. It is written as 24 hexadecimal characters.

Those 12 bytes are four bytes of Unix seconds, then five random bytes that stay fixed for the life of the process, then a three-byte counter that steps once per ID.

The driver makes it, not the server

This is the part people find surprising. When you insert a document with no _id, the MongoDB driver on your machine mints the ObjectID before the write goes anywhere. The server never generates one.

So an inserted document already knows its own ID without a round trip, and the timestamp inside it is your clock rather than the database's. A machine with a skewed clock writes skewed IDs.

How well it sorts

Roughly, and only down to the second. Inside a single process the counter breaks ties, so that process's own IDs come out in order.

Across processes there is no defined order within a second at all, because the bytes that decide it are the random per-process field. If you need ordering you can rely on, reach for UUID v7 and its millisecond clock.

An ObjectID is not a secret

Only five of the twelve bytes are random, and those five hold still for a whole process. The timestamp is public and the counter walks upward one step at a time, so holding one ObjectID makes the ones around it easy to guess.

That is fine for a primary key. It is not fine for a URL slug guarding something private. Paste one into the decoder to see every field come back out.

About the generator above

It mints IDs the way a driver would, counter and all, so pressing the button repeatedly gives you a run of IDs that really do sort. The per-process random field here is per browser tab. Reload the page and you get a new one.

Generate one in code

TypeScript

import { ObjectId } from "mongodb";

new ObjectId().toHexString();

Python

from bson import ObjectId  # pip install pymongo

str(ObjectId())

Go

import "go.mongodb.org/mongo-driver/bson/primitive"

primitive.NewObjectID().Hex()

Rust

// cargo add bson
use bson::oid::ObjectId;

ObjectId::new().to_hex();