Insert documents

Inserts multiple documents 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.

If the collection is vector-enabled, pregenerated vector embeddings can be included by using the reserved $vector field for each document. If the collection has vectorize enabled, vector embeddings can be automatically generated from text specified in the reserved $vectorize field for each document. 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 of space-separated keywords or terms to index for 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. If you are new to the Data API, check out the quickstart.

Result

  • Python

  • TypeScript

  • Java

  • curl

Inserts the specified documents and returns a CollectionInsertManyResult object that includes the IDs of the inserted documents and details about the success of the operation.

The ID value depends on the ID type. For more information, see Document IDs.

Example response:

CollectionInsertManyResult(inserted_ids=[
    "3f557bef-fd53-47ea-957b-effd53c7eaec",
    101,
    "132ffr343"
], raw_results=...)

Inserts the specified documents and returns a promise that resolves to a CollectionInsertManyResult<Schema> object that includes the IDs of the inserted documents and the number of inserted documents.

The ID value depends on the ID type. For more information, see Document IDs.

Example response:

{
  insertedCount: 3,
  insertedIds: [
    '92b3c4f4-db44-4440-b4c4-f4db54e440b8',
    101,
    '132ffr343',
  ]
}

Inserts the specified documents and returns a wrapper (CollectionInsertManyResult) that includes the IDs of the inserted documents.

The ID value depends on the ID type. For more information, see Document IDs.

Inserts the specified documents and returns a JSON object that includes the IDs of the inserted documents.

The ID value depends on the ID type. For more information, see Document IDs.

Example response:

{
  "status": {
    "insertedIds": [
      "3f557bef-fd53-47ea-957b-effd53c7eaec",
      101,
      "132ffr343"
    ]
  }
}

Parameters

  • Python

  • TypeScript

  • Java

  • curl

Use the insert_many method, which belongs to the astrapy.Collection class.

Method signature
insert_many(
  documents: Iterable[Dict[str, Any]],
  *,
  ordered: bool,
  chunk_size: int,
  concurrency: int
  general_method_timeout_ms: int,
  request_timeout_ms: int,
  timeout_ms: int,
) -> CollectionInsertManyResult
Name Type Summary

documents

Iterable[Dict[str, Any]]

An iterable of dictionaries, with each dictionary describing a document to insert.

A document can contain user-defined and reserved fields.

User-defined field names must follow these rules:

  • Must start and end with a letter or an underscore

  • Can contain letters, numbers, underscores, and hyphens

  • Must have a length of 1 to 100 characters

  • Cannot match the name of a reserved field

Reserved fields are tied to specific functionality. Include the following reserved fields in your documents, if applicable:

  • _id: An optional unique identifier for the document. If _id is omitted, it is created automatically based on the collection’s ID type. For more information, see Document IDs.

  • $vector: An optional array of numbers representing a vector embedding for vector search. The $vector field is only supported for vector-enabled collections. A document cannot contain both a $vector and a $vectorize field. For more information, see $vector in collections.

  • $vectorize: An optional string from which to generate vector embeddings for vector search. The $vectorize field is only supported for collections that have an embedding provider integration. A document cannot contain both a $vector and a $vectorize field. For more information, see $vectorize in collections.

  • $lexical: An optional string of space-separated keywords or terms to make the document searchable for the lexical search component of hybrid search. The $lexical field is only supported for collections that have lexical search enabled. For more information, see Create a collection.

  • $hybrid: An optional string that populates both $vectorize and $lexical. The $hybrid shorthand is only supported for collections that have vectorize and lexical search enabled. If a document uses $hybrid, it cannot contain a root-level $vectorize or $lexical field.

ordered

bool

Optional. Whether the insertions must be processed sequentially. If False, the documents may be inserted in an arbitrary order and possibly concurrently. If you don’t need ordered inserts, DataStax recommends setting this parameter to False for faster performance.

Default: False

chunk_size

int

Optional. The number of documents to include in a single API request. DataStax recommends leaving this parameter unspecified to use the system default.

Maximum: 100

Default: 50

concurrency

int

Optional. The maximum number of concurrent requests to the API at a given time.

