Skip to main content

Collection

The Collection class represents a collection of embeddings in Chroma. It provides methods for adding, querying, updating, and deleting embeddings.

Properties

name
string
The name of the collection
id
string
The unique identifier of the collection
metadata
CollectionMetadata | undefined
Optional metadata associated with the collection
embeddingFunction
IEmbeddingFunction
The embedding function used by this collection
configuration
CollectionConfiguration | undefined
The collection’s configuration

Methods

add

Add items to the collection.
await collection.add(params: AddRecordsParams): Promise<void>
Parameters:
params
AddRecordsParams
required
Returns: Promise that resolves when items are added
You must provide either embeddings or documents (which will be embedded automatically using the collection’s embedding function).
Example:
await collection.add({
  ids: ["id1", "id2"],
  embeddings: [[1, 2, 3], [4, 5, 6]],
  metadatas: [{ "key": "value" }, { "key": "value" }],
  documents: ["document1", "document2"]
});

query

Performs a query on the collection using the specified parameters.
await collection.query(params: QueryRecordsParams): Promise<MultiQueryResponse>
Parameters:
params
QueryRecordsParams
required
Returns: Promise resolving to MultiQueryResponse Throws: Error if neither queryEmbeddings nor queryTexts are provided
You must provide either queryEmbeddings or queryTexts.
Example with query text:
const results = await collection.query({
  queryTexts: "some text",
  nResults: 10,
  where: {"name": {"$eq": "John Doe"}},
  include: ["metadatas", "documents"]
});
Example with query embeddings:
const results = await collection.query({
  queryEmbeddings: [[0.1, 0.2, 0.3]],
  nResults: 10,
  where: {"name": {"$eq": "John Doe"}},
  include: ["metadatas", "documents"]
});

get

Get items from the collection.
await collection.get(params?: BaseGetParams): Promise<GetResponse>
Parameters:
params
BaseGetParams
Returns: Promise resolving to GetResponse Example:
const response = await collection.get({
  ids: ["id1", "id2"],
  where: { "key": "value" },
  limit: 10,
  offset: 0,
  include: ["embeddings", "metadatas", "documents"],
  whereDocument: { "$contains": "value" }
});

update

Update items in the collection.
await collection.update(params: UpdateRecordsParams): Promise<void>
Parameters:
params
UpdateRecordsParams
required
Returns: Promise that resolves when items are updated Example:
await collection.update({
  ids: ["id1", "id2"],
  embeddings: [[1, 2, 3], [4, 5, 6]],
  metadatas: [{ "key": "value" }, { "key": "value" }],
  documents: ["document1", "document2"]
});

upsert

Upsert (insert or update) items to the collection.
await collection.upsert(params: UpsertRecordsParams): Promise<void>
Parameters:
params
UpsertRecordsParams
required
Same as AddRecordsParams
Returns: Promise that resolves when items are upserted Example:
await collection.upsert({
  ids: ["id1", "id2"],
  embeddings: [[1, 2, 3], [4, 5, 6]],
  metadatas: [{ "key": "value" }, { "key": "value" }],
  documents: ["document1", "document2"]
});

delete

Deletes items from the collection.
await collection.delete(params?: DeleteParams): Promise<void>
Parameters:
params
DeleteParams
Returns: Promise that resolves when items are deleted Throws: Error if there is an issue deleting items Example:
await collection.delete({
  ids: "some_id",
  where: {"name": {"$eq": "John Doe"}},
  whereDocument: {"$contains": "search_string"}
});

count

Count the number of items in the collection.
await collection.count(): Promise<number>
Returns: Promise resolving to the number of items in the collection Example:
const count = await collection.count();
console.log(`Collection has ${count} items`);

peek

Peek inside the collection to get a sample of items.
await collection.peek(params?: PeekParams): Promise<MultiGetResponse>
Parameters:
params
PeekParams
Returns: Promise resolving to the query results Throws: Error if there is an issue executing the query Example:
const results = await collection.peek({
  limit: 10
});

modify

Modify the collection name or metadata.
await collection.modify(params: {
  name?: string;
  metadata?: CollectionMetadata;
  configuration?: UpdateCollectionConfiguration;
}): Promise<CollectionParams>
Parameters:
params
object
required
Returns: Promise resolving to the updated collection parameters Example:
await collection.modify({
  name: "new_collection_name",
  metadata: { "description": "Updated description" }
});

fork

Forks the collection into a new collection with a new name.
await collection.fork(params: ForkCollectionParams): Promise<Collection>
Parameters:
params
ForkCollectionParams
required
Returns: Promise resolving to the new forked Collection object Throws: Error if there is an issue forking the collection Example:
const newCollection = await collection.fork({
  newName: "my_forked_collection"
});

Build docs developers (and LLMs) love