Document IDs
_id
field
Documents in a collection are always identified by an ID that is unique within the collection.
This identifier is stored in the reserved field _id
.
Default document IDs
When you create a collection, you can specify the default ID type for documents in the collection. The Data API supports Object IDs, version 4 UUIDs, version 6 UUIDs, and version 7 UUIDs. If you don’t specify the default ID type, the default type is a string form of a version 4 UUID.
If you don’t explicitly set the _id
field when you insert a document into the collection, the Data API will automatically generate the _id
field based on the default ID type for the collection.
For more information about setting the default ID type, see Create a collection.
Specifying document IDs
DataStax recommends using the automatically generated document ID instead of specifying the ID.
This ensures uniqueness across the database and reduces the complexity of your code.
However, you can use the reserved _id
field to specify a document ID when you insert a document.
For examples, see Insert documents and specify the IDs.
Other document identifiers
Regardless of the collection’s default ID type, you can use document identifiers of any type outside of the reserved _id
field.
The Data API does not force uniqueness across identifiers outside of the _id
field.