If ordered is True, then concurrency must be 1 or unspecified.

Default: 20 if ordered is False. 1 if ordered is True.

general_method_timeout_ms

int

Optional. The maximum time, in milliseconds, that the whole operation, which may involve multiple HTTP requests, can take.

Default: The default value for the collection. This default is 30 seconds unless you specified a different default when you initialized the Collection or DataAPIClient object. For more information, see Timeout options.

This parameter is aliased as timeout_ms for convenience.

request_timeout_ms

int

Optional. The maximum time, in milliseconds, that the client should wait for each underlying HTTP request.

Default: The default value for the collection. This default is 10 seconds unless you specified a different default when you initialized the Collection object. For more information, see Timeout options.

Use the insertMany method, which belongs to the Collection class.

Method signature
async insertMany(
  documents: MaybeId<Schema>[],
  options?: {
    ordered?: boolean,
    concurrency?: number,
    chunkSize?: number,
    timeout?: number | TimeoutDescriptor,
  },
): CollectionInsertManyResult<Schema>
Name Type Summary

documents

MaybeId<Schema>[]

An array of documents to insert.

A document can contain user-defined and reserved fields.

User-defined field names must follow these rules:

  • Must start and end with a letter or an underscore

  • Can contain letters, numbers, underscores, and hyphens

  • Must have a length of 1 to 100 characters

  • Cannot match the name of a reserved field

Reserved fields are tied to specific functionality. Include the following reserved fields in your documents, if applicable:

  • _id: An optional unique identifier for the document. If _id is omitted, it is created automatically based on the collection’s ID type. For more information, see Document IDs.

  • $vector: An optional array of numbers representing a vector embedding for vector search. The $vector field is only supported for vector-enabled collections. A document cannot contain both a $vector and a $vectorize field. For more information, see $vector in collections.

  • $vectorize: An optional string from which to generate vector embeddings for vector search. The $vectorize field is only supported for collections that have an embedding provider integration. A document cannot contain both a $vector and a $vectorize field. For more information, see $vectorize in collections.

  • $lexical: An optional string of space-separated keywords or terms to make the document searchable for the lexical search component of hybrid search. The $lexical field is only supported for collections that have lexical search enabled. For more information, see Create a collection.

  • $hybrid: An optional string that populates both $vectorize and $lexical. The $hybrid shorthand is only supported for collections that have vectorize and lexical search enabled. If a document uses $hybrid, it cannot contain a root-level $vectorize or $lexical field.

options

CollectionInsertManyOptions

Optional. The options for this operation. See Properties of options for more details.

Properties of options
Name Type Summary

ordered

boolean

Optional. Whether the insertions must be processed sequentially. If False, the documents may be inserted in an arbitrary order and possibly concurrently. If you don’t need ordered inserts, DataStax recommends setting this parameter to False for faster performance.

concurrency

number

Optional. The maximum number of concurrent requests to the API at a given time.

If ordered is true, then concurrency must be 1 or unspecified.

Default: 8 if ordered is false. 1 if ordered is true.

chunkSize

number

Optional. The number of documents to include in a single API request. DataStax recommends leaving this parameter unspecified to use the system default.

Maximum: 100

Default: 50

timeout

number | TimeoutDescriptor

Optional.

The timeout(s) to apply to this method. You can specify requestTimeoutMs and generalMethodTimeoutMs.

Details about the timeout parameter

The TimeoutDescriptor object can contain these properties:

  • requestTimeoutMs (number): The maximum time, in milliseconds, that the client should wait for each underlying HTTP request. Default: The default value for the collection. This default is 10 seconds unless you specified a different default when you initialized the Collection or DataAPIClient object.

  • generalMethodTimeoutMs (number): The maximum time, in milliseconds, that the whole operation, which may involve multiple HTTP requests, can take. Since this method issues a single HTTP request, generalMethodTimeoutMs and requestTimeoutMs are equivalent. If you specify both, the minimum of the two will be used. Default: The default value for the collection. This default is 30 seconds unless you specified a different default when you initialized the Collection or DataAPIClient object.

