Create a collection (Go)

Creates a new collection in a database.

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

Creates a collection with the specified parameters.

Returns a Collection object. You can use this object to work with documents in the collection.

Parameters

You cannot edit a collection’s definition after you create the collection.

Use the CreateCollection method, which belongs to the Db type.

Method signature
func (d *Db) CreateCollection(
  ctx context.Context,
  name string,
  opts ...options.CreateCollectionOption
) (*Collection, error)
Name Type Summary

ctx

context.Context

The context for the operation.

name

string

The name of the new collection.

Collection names must follow these rules:

  • Can contain letters, numbers, and underscores

  • Cannot exceed 48 characters

  • Must be unique within the keyspace

opts

…​options.CreateCollectionOption

Optional. The options for this method.

Methods of the CreateCollectionOption builder
Method Summary

UpdateVector(v …​VectorOption)

Optional. The vector configuration for the collection. This includes things like the vector dimension, similarity metric, and source model.

Required for vector search.

The VectorOptions builder has the following methods:

  • SetDimension(v int): The dimension for vector embeddings in the collection. This should match the dimension of the vector that your embedding model produces. Optional if you specify a model that has a default dimension value.

  • SetMetric(v string): Optional. The similarity metric to use for vector search. Can be one of: options.MetricCosine, options.MetricDotProduct, options.MetricEuclidean.

  • SetSourceModel(v string): Optional. The model used to generate the vector embeddings. This enables certain vector optimizations on the index. Can be one of: ada002, bert, cohere-v3, gecko, nv-qa-4, openai-v3-large, openai-v3-small, other.

UpdateLexical(v …​LexicalOption)

Optional. The lexical search configuration for the collection.

Only collections in databases in the AWS us-east-2 region support this parameter.

The LexicalOptions builder has the following methods:

Default: Lexical search is enabled and uses the standard Apache Lucene™ analyzer.

SetDefaultIdType(t options.CollectionIdType)

UpdateDefaultId(v …​CollectionDefaultIdOption)

Optional. Specifies the default ID type for documents in the collection. This is used when you insert a document without an _id field.

Supported ID types:

  • objectId: Each autogenerated _id value is an objectId as provided by the bson library.

  • uuidv7: Each autogenerated _id value is a version 7 UUID. This is designed as a replacement for version 1 time UUID, and it is recommended for use in new systems.

  • uuidv6: Each autogenerated _id value is a version 6 UUID. This is field-compatible with version 1 time UUIDs, and it supports lexicographical sorting.

  • uuid: Each autogenerated _id value is a version 4 UUID. This type is analogous to the uuid type and functions in Apache Cassandra®.

For more information, see Document IDs (Go).

Default: Each autogenerated _id value is a string form of a version 4 UUID

SetIndexingAllow(v …​string)

SetIndexingDeny(v …​string)

Optional. The selective indexing configuration for the collection.

You must use & to escape any . or & in field names in the indexing clause. You cannot use & to escape any other characters. Dot notation, which is used to reference nested fields, should not be escaped. For more information, see Work with . and & in field names (Go).

For examples, see Create a collection and specify which fields to index and Create a collection and specify which fields shouldn’t be indexed.

Default: All fields of all documents.

SetKeyspace(v string)

Optional if you specified a working keyspace when you created the Db object. The keyspace in which to create the collection.

Default: The working keyspace set when you created the Db object, if one was provided.

UpdateAPIOptions(v …​APIOption)

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

Examples

The following examples demonstrate how to create a collection.

Create a collection that is not vector-enabled

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 database
	client := astra.NewClient(
		options.API().SetEnvironment(options.EnvironmentHCD),
	)

	database := client.Database(
		"API_ENDPOINT",
		options.API().
			SetUsernamePasswordTokenProvider(
				"USERNAME",
				"PASSWORD",
			).
			SetKeyspace("KEYSPACE_NAME"),
	)

	// Create a collection
	_, err := database.CreateCollection(
		ctx,
		"COLLECTION_NAME",
	)
	if err != nil {
		log.Fatal(err)
	}
}

Create a collection that can store vector embeddings

Collections that are vector-enabled can store vector embeddings in the reserved $vector field and work with vector search.

