Insert documents (Go)

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 (Go).

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 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. If you are new to the Data API, check out the quickstart.

Result

Inserts the specified documents and returns an *InsertManyResult struct that includes the IDs of the inserted documents, which you can access with the RawIDs() or DecodeIDs(v any) methods.

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

Parameters

Use the InsertMany method, which belongs to the Collection type.

Method signature
func (c *Collection) InsertMany(
  ctx context.Context,
  documents any,
  opts ...options.CollectionInsertManyOption
) (*results.InsertManyResult, error)
Name Type Summary

ctx

context.Context

The context for the operation.

documents

any

A slice of structs or maps, with each struct or map describing a 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:

  • Field names cannot start with $.

  • Field names cannot be exactly *.

  • If a field name includes & or ., you must escape those characters when you use the field in a filter, sort, projection, or update. For more information, see Work with . and & in field names (Go).

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 (Go).

  • $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 (Go).

  • $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 (Go).

  • $lexical: An optional string to make the document searchable for lexicographical matching and the lexical search component of hybrid search. The $lexical field is only supported for collections that have lexical search enabled. For more information, see $lexical in collections (Go).

  • $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. For more information, see $hybrid in collections (Go).

For examples, see Examples.

opts

…​options.CollectionInsertManyOption

Optional. A builder to generate options for this operation. See Methods of the CollectionInsertManyOption builder for more details.

Methods of the CollectionInsertManyOption builder
Method Summary

