Database / ChromaDB Interview Questions
How do you design metadata schemas for effective filtering in ChromaDB?
Metadata in ChromaDB is stored as flat key-value dictionaries where values must be strings, integers, or floats (not nested dicts or lists). Good metadata design makes the difference between fast, precise filtered queries and slow full-collection scans.
import chromadb
from datetime import datetime
client = chromadb.Client()
col = client.create_collection("knowledge_base")
# Good metadata design — flat, filterable fields
col.add(
documents=[
"Introduction to transformer architecture in deep learning.",
"BERT: Pre-training of Deep Bidirectional Transformers.",
"GPT-4 technical report overview.",
],
metadatas=[
{
"source": "textbook",
"author": "Vaswani",
"year": 2017, # int — supports $gt, $lt
"category": "architecture",
"citations": 50000, # int — sortable
"language": "en",
# timestamp as int for range queries
"added_ts": int(datetime(2024,1,1).timestamp()),
},
{
"source": "paper",
"author": "Devlin",
"year": 2018,
"category": "pretraining",
"citations": 40000,
"language": "en",
"added_ts": int(datetime(2024,1,2).timestamp()),
},
{
"source": "report",
"author": "OpenAI",
"year": 2023,
"category": "LLM",
"citations": 5000,
"language": "en",
"added_ts": int(datetime(2024,1,3).timestamp()),
},
],
ids=["p1","p2","p3"],
)
# Effective filtered queries
results = col.query(
query_texts=["neural network architecture"],
n_results=5,
where={"$and": [
{"year": {"$gte": 2017}},
{"citations":{"$gte": 10000}},
{"language": "en"},
]},
)
# Anti-patterns to avoid in metadata:
# BAD: {"tags": ["python", "nlp"]} — lists not supported
# BAD: {"author": {"name": "Vaswani", "affiliation": "Google"}} — nested not supported
# GOOD: {"tag_python": 1, "tag_nlp": 1} — flatten list membership to bool ints
# GOOD: {"author_name": "Vaswani", "author_org": "Google"} — flatten nested| Type | Supported? | Supports range filters? |
|---|---|---|
| str | Yes | Only $eq, $ne, $in, $nin |
| int | Yes | Yes — $gt, $gte, $lt, $lte |
| float | Yes | Yes — $gt, $gte, $lt, $lte |
| bool | No — use int 0/1 | — |
| list | No | — |
| dict (nested) | No | — |
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...
