Array filter logic
Normal filters use OR logic across array items:- Find films where
actors.country = "France" OR actors.age > 35 - Returns films with ANY actor who is French OR over 35 (possibly different actors)
- Find films where
_foreign(actors, country = "France" AND age > 35) - Returns films with an actor who is BOTH French AND over 35 (same actor meets both conditions)
Why this matters
Precise filtering is essential when you need to find documents where a related item meets multiple conditions simultaneously. Without this capability, you’d need to denormalize data or post-process results in your application code.Film cast example
Consider a film with three actors:Query 1: Normal filter (OR logic)
- Actor B is French (country = “France”) ✓
- Actor A is over 35 (age > 35) ✓
Query 2: Foreign filter (AND logic)
- Actor B is BOTH French AND over 35 ✓
Real-world examples
Find products with multiple required certifications
Find articles with specific tag combinations
Find articles that have both the “security” AND “encryption” tags (same article, not different articles):Find employees with specific skill levels
Find employees with both Python AND JavaScript at advanced level:When to use precise filtering
Use foreign filters with AND logic when:- A document has an array of related items
- You need to find items where a single related item meets multiple conditions
- You’re filtering on nested properties of array items
- You’re checking if any item in an array matches your criteria
- You need broader matching across multiple array items
Next steps
Foreign filters overview
Learn about foreign filters and filtering by joined data
Define relationships
Set up the relationships this filtering depends on