Filter Examples

Example 1: E-commerce User Filtering

Let's consider a user collection with the following structure:

{
  "_id": "64f5a1b2c3d4e5f6a7b8c9d0",
  "name": "John Smith",
  "email": "[email protected]",
  "age": 28,
  "city": "New York",
  "registrationDate": "2023-01-15",
  "isActive": true,
  "orders": [
    {
      "orderId": "order_001",
      "amount": 250.50,
      "date": "2023-10-01",
      "status": "completed",
      "products": ["laptop", "mouse"]
    },
    {
      "orderId": "order_002",
      "amount": 89.99,
      "date": "2023-11-15",
      "status": "pending",
      "products": ["keyboard"]
    }
  ],
  "preferences": {
    "notifications": true,
    "newsletter": false,
    "categories": ["electronics", "books"]
  },
  "tags": ["premium", "regular_customer"]
}

Simple Filtering for Active Users from New York

{
  "isActive": true,
  "city": "New York"
}

Age and City-Based Filtering

{
  "$and": [
    { "age": { "$gte": 25 } },
    { "age": { "$lte": 35 } },
    { "city": { "$in": ["New York", "Los Angeles", "Chicago"] } }
  ]
}

Complex Order-Based Filtering

Find active users who made an order worth more than $100 in the last 6 months:

{
  "$and": [
    { "isActive": true },
    {
      "orders": {
        "$elemMatch": {
          "$and": [
            { "amount": { "$gt": 100 } },
            { "date": { "$gte": "2023-06-01" } },
            { "status": "completed" }
          ]
        }
      }
    }
  ]
}

Preferences and Tags Filtering

Find users subscribed to notifications, interested in electronics, and tagged as "premium":

{
  "$and": [
    { "preferences.notifications": true },
    { "preferences.categories": { "$in": ["electronics"] } },
    { "tags": { "$in": ["premium"] } }
  ]
}

Example 2: Product Catalog Filtering

Product structure:

{
  "_id": "product_123",
  "name": "Gaming Pro Laptop",
  "category": "electronics",
  "subcategory": "laptops",
  "price": 1299.99,
  "discountPrice": 1099.99,
  "inStock": true,
  "quantity": 15,
  "brand": "TechBrand",
  "rating": 4.7,
  "reviewsCount": 245,
  "specifications": {
    "cpu": "Intel i7",
    "ram": "16GB",
    "storage": "512GB SSD",
    "graphics": "RTX 3070"
  },
  "features": ["gaming", "portable", "high-performance"],
  "colors": ["black", "silver"],
  "createdDate": "2023-08-15",
  "tags": ["bestseller", "new"]
}

Price Range and Availability Filtering

Products in the $500-$1500 price range that are in stock:

{
  "$and": [
    { "price": { "$gte": 500 } },
    { "price": { "$lte": 1500 } },
    { "inStock": true },
    { "quantity": { "$gt": 0 } }
  ]
}

Complex Specification-Based Filtering

Gaming laptops with high ratings and specific specs:

{
  "$and": [
    { "category": "electronics" },
    { "subcategory": "laptops" },
    { "features": { "$in": ["gaming"] } },
    { "rating": { "$gte": 4.5 } },
    { "reviewsCount": { "$gte": 100 } },
    {
      "$or": [
        { "specifications.cpu": { "$regex": "i7|i9", "$options": "i" } },
        { "specifications.cpu": { "$regex": "Ryzen 7|Ryzen 9", "$options": "i" } }
      ]
    },
    { "specifications.ram": { "$in": ["16GB", "32GB"] } }
  ]
}

Discount-Based Filtering

Products with more than 15% discount and high ratings:

{
  "$and": [
    { "discountPrice": { "$exists": true } },
    {
      "$expr": {
        "$gt": [
          { "$divide": [{ "$subtract": ["$price", "$discountPrice"] }, "$price"] },
          0.15
        ]
      }
    },
    { "rating": { "$gte": 4.0 } },
    { "inStock": true }
  ]
}

Example 3: Complex Event Filtering

Event structure:

{
  "_id": "event_456",
  "title": "Web Development Conference",
  "description": "Exploring modern technologies",
  "category": "technology",
  "subcategories": ["web-development", "javascript", "react"],
  "startDate": "2023-12-01T09:00:00Z",
  "endDate": "2023-12-01T18:00:00Z",
  "location": {
    "city": "San Francisco",
    "venue": "Tech Hub",
    "address": "123 Innovation St"
  },
  "price": 199.99,
  "currency": "USD",
  "capacity": 200,
  "registeredCount": 150,
  "status": "active",
  "organizer": {
    "name": "Tech Events Inc",
    "rating": 4.8
  },
  "speakers": [
    {
      "name": "Alex Johnson",
      "expertise": ["javascript", "react"],
      "rating": 4.9
    }
  ],
  "requirements": ["laptop", "basic_js_knowledge"],
  "benefits": ["certificate", "networking", "materials"]
}

Events in December 2023 in San Francisco or online:

{
  "$and": [
    { "startDate": { "$gte": "2023-12-01T00:00:00Z" } },
    { "startDate": { "$lt": "2024-01-01T00:00:00Z" } },
    {
      "$or": [
        { "location.city": "San Francisco" },
        { "location.city": "online" }
      ]
    },
    { "status": "active" }
  ]
}

Complex Speaker and Topic Filtering

Tech events with high-rated JavaScript/React speakers:

{
  "$and": [
    { "category": "technology" },
    { "subcategories": { "$in": ["javascript", "react", "web-development"] } },
    {
      "speakers": {
        "$elemMatch": {
          "$and": [
            { "expertise": { "$in": ["javascript", "react"] } },
            { "rating": { "$gte": 4.5 } }
          ]
        }
      }
    },
    { "organizer.rating": { "$gte": 4.0 } },
    {
      "$expr": {
        "$lt": ["$registeredCount", { "$multiply": ["$capacity", 0.9] }]
      }
    }
  ]
}

On this page