The type of the default ID that the API should generate if no ID is provided in the inserted document.
If not specified, the default ID will be a string UUID.
Type | Description | Example |
---|---|---|
uuid |
A UUID v4. | new UUID('f47ac10b-58cc-4372-a567-0e02b2c3d479') |
uuidv6 |
A UUID v6. | new UUID('6f752f1a-6b6d-4f3e-8e1e-2e167e3b5f3d') |
uuidv7 |
A UUID v7. | new UUID('018e75ff-a07b-7b08-bb91-aa566c5abaa6') |
objectId |
An ObjectID. | new ObjectId('507f1f77bcf86cd799439011') |
default | A string UUID. | 'f47ac10b-58cc-4372-a567-0e02b2c3d479' |
const collection = await db.createCollection('my-collection');
// { name: 'Jessica', _id: 'f47ac10b-58cc-4372-a567-0e02b2c3d479' }
await collection.insertOne({ name: 'Jessica' });
const collection = await db.createCollection('my-collection', {
defaultId: { type: 'uuidv6' },
});
// { name: 'Allman', _id: UUID('6f752f1a-6b6d-6f3e-8e1e-2e167e3b5f3d') }
await collection.insertOne({ name: 'Allman' });
const collection = await db.createCollection('my-collection', {
defaultId: { type: 'objectId' },
});
// { name: 'Brothers', _id: ObjectId('507f1f77bcf86cd799439011') }
await collection.insertOne({ name: 'Brothers' });
Make sure you're keeping this all in mind if you're specifically typing your _id field.
Represents the options for the default ID.
If
type
is not specified, the default ID will be a string UUID.Field
type - The type of the default ID.