You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Advanced patterns and complex queries with $and, $or, $not operators
Advanced Logical Operators Guide
Complex Query Patterns
E-commerce Product Filtering
interfaceProduct{name: string;price: number;category: string;brand: string;rating: number;inStock: boolean;tags: string[];discount: number;}constproducts: Product[]=[...];// Find premium products: (High rating OR popular brand) AND in stock AND affordablefilter(products,{$and: [{$or: [{rating: {$gte: 4.5}},{brand: {$in: ['Apple','Samsung','Sony']}}]},{inStock: true},{price: {$lte: 1000}}]});// Clearance items: On sale BUT NOT (out of stock OR discontinued)filter(products,{discount: {$gt: 0},$not: {$or: [{inStock: false},{tags: {$contains: 'discontinued'}}]}});
User Access Control
interfaceUser{id: number;role: string;department: string;active: boolean;permissions: string[];lastLogin: Date;}constusers: User[]=[...];// Active admins OR users with specific permissionfilter(users,{active: true,$or: [{role: 'admin'},{permissions: {$contains: 'manage_users'}}]});// Users needing attention: Inactive OR (no recent login AND not admin)filter(users,{$or: [{active: false},{$and: [{lastLogin: {$lt: newDate('2025-01-01')}},{$not: {role: 'admin'}}]}]});
interfaceLocation{city: string;state: string;country: string;region: string;}// US West Coast OR major European citiesfilter(locations,{$or: [{$and: [{country: 'USA'},{state: {$in: ['CA','OR','WA']}}]},{$and: [{country: {$in: ['UK','France','Germany']}},{city: {$in: ['London','Paris','Berlin']}}]}]});
Time-Based Queries
// Recent activity: Last 7 days OR high-priority itemsconstsevenDaysAgo=newDate();sevenDaysAgo.setDate(sevenDaysAgo.getDate()-7);filter(tasks,{$or: [{updatedAt: {$gte: sevenDaysAgo}},{priority: 'high'}],$not: {status: 'completed'}});
// ❌ Hard to read and maintainfilter(data,{$and: [{$or: [{$and: [{a: 1},{$or: [{b: 2},{c: 3}]}]},{d: 4}]}]});// ✅ Better: Flatten when possiblefilter(data,{$or: [{$and: [{a: 1},{b: 2}]},{$and: [{a: 1},{c: 3}]},{d: 4}]});
Common Patterns
Inclusion with Exclusions
// Include category BUT exclude specific itemsfilter(products,{category: 'Electronics',$not: {$or: [{brand: 'BrandX'},{tags: {$contains: 'refurbished'}}]}});
Range with Exceptions
// Price range BUT NOT specific excluded pricesfilter(products,{$and: [{price: {$gte: 100,$lte: 500}},{$not: {price: {$in: [199,299,399]}}}]});
// ❌ Wrong: $and with single condition (unnecessary)filter(data,{$and: [{price: {$gt: 100}}]});// ✅ Correct: Direct conditionfilter(data,{price: {$gt: 100}});// ❌ Wrong: $or with object (should be array)filter(data,{$or: {a: 1,b: 2}});// ✅ Correct: $or with arrayfilter(data,{$or: [{a: 1},{b: 2}]});// ❌ Wrong: $not with array (should be single expression)filter(data,{$not: [{a: 1},{b: 2}]});// ✅ Correct: $not with single expression or use $andfilter(data,{$not: {$or: [{a: 1},{b: 2}]}});