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 |
More Related questions...