If you specify a number instead of a TimeoutDescriptor object, that number will be applied to generalMethodTimeoutMs.

Use the insertMany method, which belongs to the com.datastax.astra.client.Collection class.

Method signature
CollectionInsertManyResult insertMany(
  List<? extends T> documents
)
CollectionInsertManyResult insertMany(
  List<? extends T> documents,
  CollectionInsertManyOptions options
)
Name Type Summary

documents

List<? extends T>

A list of objects describing the documents to insert.

A document can contain user-defined and reserved fields.

User-defined field names must follow these rules:

  • Must start and end with a letter or an underscore

  • Can contain letters, numbers, underscores, and hyphens

  • Must have a length of 1 to 100 characters

  • Cannot match the name of a reserved field

Reserved fields are tied to specific functionality. Include the following reserved fields in your documents, if applicable:

  • _id: An optional unique identifier for the document. If _id is omitted, it is created automatically based on the collection’s ID type. For more information, see Document IDs.

  • $vector: An optional array of numbers representing a vector embedding for vector search. The $vector field is only supported for vector-enabled collections. A document cannot contain both a $vector and a $vectorize field. For more information, see $vector in collections.

  • $vectorize: An optional string from which to generate vector embeddings for vector search. The $vectorize field is only supported for collections that have an embedding provider integration. A document cannot contain both a $vector and a $vectorize field. For more information, see $vectorize in collections.

  • $lexical: An optional string of space-separated keywords or terms to make the document searchable for the lexical search component of hybrid search. The $lexical field is only supported for collections that have lexical search enabled. For more information, see Create a collection.

  • $hybrid: An optional string that populates both $vectorize and $lexical. The $hybrid shorthand is only supported for collections that have vectorize and lexical search enabled. If a document uses $hybrid, it cannot contain a root-level $vectorize or $lexical field.

options

CollectionInsertManyOptions

Optional. The options for this operation. See Methods of the CollectionInsertManyOptions class for more details.

Methods of the CollectionInsertManyOptions class
Name Type Summary

ordered()

boolean

Optional. Whether the insertions must be processed sequentially. If false, the documents may be inserted in an arbitrary order and possibly concurrently. If you don’t need ordered inserts, DataStax recommends setting this parameter to false for faster performance.

concurrency()

int

Optional. The maximum number of concurrent requests to the API at a given time.

If ordered is true, then concurrency must be 1 or unspecified.

Default: 1.

chunkSize()

int

Optional. The number of documents to include in a single API request. DataStax recommends leaving this parameter unspecified to use the system default.

Maximum: 100

Default: 50

timeout

int

Optional. The maximum time, in milliseconds, that the client should wait for each underlying HTTP request.

Default: The default value for the collection. This default is 30 seconds unless you specified a different default when you initialized the Collection or DataAPIClient object.

Use the insertMany command.

Command signature
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
  "insertMany": {
    "documents": DOCUMENTS_JSON_ARRAY,
    "options": {
      "ordered": BOOLEAN,
    }
  }
}'
Name Type Summary

documents

array

An array of JSON objects describing the documents to insert.

A document can contain user-defined and reserved fields.

User-defined field names must follow these rules:

  • Must start and end with a letter or an underscore

  • Can contain letters, numbers, underscores, and hyphens

  • Must have a length of 1 to 100 characters

  • Cannot match the name of a reserved field

Reserved fields are tied to specific functionality. Include the following reserved fields in your documents, if applicable:

  • _id: An optional unique identifier for the document. If _id is omitted, it is created automatically based on the collection’s ID type. For more information, see Document IDs.

  • $vector: An optional array of numbers representing a vector embedding for vector search. The $vector field is only supported for vector-enabled collections. A document cannot contain both a $vector and a $vectorize field. For more information, see $vector in collections.

  • $vectorize: An optional string from which to generate vector embeddings for vector search. The $vectorize field is only supported for collections that have an embedding provider integration. A document cannot contain both a $vector and a $vectorize field. For more information, see $vectorize in collections.

  • $lexical: An optional string of space-separated keywords or terms to make the document searchable for the lexical search component of hybrid search. The $lexical field is only supported for collections that have lexical search enabled. For more information, see Create a collection.

  • $hybrid: An optional string that populates both $vectorize and $lexical. The $hybrid shorthand is only supported for collections that have vectorize and lexical search enabled. If a document uses $hybrid, it cannot contain a root-level $vectorize or $lexical field.

