Insert a document (HTTP)
|
For details about additional languages, see: |
Inserts a single document into a collection.
Documents are stored in collections. They represent a single row or record of data in Astra DB Serverless databases. For more information, see About collections with the Data API (HTTP).
If the collection is vector-enabled, pregenerated vector embeddings can be included by using the reserved $vector field.
If the collection has vectorize enabled, vector embeddings can be automatically generated from text specified in the reserved $vectorize field.
You can later use the $vector or $vectorize field to perform a vector search or hybrid search.
If the collection has lexical enabled, use the reserved $lexical field to store a string to index for lexicographical matching and the lexical search component of hybrid search.
Alternatively, you can use the $hybrid shorthand to populate the $vectorize and $lexical fields.
|
Ready to write code? See the examples for this method to get started. |
Result
Inserts the specified document and returns a JSON object that includes the ID of the inserted document.
The ID value depends on the ID type. For more information, see Document IDs (HTTP).
Example response:
{
"status": {
"insertedIds": [
"3f557bef-fd53-47ea-957b-effd53c7eaec"
]
}
}
Signature
Use the insertOne command.
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"insertOne": {
"document": DOCUMENT_JSON_OBJECT
}
}'
Parameters
| Name | Type | Summary |
|---|---|---|
|
|
A JSON object describing the document to insert. A document can contain user-defined and reserved fields. User-defined field names can be any non-empty sequence of Unicode characters, with the following exceptions:
Reserved fields are tied to specific functionality. Include the following reserved fields in your documents, if applicable:
For examples, see Examples. |
Examples
The following examples demonstrate how to insert a document into a collection.
Insert a document
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"insertOne": {
"document": {
"title": "Hidden Shadows of the Past",
"genres": ["Biography", "Graphic Novel", "Dystopian", "Drama"],
"metadata": {
"isbn": "978-1-905585-40-3",
"language": "French",
"edition": "Anniversary Edition"
},
"number_of_pages": 245
}
}
}'
Insert a document with vector embeddings
Use the reserved $vector field to insert a document with pregenerated vector embeddings.
You can later use this field to perform a vector search.
All embeddings in the collection should use the same provider, model, and dimensions. Mismatched embeddings can cause inaccurate vector searches.
The $vector field is only supported for vector-enabled collections.
For more information, see Create a collection that can store vector embeddings and $vector in collections (HTTP).
You can provide the vector embeddings as an array of floats, or you can use $binary to provide the vector embeddings as a Base64-encoded string.
$binary can be more performant.
For more information about how to convert an array of floats to a Base64-encoded string, see Binary encoding of vector embeddings.
- Array of floats
-
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \ --header "Token: APPLICATION_TOKEN" \ --header "Content-Type: application/json" \ --data '{ "insertOne": { "document": { "name": "Jane Doe", "$vector": [0.08, -0.62, 0.39] } } }' - $binary
-
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \ --header "Token: APPLICATION_TOKEN" \ --header "Content-Type: application/json" \ --data '{ "insertOne": { "document": { "name": "Jane Doe", "$vector": {"$binary": "PaPXCr8euFI+x64U"} } } }'
Insert a document and generate vector embeddings
Use the reserved $vectorize field to generate a vector embedding automatically. The value of $vectorize can be any string.
You can later use this field to perform a vector search.
The $vectorize field is only supported for collections that have vectorize enabled.
For more information, see Create a collection that can automatically generate vector embeddings and $vectorize in collections (HTTP).
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"insertOne": {
"document": {
"name": "Jane Doe",
"$vectorize": "Text to vectorize"
}
}
}'
Insert a document for retrieval with hybrid search
If you plan to use hybrid search to find this document, the document must have both the $lexical field and the $vector field populated.
Example specifying the $vector and $lexical fields:
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"insertOne": {
"document": {
"name": "Jane Doe",
"$vector": [0.08, -0.62, 0.39],
"$lexical": "An athlete who loves biking, hiking, running, and swimming in the outdoors"
}
}
}'
Example specifying the $vectorize and $lexical fields:
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"insertOne": {
"document": {
"name": "Jane Doe",
"$vectorize": "An athlete who loves biking, hiking, running, and swimming in the outdoors",
"$lexical": "She shares her love of triathlons by coaching kids after school"
}
}
}'
Example using the $hybrid shorthand, which populates the $lexical and $vectorize field:
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"insertOne": {
"document": {
"name": "Jane Doe",
"$hybrid": "An athlete who loves biking, hiking, running, and swimming in the outdoors"
}
}
}'
Insert a document for retrieval with lexicographical matching
If you plan to use lexicographical matching to find this document, the document must have the $lexical field populated.
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"insertOne": {
"document": {
"name": "Jane Doe",
"$lexical": "An active hiker, runner, and triathlete who loves the outdoors."
}
}
}'
Insert a document and specify the ID
You can specify the _id field directly, or you can use the objectId, uuid, uuidv6, or uuidv7 types.
Example specifying an integer:
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"insertOne": {
"document": {
"name": "Jane Doe",
"_id": 1
}
}
}'
Example using the objectId type:
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"insertOne": {
"document": {
"name": "Jane Doe",
"_id": { "$objectId": "6672e1cbd7fabb4e5493916f" }
}
}
}'
Insert a document with a binary field
You can insert binary data as a Base64-encoded string with $binary.
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"insertOne": {
"document": {
"exampleBinary": {"$binary": "PaPXCr8euFI+x64U"}
}
}
}'
Insert a document with nested fields
Although you can use dot notation in a filter to find a document, you cannot use dot notation to insert a document. To specify nested fields in the inserted document, you must build a map, list, or set.
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"insertOne": {
"document": {
"title": "Hidden Shadows of the Past",
"genres": ["Biography", "Graphic Novel", "Dystopian", "Drama"],
"metadata": {
"isbn": "978-1-905585-40-3",
"language": "French",
"edition": "Anniversary Edition"
}
}
}
}'