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 |
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
