Database / ChromaDB Interview Questions
How do you retrieve, update, and delete specific documents in ChromaDB?
Beyond querying by similarity, ChromaDB supports exact lookups by ID with get(), in-place updates with update() or upsert(), and deletion with delete().
import chromadb
client = chromadb.Client()
col = client.create_collection("items")
col.add(
documents=["First document", "Second document", "Third document"],
metadatas=[{"v": 1}, {"v": 2}, {"v": 3}],
ids=["id1", "id2", "id3"],
)
# GET — fetch by specific IDs
result = col.get(ids=["id1", "id3"])
print(result["documents"]) # ["First document", "Third document"]
# GET all documents in the collection
all_docs = col.get() # no ids= returns everything
# GET with include control
result = col.get(
ids=["id1"],
include=["documents", "metadatas", "embeddings"],
)
# UPDATE — must already exist, updates only specified fields
col.update(
ids=["id1"],
documents=["Updated first document"],
metadatas=[{"v": 10, "updated": True}],
# ChromaDB re-embeds the new document text automatically
)
# UPSERT — insert if not exists, update if exists (idempotent)
col.upsert(
documents=["Brand new doc", "Updated second doc"],
metadatas=[{"v": 99}, {"v": 20}],
ids= ["id-new", "id2"],
)
# id-new is inserted; id2 is updated
# DELETE by IDs
col.delete(ids=["id3"])
print(col.count()) # 3 (id1, id2, id-new remain)
# DELETE by metadata filter (where clause)
col.delete(where={"v": {"$gt": 15}})| Method | Behaviour when ID exists | Behaviour when ID missing |
|---|---|---|
| add() | Raises IDAlreadyExistsError | Inserts new document |
| update() | Updates the document | Raises error — ID must exist |
| upsert() | Updates the document | Inserts new document |
| delete() | Removes the document | Silently ignores |
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...