options

object

Optional. The options for this operation. See Properties of options for more details.

Properties of options
Name Type Summary

ordered

boolean

Optional. Whether the insertions must be processed sequentially. If false, the documents may be inserted in an arbitrary order and possibly concurrently. If you don’t need ordered inserts, DataStax recommends setting this parameter to false for faster performance.

Default: false

Examples

The following examples demonstrate how to insert multiple documents into a collection.

Insert documents

The documents can have different structures.

  • Python

  • TypeScript

  • Java

  • curl

from astrapy import DataAPIClient

# Get an existing collection
client = DataAPIClient()
database = client.get_database(
    "API_ENDPOINT",
    token="APPLICATION_TOKEN",
)
collection = database.get_collection("COLLECTION_NAME")

# Insert documents into the collection
result = collection.insert_many(
    [
        {
            "name": "Jane Doe",
            "age": 42,
        },
        {
            "nickname": "Bobby",
            "color": "blue",
            "foods": ["carrots", "chocolate"],
        },
    ]
)
import {
  DataAPIClient,
  CollectionInsertManyError,
} from "@datastax/astra-db-ts";

// Get an existing collection
const client = new DataAPIClient("APPLICATION_TOKEN");
const database = client.db("API_ENDPOINT");
const collection = database.collection("COLLECTION_NAME");

// Insert documents into the collection
(async function () {
  try {
    const result = await collection.insertMany([
      {
        name: "Jane Doe",
        age: 42,
      },
      {
        nickname: "Bobby",
        color: "blue",
        foods: ["carrots", "chocolate"],
      },
    ]);
  } catch (error) {
    if (error instanceof CollectionInsertManyError) {
      console.log(error.insertedIds());
    }
  }
})();
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.commands.results.CollectionInsertManyResult;
import com.datastax.astra.client.collections.definition.documents.Document;
import java.util.Arrays;
import java.util.List;

public class Example {

  public static void main(String[] args) {
    // Get an existing collection
    Collection<Document> collection =
        new DataAPIClient("APPLICATION_TOKEN")
            .getDatabase("API_ENDPOINT")
            .getCollection("COLLECTION_NAME");

    // Insert documents to the collection
    Document document1 = new Document().append("name", "Jane Doe").append("age", 42);
    Document document2 =
        new Document()
            .append("nickname", "Bobby")
            .append("color", "blue")
            .append("foods", Arrays.asList("carrots", "chocolate"));
    CollectionInsertManyResult result = collection.insertMany(List.of(document1, document2));
    System.out.println("IDs inserted: " + result.getInsertedIds());
  }
}
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
  "insertMany": {
    "documents": [
      {
        "name": "Jane Doe",
        "age": 42
      },
      {
        "nickname": "Bobby",
        "color": "blue",
        "foods": ["carrots", "chocolate"]
      }
    ]
  }
}'

Insert documents with vector embeddings

Use the reserved $vector field to insert documents with pregenerated vector embeddings.

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.

You may also insert a mix of documents with and without the $vector field.

  • Python

  • TypeScript

  • Java

  • curl

from astrapy import DataAPIClient
from astrapy.data_types import DataAPIVector


# Get an existing collection
client = DataAPIClient()
database = client.get_database(
    "API_ENDPOINT",
    token="APPLICATION_TOKEN",
)
collection = database.get_collection("COLLECTION_NAME")

# Insert documents to the collection
# The following also demonstrates use of both plain lists and DataAPIVector
result = collection.insert_many(
    [
        {"name": "Jane Doe", "age": 42, "$vector": [0.45, 0.32, 0.31]},
        {
            "nickname": "Bobby",
            "$vector": DataAPIVector([0.08, 0.68, 0.30]),
        },
    ]
)
import {
  DataAPIClient,
  CollectionInsertManyError,
} from "@datastax/astra-db-ts";

