MongoDB Atlas is a fully managed MongoDB service with global multi-region clusters, Atlas Search, and real-time triggers. UUID v7 as a custom _id provides RFC-standard identifiers with millisecond ordering — a drop-in improvement over ObjectId for systems that need UUID interoperability. See UUID in MongoDB for the full guide.
Insert with UUID v7 _id (Node.js driver)
import { MongoClient } from 'mongodb';
import { v7 as uuidv7 } from 'uuid';
const client = new MongoClient(process.env.MONGODB_URI!);
const db = client.db('shop');
interface Order {
_id: string;
customerId: string;
total: number;
status: string;
createdAt: Date;
}
async function createOrder(customerId: string, total: number): Promise<Order> {
const order: Order = {
_id: uuidv7(), // UUID v7 — millisecond-ordered, RFC 9562
customerId,
total,
status: 'pending',
createdAt: new Date(),
};
await db.collection<Order>('orders').insertOne(order);
return order;
}
Cursor pagination on _id
// UUID v7 lexicographic order = insertion/creation order
async function listOrders(cursor?: string, limit = 20) {
const filter = cursor ? { _id: { $gt: cursor } } : {};
const items = await db.collection<Order>('orders')
.find(filter)
.sort({ _id: 1 })
.limit(limit)
.toArray();
const nextCursor = items.length === limit ? items.at(-1)?._id : undefined;
return { items, nextCursor };
}
Atlas Search range query by time
// Find all orders created in the last hour using UUID v7 range
function uuidV7FromDate(date: Date): string {
const ms = BigInt(date.getTime());
const hex = ms.toString(16).padStart(12, '0');
// Build minimal UUID v7 at this timestamp
return `${hex.slice(0,8)}-${hex.slice(8,12)}-7000-8000-000000000000`;
}
async function ordersInLastHour() {
const since = uuidV7FromDate(new Date(Date.now() - 3_600_000));
return db.collection<Order>('orders')
.find({ _id: { $gt: since } })
.sort({ _id: 1 })
.toArray();
}
UUID v7 vs ObjectId comparison
| Property | UUID v7 | ObjectId |
|---|---|---|
| Size | 36 bytes (string) / 16 bytes (binary) | 12 bytes |
| Time precision | Millisecond (48-bit) | Second (32-bit) |
| Randomness | 74 bits | 40 bits (process + counter) |
| Standard | RFC 9562 | MongoDB proprietary |
| Cross-service | Any UUID-aware system | MongoDB-specific |
Further reading
External references
Frequently asked questions
Can I use UUID v7 as _id in MongoDB Atlas?
Yes. Pass _id: uuidv7() in your insert document. MongoDB accepts any BSON type as _id — strings and Binary subtypes both work. UUID v7 strings sort lexicographically in the same order as creation time, enabling range queries and cursor pagination on _id without a separate createdAt field. See UUID in MongoDB.
Does UUID v7 replace ObjectId in MongoDB?
UUID v7 and ObjectId serve the same role — unique, time-prefixed document identifiers. ObjectId uses second-level timestamps (4 bytes) plus a process counter. UUID v7 uses millisecond timestamps (48 bits) plus 74 random bits. UUID v7 is more portable across services that expect standard UUID strings and provides finer time granularity. See UUID v4 vs UUID v7.
How does UUID v7 help MongoDB Atlas global clusters?
Atlas global clusters replicate across regions. Auto-increment IDs require a centralized sequence — impossible across regions. ObjectIds include a machine/process component that can theoretically collide with high cardinality. UUID v7 has 74 random bits — collision probability per millisecond is negligible across any number of writers. See UUID v7 in Distributed Systems.