Database / ChromaDB Interview Questions
What is a ChromaDB collection and how do you create, list, get, and delete collections?
A collection is ChromaDB's primary organisational unit -analogous to a table in SQL or an index in a search engine. Each collection stores documents, their embeddings, IDs, and optional metadata. All items in a collection share the same embedding function and distance metric.
import chromadb client = chromadb.PersistentClient(path="./chroma_db") # CREATE a collection collection = client.create_collection( name="research_papers", metadata={"hnsw:space": "cosine"}, # embedding_function defaults to all-MiniLM-L6-v2 ) # GET an existing collection (raises error if not found) collection = client.get_collection("research_papers") # GET or CREATE - idempotent, safe to call on every startup collection = client.get_or_create_collection( name="research_papers", metadata={"hnsw:space": "cosine"}, ) # LIST all collections collections = client.list_collections() for col in collections: print(col.name) # prints collection names # COUNT documents in a collection print(collection.count()) # number of items stored # DELETE a collection and all its data client.delete_collection("research_papers") # MODIFY collection name or metadata collection.modify( name="arxiv_papers", metadata={"hnsw:space": "cosine", "description": "arXiv CS papers"}, )
| Method | Purpose | Raises if |
|---|---|---|
| create_collection(name) | Creates new collection | Name already exists |
| get_collection(name) | Gets existing collection | Name not found |
| get_or_create_collection(name) | Idempotent get/create | Never raises |
| list_collections() | Returns all collection names | - |
| delete_collection(name) | Permanently deletes collection + data | Name not found |
More Related questions...