// Get an existing collection
const client = new DataAPIClient("APPLICATION_TOKEN");
const database = client.db("API_ENDPOINT");
const collection = database.collection("COLLECTION_NAME");

// Insert documents into the collection
(async function () {
  try {
    const result = await collection.insertMany([
      {
        name: "Jane Doe",
        age: 42,
        $vector: [0.45, 0.32, 0.31],
      },
      {
        nickname: "Bobby",
        $vector: [0.08, 0.68, 0.3],
      },
    ]);
  } catch (error) {
    if (error instanceof CollectionInsertManyError) {
      console.log(error.insertedIds());
    }
  }
})();
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.commands.results.CollectionInsertManyResult;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.core.vector.DataAPIVector;
import java.util.List;

public class Example {

  public static void main(String[] args) {
    // Get an existing collection
    Collection<Document> collection =
        new DataAPIClient("APPLICATION_TOKEN")
            .getDatabase("API_ENDPOINT")
            .getCollection("COLLECTION_NAME");

    // Insert documents to the collection
    Document document1 =
        new Document()
            .append("name", "Jane Doe")
            .append("age", 42)
            .append("$vector", new DataAPIVector(new float[] {0.45f, 0.32f, 0.41f}));
    Document document2 =
        new Document()
            .append("nickname", "Bobby")
            .append("$vector", new DataAPIVector(new float[] {0.08f, 0.68f, 0.3f}));
    CollectionInsertManyResult result = collection.insertMany(List.of(document1, document2));
    System.out.println("IDs inserted: " + result.getInsertedIds());
  }
}

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.

  • Array of floats

  • $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 '{
  "insertMany": {
    "documents": [
      {
        "name": "Jane Doe",
        "age": 42,
        "$vector": [.12, .52, .32]
      },
      {
        "nickname": "Bobby",
        "$vector": [0.3, 0.6, 0.5]
      }
    ]
  }
}'
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
  "insertMany": {
    "documents": [
      {
        "name": "Jane Doe",
        "age": 42,
        "$vector": {"$binary": "PfXCjz8FHrg+o9cK"}
      },
      {
        "nickname": "Bobby",
        "$vector": {"$binary": "PpmZmj8ZmZo/AAAA"}
      }
    ]
  }
}'

Insert documents and generate vector embeddings

Use the reserved $vectorize field to generate a vector embedding automatically. The value of $vectorize can be any string.

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.

You may also insert a mix of documents with and without the $vectorize field.

  • Python

  • TypeScript

  • Java

  • curl

from astrapy import DataAPIClient


# Get an existing collection
client = DataAPIClient()
database = client.get_database(
    "API_ENDPOINT",
    token="APPLICATION_TOKEN",
)
collection = database.get_collection("COLLECTION_NAME")

# Insert documents into the collection
result = collection.insert_many(
    [
        {
            "name": "Jane Doe",
            "age": 42,
            "$vectorize": "Text to vectorize for this document",
        },
        {
            "nickname": "Bobby",
            "$vectorize": "Text to vectorize for this document",
        },
    ]
)
import {
  DataAPIClient,
  CollectionInsertManyError,
} from "@datastax/astra-db-ts";

// Get an existing collection
const client = new DataAPIClient("APPLICATION_TOKEN");
const database = client.db("API_ENDPOINT");
const collection = database.collection("COLLECTION_NAME");

// Insert documents into the collection
(async function () {
  try {
    const result = await collection.insertMany([
      {
        name: "Jane Doe",
        age: 42,
        $vectorize: "Text to vectorize for this document",
      },
      {
        nickname: "Bobby",
        $vectorize: "Text to vectorize for this document",
      },
    ]);
  } catch (error) {
    if (error instanceof CollectionInsertManyError) {
      console.log(error.insertedIds());
    }
  }
})();
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.commands.results.CollectionInsertManyResult;
import com.datastax.astra.client.collections.definition.documents.Document;
import java.util.List;

public class Example {

