Database / ChromaDB Interview Questions
What is the where_document filter in ChromaDB and how does it differ from where?
ChromaDB provides two types of filters that can be used together or separately: where filters on metadata fields (structured key-value pairs), while where_document filters on the raw text content of the stored documents. Both can be combined in a single query.
import chromadb
client = chromadb.Client()
col = client.create_collection("mixed_docs")
col.add(
documents=[
"Python tutorial for beginners with examples",
"Advanced Python decorators and metaclasses",
"JavaScript async/await guide",
"Python data science with pandas and numpy",
"Rust memory safety tutorial",
],
metadatas=[
{"lang": "python", "level": "beginner"},
{"lang": "python", "level": "advanced"},
{"lang": "js", "level": "intermediate"},
{"lang": "python", "level": "intermediate"},
{"lang": "rust", "level": "beginner"},
],
ids=["d1","d2","d3","d4","d5"],
)
# where_document: filter on text content
results = col.query(
query_texts=["programming guide"],
n_results=5,
where_document={"$contains": "tutorial"}, # text must contain "tutorial"
)
print(results["ids"]) # d1, d2 (Python tut), d5 (Rust tut) — JS has no "tutorial"
# where_document with NOT
results = col.query(
query_texts=["programming"],
n_results=5,
where_document={"$not_contains": "JavaScript"},
)
# Combine where (metadata) + where_document (text content)
results = col.query(
query_texts=["learning to code"],
n_results=3,
where={"lang": "python"}, # metadata filter
where_document={"$contains": "tutorial"}, # content filter
# Only Python docs whose text contains "tutorial"
)
print(results["documents"])
# Only matches d1: "Python tutorial for beginners with examples"| Filter | Operates on | Supported operators |
|---|---|---|
| where | Metadata key-value fields | $eq, $ne, $gt, $gte, $lt, $lte, $in, $nin, $and, $or |
| where_document | Raw document text content | $contains, $not_contains |
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...
