Get a collection (C#)
Gets a reference to a collection for use with the Data API clients.
|
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
Returns a Collection object that corresponds to the specified collection name.
This method returns a Collection object even for collections that don’t exist.
By default, the Collection object is typed as Collection<Document>, where Document is Dictionary<string, object>.
You can enable stronger typing by specifying a type when you get the collection.
For more information and examples, see Custom typing for collections.
Parameters
Use the GetCollection method, which belongs to the Database class.
Method signature
public Collection<Document> GetCollection(
string collectionName, GetCollectionOptions options = null
);
public Collection<T> GetCollection<T>(
string collectionName, GetCollectionOptions options = null
) where T : class;
public Collection<T> GetCollection<T>(
GetCollectionOptions options = null
) where T : class;
public Collection<T, TId> GetCollection<T, TId>(
GetCollectionOptions options = null
) where T : class;
public Collection<T, TId> GetCollection<T, TId>(
string collectionName, GetCollectionOptions options = null
) where T : class;
| Name | Type | Summary |
|---|---|---|
|
|
The name of the collection. If not specified, the client attempts to extract it from the |
|
Optional.
Options for this operation.
For more information and examples for general options such as timeout and keyspace, see Customize API interaction.
For options specific to this method, see Method-specific properties of the |
| Name | Type | Summary |
|---|---|---|
|
|
Optional. This only applies to collections with a vectorize embedding provider integration. Use this option to provide the embedding provider API key directly with headers instead of using an API key in the Astra DB KMS. The API key is sent to the Data API for every operation on the collection. It is useful when a vectorize integration is configured but no credentials are stored, or when you want to override the stored credentials. For more information, see Manage embedding provider integrations for vectorize. If you use an AWS embedding provider, you must use the |
|
|
If you use an AWS embedding provider, you must use the |
Examples
The following examples demonstrate how to get a collection.
Get a collection
-
Typed collections
-
Untyped collections
You can manually define a client-side type for your collection to help statically catch errors. For more information and examples, see Custom typing for collections.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.SerDes;
namespace Examples;
// Define the type for the collection
[CollectionName("COLLECTION_NAME")]
public class User
{
[DocumentId]
public Guid? Id { get; set; }
public string Name { get; set; } = null!;
public int? Age { get; set; }
[DocumentMapping(DocumentMappingField.Vectorize)]
public string StringToVectorize => Name;
}
public class Program
{
static void Main()
{
// Get a database
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
// Get a collection
var collection = database.GetCollection<User>();
}
}
If you don’t pass a type parameter, the collection or table remains untyped. This is a more flexible but less type-safe option.
using DataStax.AstraDB.DataApi;
namespace Examples;
public class Program
{
static void Main()
{
// Get a database
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
// Get a collection
var collection = database.GetCollection("COLLECTION_NAME");
}
}
Get a collection and specify the keyspace
-
Typed collections
-
Untyped collections
You can manually define a client-side type for your collection to help statically catch errors. For more information and examples, see Custom typing for collections.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Collections;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.SerDes;
namespace Examples;
// Define the type for the collection
[CollectionName("COLLECTION_NAME")]
public class User
{
[DocumentId]
public Guid? Id { get; set; }
public string Name { get; set; } = null!;
public int? Age { get; set; }
[DocumentMapping(DocumentMappingField.Vectorize)]
public string StringToVectorize => Name;
}
public class Program
{
static void Main()
{
// Get a database
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
// Get a collection
var collection = database.GetCollection<User>(
new GetCollectionOptions() { Keyspace = "KEYSPACE_NAME" }
);
}
}
If you don’t pass a type parameter, the collection or table remains untyped. This is a more flexible but less type-safe option.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static void Main()
{
// Get a database
var client = new DataAPIClient();
var database = client.GetDatabase(
"API_ENDPOINT",
"APPLICATION_TOKEN"
);
// Get a collection
var collection = database.GetCollection(
"COLLECTION_NAME",
new GetCollectionOptions() { Keyspace = "KEYSPACE_NAME" }
);
}
}
Client reference
For more information, see the client reference.