  public static void main(String[] args) {
    // Get an existing collection
    Collection<Document> collection =
        new DataAPIClient("APPLICATION_TOKEN")
            .getDatabase("API_ENDPOINT")
            .getCollection("COLLECTION_NAME");

    // Insert documents into the collection
    Document document1 =
        new Document()
            .append("name", "Jane Doe")
            .append("age", 42)
            .append("$vectorize", "Text to vectorize for this document");
    Document document2 =
        new Document()
            .append("nickname", "Bobby")
            .append("$vectorize", "Text to vectorize for this document");
    CollectionInsertManyResult result = collection.insertMany(List.of(document1, document2));
    System.out.println("IDs inserted: " + result.getInsertedIds());
  }
}
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
  "insertMany": {
    "documents": [
      {
        "name": "Jane Doe",
        "age": 42,
        "$vectorize": "Text to vectorize for this document"
      },
      {
        "nickname": "Bobby",
        "$vectorize": "Text to vectorize for this document"
      }
    ]
  }
}'

Hybrid search, lexical search, and reranking are currently in public preview. Development is ongoing, and the features and functionality are subject to change. Astra DB Serverless, and the use of such, is subject to the DataStax Preview Terms.

If you plan to use hybrid search to find documents, each document must have both the $lexical field and the $vector field populated.

  • Python

  • TypeScript

  • Java

  • curl

from astrapy import DataAPIClient


# Get an existing collection
client = DataAPIClient("APPLICATION_TOKEN")
database = client.get_database("API_ENDPOINT")
collection = database.get_collection("COLLECTION_NAME")

# Insert documents
result = collection.insert_many(
    [
        {
            "name": "Jane Doe",
            "$vector": [0.08, 0.68, 0.30],
            "$lexical": "author scifi fantasy",
        },
        {
            "name": "Mary Day",
            "$vectorize": "An athlete who loves biking, hiking, running, and swimming in the outdoors",
            "$lexical": "hiker runner triathlete",
        },
        {
            "name": "Bobby",
            "$hybrid": "A software developer who enjoys managing databases",
        },
    ]
)
import {
  DataAPIClient,
  CollectionInsertManyError,
} from "@datastax/astra-db-ts";

// Get an existing collection
const client = new DataAPIClient("APPLICATION_TOKEN");
const database = client.db("API_ENDPOINT");
const collection = database.collection("COLLECTION_NAME");

// Insert documents into the collection
(async function () {
  try {
    const result = await collection.insertMany([
      {
        name: "Jane Doe",
        $vector: [0.08, 0.68, 0.3],
        $lexical: "author scifi fantasy",
      },
      {
        name: "Mary Day",
        $vectorize:
          "An athlete who loves biking, hiking, running, and swimming in the outdoors",
        $lexical: "hiker runner triathlete",
      },
      {
        name: "Bobby",
        $hybrid: "A software developer who enjoys managing databases",
      },
    ]);
  } catch (error) {
    if (error instanceof CollectionInsertManyError) {
      console.log(error.insertedIds());
    }
  }
})();
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.core.vector.DataAPIVector;

public class Example {

  public static void main(String[] args) {
    // Get an existing collection
    Collection<Document> collection =
        new DataAPIClient("APPLICATION_TOKEN")
            .getDatabase("API_ENDPOINT")
            .getCollection("COLLECTION_NAME");

    Document document1 =
        new Document()
            .append("name", "John Doe")
            .append("$vector", new DataAPIVector(new float[] {0.45f, 0.32f, 0.41f}))
            .append("$lexical", "author scifi fantasy");
    Document document2 =
        new Document()
            .append("name", "Mary Day")
            .append(
                "$vectorize",
                "An athlete who loves biking, hiking, running, and swimming in the outdoors")
            .append("$lexical", "hiker runner triathlete");
    Document document3 =
        new Document()
            .append("name", "'Bobby'")
            .append("$hybrid", "A software developer who enjoys managing databases");

    collection.insertMany(document1, document2, document3);
  }
}
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
  "insertMany": {
    "documents": [
      {
        "name": "Jane Doe",
        "$vector": [.08, .68, .30],
        "$lexical": "author scifi fantasy"
      },
      {
        "name": "Mary Day",
        "$vectorize": "An athlete who loves biking, hiking, running, and swimming in the outdoors",
        "$lexical": "hiker runner triathlete"
      },
      {
        "name": "Bobby",
        "$hybrid": "A software developer who enjoys managing databases"
      }
    ]
  }
}'