SetOrdered(v 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

SetChunkSize(v int)

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

For an example, see Insert documents and specify insertion behavior.

Maximum: 100

Default: 50

SetConcurrency(v 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.

For an example, see Insert documents and specify insertion behavior.

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

UpdateAPIOptions(v …​APIOption)

Optional. General API options for this operation, including the timeout.

Examples

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

Insert documents

The documents can have different structures.

package main

import (
	"context"
	"log"

	"github.com/datastax/astra-db-go/v2/astra"
	"github.com/datastax/astra-db-go/v2/astra/options"
)

func main() {
	ctx := context.Background()
	// Get an existing collection
	client := astra.NewClient()

	database := client.Database(
		"API_ENDPOINT",
		options.API().SetToken("APPLICATION_TOKEN"),
	)

	collection := database.Collection("COLLECTION_NAME")

	// Insert documents into the collection
	_, err := collection.InsertMany(
		ctx,
		[]map[string]any{
			{
				"name": "Jane Doe",
				"age":  42,
			},
			{
				"nickname": "Bobby",
				"color":    "blue",
				"foods":    []string{"carrots", "chocolate"},
			},
		},
	)
	if err != nil {
		log.Fatal(err)
	}
}

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 (Go).

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

package main

import (
	"context"
	"log"

	"github.com/datastax/astra-db-go/v2/astra"
	"github.com/datastax/astra-db-go/v2/astra/options"
)

func main() {
	ctx := context.Background()
	// Get an existing collection
	client := astra.NewClient()

	database := client.Database(
		"API_ENDPOINT",
		options.API().SetToken("APPLICATION_TOKEN"),
	)

	collection := database.Collection("COLLECTION_NAME")

	// Insert documents into the collection
	_, err := collection.InsertMany(
		ctx,
		[]map[string]any{
			{
				"name":    "Jane Doe",
				"age":     42,
				"$vector": []float32{0.08, -0.62, 0.39},
			},
			{
				"nickname": "Bobby",
				"$vector":  []float32{0.12, 0.53, 0.32},
			},
		},
	)
	if err != nil {
		log.Fatal(err)
	}
}

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 (Go).

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

package main

import (
	"context"
	"log"

	"github.com/datastax/astra-db-go/v2/astra"
	"github.com/datastax/astra-db-go/v2/astra/options"
)

func main() {
	ctx := context.Background()
	// Get an existing collection
	client := astra.NewClient()

	database := client.Database(
		"API_ENDPOINT",
		options.API().SetToken("APPLICATION_TOKEN"),
	)

	collection := database.Collection("COLLECTION_NAME")

	// Insert documents into the collection
	_, err := collection.InsertMany(
		ctx,
		[]map[string]any{
			{
				"name":       "Jane Doe",
				"age":        42,
				"$vectorize": "Text to vectorize for this document",
			},
			{
				"nickname":   "Bobby",
				"$vectorize": "Text to vectorize for this document",
			},
		},
	)
	if err != nil {
		log.Fatal(err)
	}
}

Hybrid 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.

package main

import (
	"context"
	"log"

	"github.com/datastax/astra-db-go/v2/astra"
	"github.com/datastax/astra-db-go/v2/astra/options"
)

func main() {
	ctx := context.Background()
	// Get an existing collection
	client := astra.NewClient()

	database := client.Database(
		"API_ENDPOINT",
		options.API().SetToken("APPLICATION_TOKEN"),
	)

	collection := database.Collection("COLLECTION_NAME")

	// Insert documents into the collection
	_, err := collection.InsertMany(
		ctx,
		[]map[string]any{
			{
				"name":     "Jane Doe",
				"$vector":  []float32{0.08, -0.62, 0.39},
				"$lexical": "An author who writes SciFi and fantasy novels.",
			},
			{
				"name":       "Mary Day",
				"$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.",
			},
			{
				"name":    "Bobby",
				"$hybrid": "A software developer who enjoys managing databases",
			},
		},
	)
	if err != nil {
		log.Fatal(err)
	}
}

Insert documents for retrieval with lexicographical matching

Lexicographical matching is 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 lexicographical matching to find documents, each document must have the $lexical field populated.

package main

import (
	"context"
	"log"

	"github.com/datastax/astra-db-go/v2/astra"
	"github.com/datastax/astra-db-go/v2/astra/options"
)

func main() {
	ctx := context.Background()
	// Get an existing collection
	client := astra.NewClient()

	database := client.Database(
		"API_ENDPOINT",
		options.API().SetToken("APPLICATION_TOKEN"),
	)

	collection := database.Collection("COLLECTION_NAME")

	// Insert documents into the collection
	_, err := collection.InsertMany(
		ctx,
		[]map[string]any{
			{
				"name":     "Jane Doe",
				"$lexical": "An author who writes SciFi and fantasy novels.",
			},
			{
				"name":     "Mary Day",
				"$lexical": "An active hiker, runner, and triathlete who loves the outdoors.",
			},
		},
	)
	if err != nil {
		log.Fatal(err)
	}
}

Insert documents and specify the IDs

The Go client provides the func NewUUID(), func NewUUIDv4(), func NewUUIDv1(), func NewUUIDv1At(t time.Time), func NewUUIDv6(), func NewUUIDv6At(t time.Time), func NewUUIDv7(), func NewUUIDv7At(t time.Time), functions to generate identifiers. It also provides the datatypes.ParseUUID(s string) and datatypes.ParseObjectId(s string) functions to generate identifiers from a string representation

package main

import (
	"context"
	"log"

	"github.com/datastax/astra-db-go/v2/astra"
	"github.com/datastax/astra-db-go/v2/astra/datatypes"
	"github.com/datastax/astra-db-go/v2/astra/options"
)

func main() {
	ctx := context.Background()
	// Get an existing collection
	client := astra.NewClient()

	database := client.Database(
		"API_ENDPOINT",
		options.API().SetToken("APPLICATION_TOKEN"),
	)

	collection := database.Collection("COLLECTION_NAME")

	// Insert documents into the collection
	uuid := datatypes.NewUUIDv7()

	objId, err := datatypes.ParseObjectId("6672e1cbd7fabb4e5493916f")
	if err != nil {
		log.Fatal(err)
	}

	_, err = collection.InsertMany(
		ctx,
		[]map[string]any{
			{
				"name": "Melissa",
				"_id":  objId,
			},
			{
				"name": "Jess",
				"_id":  uuid,
			},
			{
				"name": "Jane",
				"_id":  1,
			},
			{
				"name": "Bobby",
				"_id":  "b_023",
			},
		},
	)
	if err != nil {
		log.Fatal(err)
	}
}

Insert documents and specify insertion behavior

package main

import (
	"context"
	"log"
	"time"

	"github.com/datastax/astra-db-go/v2/astra"
	"github.com/datastax/astra-db-go/v2/astra/options"
)

func main() {
	ctx := context.Background()
	// Get an existing collection
	client := astra.NewClient()

	database := client.Database(
		"API_ENDPOINT",
		options.API().SetToken("APPLICATION_TOKEN"),
	)

	collection := database.Collection("COLLECTION_NAME")

	// Insert documents into the collection
	_, err := collection.InsertMany(
		ctx,
		[]map[string]any{
			{
				"name": "Jane Doe",
				"age":  42,
			},
			{
				"nickname": "Bobby",
				"color":    "blue",
				"foods":    []string{"carrots", "chocolate"},
			},
		},
		options.CollectionInsertMany().
			SetChunkSize(2).
			SetConcurrency(2).
			SetOrdered(false).
			UpdateAPIOptions(options.API().SetRequestTimeout(3*time.Second)),
	)
	if err != nil {
		log.Fatal(err)
	}
}

Insert documents with a binary field

You can insert binary data as a []byte slice or a Base64-encoded string with $binary. []byte slices are automatically Base64-encoded.

package main

import (
	"context"
	"log"

	"github.com/datastax/astra-db-go/v2/astra"
	"github.com/datastax/astra-db-go/v2/astra/options"
)

func main() {
	ctx := context.Background()
	// Get an existing collection
	client := astra.NewClient()

	database := client.Database(
		"API_ENDPOINT",
		options.API().SetToken("APPLICATION_TOKEN"),
	)

	collection := database.Collection("COLLECTION_NAME")

	// Insert documents with binary fields
	_, err := collection.InsertMany(
		ctx,
		[]map[string]any{
			{
				// Using $binary with a base64-encoded string
				"exampleBinary": map[string]any{
					"$binary": "PfvnbT7peNU/Sfvn",
				},
				// No need for explicit '$binary' with a byte array
				"anotherExampleBinary": []byte{
					0x3d,
					0xfb,
					0xe7,
					0x6d,
					0x3e,
					0xe9,
					0x78,
					0xd5,
					0x3f,
					0x49,
					0xfb,
					0xe7,
				},
			},
		},
	)
	if err != nil {
		log.Fatal(err)
	}
}

Insert documents 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.

package main

import (
	"context"
	"log"

	"github.com/datastax/astra-db-go/v2/astra"
	"github.com/datastax/astra-db-go/v2/astra/options"
)

func main() {
	ctx := context.Background()
	// Get an existing collection
	client := astra.NewClient()

	database := client.Database(
		"API_ENDPOINT",
		options.API().SetToken("APPLICATION_TOKEN"),
	)

	collection := database.Collection("COLLECTION_NAME")

	// Insert documents into the collection
	_, err := collection.InsertMany(
		ctx,
		[]map[string]any{
			{
				"title": "Hidden Shadows of the Past",
				"genres": []string{
					"Biography",
					"Graphic Novel",
					"Dystopian",
					"Drama",
				},
				"metadata": map[string]any{
					"isbn":     "978-1-905585-40-3",
					"language": "French",
					"edition":  "Anniversary Edition",
				},
			},
			{
				"title": "Bake a Dozen",
				"genres": []string{
					"Biography",
					"Fiction",
				},
				"metadata": map[string]any{
					"isbn":     "342-2-875587-50-2",
					"language": "English",
					"edition":  "Illustrated Edition",
				},
			},
		},
	)
	if err != nil {
		log.Fatal(err)
	}
}

Client reference

For more information, see the client reference.

Was this helpful?

Give Feedback

How can we improve the documentation?

© Copyright IBM Corporation 2026 | 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: Contact IBM