For optimal vector search results, you should specify the dimension, metric, and source model of your vector embeddings. All vector embeddings in a collection should be generated by the same model with the same dimensions. The source model can be one of: ada002, bert, cohere-v3, gecko, nv-qa-4, openai-v3-large, openai-v3-small, other.

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 database
	client := astra.NewClient(
		options.API().SetEnvironment(options.EnvironmentHCD),
	)

	database := client.Database(
		"API_ENDPOINT",
		options.API().
			SetUsernamePasswordTokenProvider(
				"USERNAME",
				"PASSWORD",
			).
			SetKeyspace("KEYSPACE_NAME"),
	)

	// Create a collection
	_, err := database.CreateCollection(
		ctx,
		"COLLECTION_NAME",
		options.CreateCollection().
			UpdateVector(options.Vector().
				SetDimension(1024).
				SetMetric(options.MetricCosine).
				SetSourceModel("nv-qa-4")),
	)
	if err != nil {
		log.Fatal(err)
	}
}

Create a collection that supports lexicographical matching

If you want to use lexicographical matching to find documents in your collection, you must create a collection that has lexical enabled. Your collection must also be in a database in the AWS us-east-2 region.

Lexical is enabled by default when you create a collection in a database in the AWS us-east-2 region, but you can optionally configure the lexical analyzer.

For configuration details about the lexical analyzer, see Configure and use SAI text analyzers with CQL. The following example uses a configuration suitable for English text.

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 database
	client := astra.NewClient(
		options.API().SetEnvironment(options.EnvironmentHCD),
	)

	database := client.Database(
		"API_ENDPOINT",
		options.API().
			SetUsernamePasswordTokenProvider(
				"USERNAME",
				"PASSWORD",
			).
			SetKeyspace("KEYSPACE_NAME"),
	)

	// Create a collection
	_, err := database.CreateCollection(
		ctx,
		"COLLECTION_NAME",
		options.CreateCollection().
			UpdateLexical(options.Lexical().
				SetEnabled(true).
				SetCustomAnalyzer(map[string]any{
					"tokenizer": map[string]any{
						"name": "standard",
						"args": map[string]any{},
					},
					"filters": []map[string]any{
						{"name": "lowercase"},
						{"name": "stop"},
						{"name": "porterstem"},
						{"name": "asciifolding"},
					},
					"charFilters": []map[string]any{},
				})),
	)
	if err != nil {
		log.Fatal(err)
	}
}

Create a collection and specify the default ID format

For more information about the default ID format, see Document IDs (Go). For allowed values, see the Parameters.

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 database
	client := astra.NewClient(
		options.API().SetEnvironment(options.EnvironmentHCD),
	)

	database := client.Database(
		"API_ENDPOINT",
		options.API().
			SetUsernamePasswordTokenProvider(
				"USERNAME",
				"PASSWORD",
			).
			SetKeyspace("KEYSPACE_NAME"),
	)

	// Create a collection
	_, err := database.CreateCollection(
		ctx,
		"COLLECTION_NAME",
		options.CreateCollection().
			SetDefaultIdType(options.CollectionIdTypeObjectId),
	)
	if err != nil {
		log.Fatal(err)
	}
}

Create a collection and specify which fields to index

For more information about selective indexing, see Indexes in collections (Go).

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 database
	client := astra.NewClient(
		options.API().SetEnvironment(options.EnvironmentHCD),
	)

	database := client.Database(
		"API_ENDPOINT",
		options.API().
			SetUsernamePasswordTokenProvider(
				"USERNAME",
				"PASSWORD",
			).
			SetKeyspace("KEYSPACE_NAME"),
	)

	// Create a collection
	_, err := database.CreateCollection(
		ctx,
		"COLLECTION_NAME",
		options.CreateCollection().SetIndexingAllow("city", "country"),
	)
	if err != nil {
		log.Fatal(err)
	}
}

Create a collection and specify which fields shouldn’t be indexed

For more information about selective indexing, see Indexes in collections (Go).

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 database
	client := astra.NewClient(
		options.API().SetEnvironment(options.EnvironmentHCD),
	)

	database := client.Database(
		"API_ENDPOINT",
		options.API().
			SetUsernamePasswordTokenProvider(
				"USERNAME",
				"PASSWORD",
			).
			SetKeyspace("KEYSPACE_NAME"),
	)

	// Create a collection
	_, err := database.CreateCollection(
		ctx,
		"COLLECTION_NAME",
		options.CreateCollection().SetIndexingDeny("city", "country"),
	)
	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