Insert documents and specify the IDs

  • Python

  • TypeScript

  • Java

  • curl

from astrapy import DataAPIClient


# Get an existing collection
client = DataAPIClient()
database = client.get_database(
    "API_ENDPOINT",
    token="APPLICATION_TOKEN",
)
collection = database.get_collection("COLLECTION_NAME")

# Insert documents into the collection
result = collection.insert_many(
    [
        {
            "name": "Jane Doe",
            "_id": 1,
        },
        {
            "nickname": "Bobby",
            "_id": "b_023",
        },
    ]
)

The TypeScript client provides the UUID and ObjectId classes to use and generate identifiers. These are not the same as those exported from the uuid or bson libraries.

To generate new identifiers, you can use UUID.v1(), UUID.v4(), UUID.v6(), UUID.v7(), or new ObjectId(), or you can use the uuid and oid shorthand methods. These methods accept a string representation of the IDs.

All UUID methods return an instance of the same class, which exposes a version property.

import {
  DataAPIClient,
  CollectionInsertManyError,
  UUID,
  ObjectId,
  uuid,
  oid,
} from "@datastax/astra-db-ts";

// Get an existing collection
const client = new DataAPIClient("APPLICATION_TOKEN");
const database = client.db("API_ENDPOINT");
const collection = database.collection("COLLECTION_NAME");

// Insert documents into the collection
(async function () {
  try {
    const result = await collection.insertMany([
      {
        name: "Melissa",
        _id: new ObjectId(),
      },
      {
        name: "Jess",
        _id: new ObjectId("65fd9b52d7fabba03349d013"),
      },
      {
        name: "Adam",
        _id: UUID.v4(),
      },
      {
        name: "Beth",
        _id: new UUID("016b1cac-14ce-660e-8974-026c927b9b91"),
      },
      {
        name: "Cathy",
        _id: uuid("bb3def0c-2ff2-43e1-b346-6cf0e5e36f10"),
      },
      {
        name: "Debra",
        _id: oid("67ea409a5e6499dabe0831bc"),
      },
      {
        name: "Jane",
        _id: 1,
      },
      {
        nickname: "Bobby",
        _id: "23",
      },
    ]);
  } catch (error) {
    if (error instanceof CollectionInsertManyError) {
      console.log(error.insertedIds());
    }
  }
})();

The Java client defines dedicated UUIDv6, UUIDv7, and ObjectId() classes. UUIDs from the Java UUID class are implemented in the UUID v4 standard. ObjectId classes are extracted from the BSON package.

When a unique identifier is retrieved from the server, it is converted to the appropriate class, based on the class definition in the defaultId option for the collection.

To generate new identifiers, you can use methods like new UUIDv6(), new UUIDv7(), or new ObjectId().

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.commands.results.CollectionInsertManyResult;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.collections.definition.documents.types.ObjectId;
import com.datastax.astra.client.collections.definition.documents.types.UUIDv7;
import java.util.List;
import java.util.UUID;

public class Example {

  public static void main(String[] args) {
    // Get an existing collection
    Collection<Document> collection =
        new DataAPIClient("APPLICATION_TOKEN")
            .getDatabase("API_ENDPOINT")
            .getCollection("COLLECTION_NAME");

    // Insert documents to the collection
    Document document1 =
        new Document()
            .append("_id", new ObjectId("6672e1cbd7fabb4e5493916f"))
            .append("name", "Melissa");
    Document document2 = new Document().append("_id", new UUIDv7()).append("name", "Jess");
    Document document3 = new Document().append("_id", UUID.randomUUID()).append("name", "Sam");
    Document document4 = new Document().append("_id", "1").append("name", "Jane");
    Document document5 = new Document().append("_id", "23").append("nickname", "Bobby");
    CollectionInsertManyResult result =
        collection.insertMany(List.of(document1, document2, document3, document4, document5));
    System.out.println("IDs inserted: " + result.getInsertedIds());
  }
}

