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.
|
You cannot edit a collection’s definition after you create the collection. |
Signature
Use the CreateCollection method, which belongs to the Db type.
func (d *Db) CreateCollection(
ctx context.Context,
name string,
opts ...options.CreateCollectionOption
) (*Collection, error)
Parameters
| Name | Type | Summary |
|---|---|---|
|
|
The context for the operation. |
|
|
The name of the new collection. Collection names must follow these rules:
|
|
Optional. The options for this method. See Methods of the |
| Method | Summary |
|---|---|
|
Optional. The vector configuration for the collection. This includes things like the vector dimension, similarity metric, and source model. Required for vector search. For an example, see Create a collection that can store vector embeddings. The
|
|
Optional.
Specifies the default ID type for documents in the collection.
This is used when you insert a document without an Supported ID types:
For examples, see Create a collection and specify the default ID format. For more information, see Document IDs (Go). Default: Each autogenerated |
|
Optional. The selective indexing configuration for the collection. You must use 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. |
|
Optional if you specified a working keyspace when you created the Default: The working keyspace set when you created the |
|
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 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)
}
}
Errors
Specific error codes for this command include EXISTING_COLLECTION_DIFFERENT_SETTINGS, TOO_MANY_COLLECTIONS, TOO_MANY_INDEXES_FOR_COLLECTION, UNSUPPORTED_SCHEMA_NAME, INVALID_CREATE_COLLECTION_FIELD, INVALID_CREATE_COLLECTION_OPTIONS, VECTORIZE_FEATURE_NOT_AVAILABLE, INVALID_INDEXING_DEFINITION, EXISTING_INDEX_FOR_COLLECTION, EXISTING_TABLE_NOT_DATA_API_COLLECTION, VECTOR_SEARCH_UNRECOGNIZED_SOURCE_MODEL_NAME, and VECTOR_SEARCH_TOO_BIG_VALUE.
For a full list of Data API error codes and their corresponding messages, see Data API errors and error codes.
For more information about how the client handles underlying Data API errors, see Go client error handling.
Client reference
For more information, see the client reference.