You can specify the _id field directly, or you can use the objectId, uuid, uuidv6, or uuidv7 types.

curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
  "insertMany": {
    "documents": [
      {
        "nickname": "Melissa",
        "_id": { "$objectId": "6672e1cbd7fabb4e5493916f" }
      },
      {
        "nickname": "Jess",
        "_id": { "$uuid": "1ef2e42c-1fdb-6ad6-aae4-e84679831739" }
      },
      {
        "name": "Jane",
        "_id": 1
      },
      {
        "nickname": "Bobby",
        "_id": "23"
      }
    ]
  }
}'

Insert documents and specify insertion behavior

  • Python

  • TypeScript

  • Java

  • curl

from astrapy import DataAPIClient

# Get an existing collection
client = DataAPIClient()
database = client.get_database(
    "API_ENDPOINT",
    token="APPLICATION_TOKEN",
)
collection = database.get_collection("COLLECTION_NAME")

# Insert documents into the collection
result = collection.insert_many(
    [
        {
            "name": "Jane Doe",
            "age": 42,
        },
        {
            "nickname": "Bobby",
            "color": "blue",
            "foods": ["carrots", "chocolate"],
        },
    ],
    chunk_size=2,
    concurrency=2,
    ordered=False,
    general_method_timeout_ms=1000,
)
import {
  DataAPIClient,
  CollectionInsertManyError,
} from "@datastax/astra-db-ts";

// Get an existing collection
const client = new DataAPIClient("APPLICATION_TOKEN");
const database = client.db("API_ENDPOINT");
const collection = database.collection("COLLECTION_NAME");

// Insert documents into the collection
(async function () {
  try {
    const result = await collection.insertMany(
      [
        {
          name: "Jane Doe",
          age: 42,
        },
        {
          nickname: "Bobby",
          color: "blue",
          foods: ["carrots", "chocolate"],
        },
      ],
      {
        chunkSize: 2,
        concurrency: 2,
        ordered: false,
      },
    );
  } catch (error) {
    if (error instanceof CollectionInsertManyError) {
      console.log(error.insertedIds());
    }
  }
})();
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.commands.options.CollectionInsertManyOptions;
import com.datastax.astra.client.collections.commands.results.CollectionInsertManyResult;
import com.datastax.astra.client.collections.definition.documents.Document;
import java.util.Arrays;
import java.util.List;

public class Example {

  public static void main(String[] args) {
    // Get an existing collection
    Collection<Document> collection =
        new DataAPIClient("APPLICATION_TOKEN")
            .getDatabase("API_ENDPOINT")
            .getCollection("COLLECTION_NAME");

    // Define the insertion options
    CollectionInsertManyOptions options =
        new CollectionInsertManyOptions().chunkSize(20).concurrency(3).ordered(false).timeout(1000);

    // Insert documents into the collection
    Document document1 = new Document().append("name", "Jane Doe").append("age", 42);
    Document document2 =
        new Document()
            .append("nickname", "Bobby")
            .append("color", "blue")
            .append("foods", Arrays.asList("carrots", "chocolate"));
    CollectionInsertManyResult result =
        collection.insertMany(List.of(document1, document2), options);
    System.out.println("IDs inserted: " + result.getInsertedIds());
  }
}
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/COLLECTION_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
  "insertMany": {
    "documents": [
      {
        "name": "Jane Doe",
        "age": 42
      },
      {
        "nickname": "Bobby",
        "color": "blue",
        "foods": ["carrots", "chocolate"]
      }
    ],
    "options": {
      "ordered": false
    }
  }
}'

Client reference

  • Python

  • TypeScript

  • Java

  • curl

For more information, see the client reference.

For more information, see the client reference.

For more information, see the client reference.

Client reference documentation is not applicable for HTTP.

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2025 DataStax | Privacy policy | Terms of use | Manage Privacy Choices

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: +1 (650) 389-6000, info@datastax.com