Advanced

Advanced Aggregation Pipeline

Window Functions

// Window functions with $setWindowFields
db.sales.aggregate([
    {
        $setWindowFields: {
            partitionBy: "$region",
            sortBy: { date: 1 },
            output: {
                runningTotal: {
                    $sum: "$amount",
                    window: {
                        documents: ["unbounded", "current"]
                    }
                },
                movingAverage: {
                    $avg: "$amount",
                    window: {
                        documents: [-2, 0]
                    }
                }
            }
        }
    }
])

// Rank documents within partitions
db.products.aggregate([
    {
        $setWindowFields: {
            partitionBy: "$category",
            sortBy: { price: -1 },
            output: {
                rank: {
                    $rank: {}
                },
                denseRank: {
                    $denseRank: {}
                }
            }
        }
    }
])

Graph Lookup

// Graph lookup for hierarchical data
db.employees.aggregate([
    {
        $graphLookup: {
            from: "employees",
            startWith: "$reportsTo",
            connectFromField: "reportsTo",
            connectToField: "_id",
            as: "hierarchy",
            maxDepth: 3,
            depthField: "level"
        }
    }
])

// Graph lookup with restrictions
db.categories.aggregate([
    {
        $graphLookup: {
            from: "categories",
            startWith: "$parentId",
            connectFromField: "parentId",
            connectToField: "_id",
            as: "ancestors",
            restrictSearchWithMatch: { active: true }
        }
    }
])
// Faceted search with multiple aggregations
db.products.aggregate([
    {
        $facet: {
            categories: [
                { $group: { _id: "$category", count: { $sum: 1 } } },
                { $sort: { count: -1 } }
            ],
            priceRanges: [
                {
                    $bucket: {
                        groupBy: "$price",
                        boundaries: [0, 50, 100, 200, 500],
                        default: "Above 500",
                        output: { count: { $sum: 1 } }
                    }
                }
            ],
            brands: [
                { $group: { _id: "$brand", count: { $sum: 1 } } },
                { $sort: { count: -1 } },
                { $limit: 10 }
            ]
        }
    }
])

Bucket Aggregation

// Auto bucket aggregation
db.products.aggregate([
    {
        $bucketAuto: {
            groupBy: "$price",
            buckets: 5,
            output: {
                count: { $sum: 1 },
                avgPrice: { $avg: "$price" },
                products: { $push: "$name" }
            }
        }
    }
])

// Manual bucket aggregation
db.orders.aggregate([
    {
        $bucket: {
            groupBy: "$amount",
            boundaries: [0, 100, 500, 1000, 5000],
            default: "High Value",
            output: {
                count: { $sum: 1 },
                totalAmount: { $sum: "$amount" },
                avgAmount: { $avg: "$amount" }
            }
        }
    }
])

Advanced Query Techniques

Text Search with Weights

// Text search with field weights
db.products.find(
    { $text: { $search: "laptop computer" } },
    { score: { $meta: "textScore" } }
).sort({ score: { $meta: "textScore" } })

// Text search with language
db.products.find(
    { $text: { $search: "café", $language: "french" } }
)

Geospatial Queries

// Near query with max distance
db.places.find({
    location: {
        $near: {
            $geometry: {
                type: "Point",
                coordinates: [-73.97, 40.77]
            },
            $maxDistance: 5000
        }
    }
})

// GeoWithin with polygon
db.places.find({
    location: {
        $geoWithin: {
            $geometry: {
                type: "Polygon",
                coordinates: [[
                    [-74, 40], [-74, 41], [-73, 41], [-73, 40], [-74, 40]
                ]]
            }
        }
    }
})

// GeoIntersects
db.areas.find({
    boundary: {
        $geoIntersects: {
            $geometry: {
                type: "Point",
                coordinates: [-73.97, 40.77]
            }
        }
    }
})

Array Operations

// Array element matching with conditions
db.users.find({
    scores: {
        $elemMatch: {
            $gte: 80,
            $lte: 100
        }
    }
})

// Array with positional operator
db.users.updateOne(
    { "scores": { $gte: 80 } },
    { $set: { "scores.$": 95 } }
)

// Array with slice
db.users.find({}, { scores: { $slice: [0, 3] } })

// Array with filter
db.users.aggregate([
    {
        $addFields: {
            highScores: {
                $filter: {
                    input: "$scores",
                    cond: { $gte: ["$$this", 80] }
                }
            }
        }
    }
])

Advanced Update Operations

Array Update Operators

// Push with each and slice
db.users.updateOne(
    { email: "john@example.com" },
    {
        $push: {
            tags: {
                $each: ["mongodb", "nodejs"],
                $slice: -5  // Keep only last 5 tags
            }
        }
    }
)

// Push with sort
db.users.updateOne(
    { email: "john@example.com" },
    {
        $push: {
            scores: {
                $each: [85, 92],
                $sort: -1  // Sort descending
            }
        }
    }
)

// Pull with regex
db.users.updateOne(
    { email: "john@example.com" },
    { $pull: { tags: { $regex: /^js/ } } }
)

// PullAll with multiple values
db.users.updateOne(
    { email: "john@example.com" },
    { $pullAll: { tags: ["old", "deprecated"] } }
)

// AddToSet (unique values only)
db.users.updateOne(
    { email: "john@example.com" },
    { $addToSet: { tags: "mongodb" } }
)

Conditional Updates

// Update with conditional logic
db.users.updateMany(
    { age: { $gte: 18 } },
    {
        $set: {
            status: {
                $cond: {
                    if: { $gte: ["$age", 21] },
                    then: "adult",
                    else: "young_adult"
                }
            }
        }
    }
)

// Update with array filters
db.users.updateOne(
    { email: "john@example.com" },
    { $set: { "scores.$[elem]": 95 } },
    { arrayFilters: [{ "elem": { $gte: 80 } }] }
)

Advanced Indexing

Partial Indexes

// Partial index with complex conditions
db.orders.createIndex(
    { orderDate: 1, status: 1 },
    {
        partialFilterExpression: {
            $and: [
                { amount: { $gte: 100 } },
                { status: { $in: ["pending", "processing"] } }
            ]
        }
    }
)

// Partial index with exists
db.users.createIndex(
    { email: 1 },
    {
        partialFilterExpression: {
            email: { $exists: true },
            status: "active"
        }
    }
)

Sparse Indexes

// Sparse unique index
db.users.createIndex(
    { phone: 1 },
    { unique: true, sparse: true }
)

// Sparse compound index
db.users.createIndex(
    { phone: 1, fax: 1 },
    { sparse: true }
)

TTL Indexes with Filters

// TTL index with partial filter
db.sessions.createIndex(
    { lastActivity: 1 },
    {
        expireAfterSeconds: 3600,
        partialFilterExpression: {
            type: "temporary"
        }
    }
)

// TTL index on specific time
db.logs.createIndex(
    { timestamp: 1 },
    { expireAfterSeconds: 86400 }
)

Advanced Data Modeling

Embedded Documents

// Complex embedded document
db.users.insertOne({
    _id: ObjectId(),
    name: "John Doe",
    profile: {
        personal: {
            firstName: "John",
            lastName: "Doe",
            birthDate: new Date("1990-05-15"),
            addresses: [
                {
                    type: "home",
                    street: "123 Main St",
                    city: "New York",
                    state: "NY",
                    zipCode: "10001",
                    country: "USA",
                    isDefault: true
                }
            ]
        },
        preferences: {
            theme: "dark",
            notifications: {
                email: true,
                sms: false,
                push: true
            },
            privacy: {
                profileVisibility: "public",
                showEmail: false
            }
        }
    }
})

Array of Objects

// Complex array of objects
db.products.insertOne({
    _id: ObjectId(),
    name: "Laptop",
    variants: [
        {
            sku: "LAP-001-8GB",
            specs: {
                ram: "8GB",
                storage: "256GB SSD",
                color: "Silver"
            },
            price: 999.99,
            inStock: true,
            inventory: {
                quantity: 50,
                reserved: 5,
                available: 45
            }
        },
        {
            sku: "LAP-001-16GB",
            specs: {
                ram: "16GB",
                storage: "512GB SSD",
                color: "Space Gray"
            },
            price: 1299.99,
            inStock: true,
            inventory: {
                quantity: 25,
                reserved: 2,
                available: 23
            }
        }
    ]
})

Advanced Validation

JSON Schema Validation

// Complex validation schema
db.createCollection("users", {
    validator: {
        $jsonSchema: {
            bsonType: "object",
            required: ["name", "email", "age"],
            properties: {
                name: {
                    bsonType: "string",
                    minLength: 2,
                    maxLength: 100
                },
                email: {
                    bsonType: "string",
                    pattern: "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$"
                },
                age: {
                    bsonType: "int",
                    minimum: 0,
                    maximum: 150
                },
                phone: {
                    bsonType: "string",
                    pattern: "^\\+?[1-9]\\d{1,14}$"
                },
                profile: {
                    bsonType: "object",
                    properties: {
                        bio: {
                            bsonType: "string",
                            maxLength: 500
                        },
                        website: {
                            bsonType: "string",
                            pattern: "^https?://.*"
                        }
                    }
                },
                tags: {
                    bsonType: "array",
                    items: {
                        bsonType: "string",
                        minLength: 1
                    },
                    maxItems: 10
                }
            }
        }
    },
    validationLevel: "strict",
    validationAction: "error"
})

Custom Validation

// Custom validation with expressions
db.createCollection("orders", {
    validator: {
        $expr: {
            $and: [
                { $gte: ["$amount", 0] },
                { $lte: ["$amount", 10000] },
                {
                    $cond: {
                        if: { $eq: ["$status", "completed"] },
                        then: { $ne: ["$completedAt", null] },
                        else: true
                    }
                }
            ]
        }
    }
})

Advanced Performance

Covered Queries

// Covered query with compound index
db.users.createIndex({ email: 1, name: 1, status: 1 })

// Query that uses index only
db.users.find(
    { email: "john@example.com" },
    { email: 1, name: 1, _id: 0 }
)

Query Optimization

// Force index usage
db.users.find({ email: "john@example.com" }).hint({ email: 1 })

// Explain query with different modes
db.users.find({ email: "john@example.com" }).explain("allPlansExecution")

// Analyze query performance
db.users.find({ email: "john@example.com" }).explain("executionStats")

Aggregation Optimization

// Aggregation with index hint
db.orders.aggregate([
    { $match: { status: "completed" } },
    { $group: { _id: "$customerId", total: { $sum: "$amount" } } }
], { hint: { status: 1, customerId: 1 } })

// Aggregation with allowDiskUse
db.largeCollection.aggregate([
    { $group: { _id: "$category", count: { $sum: 1 } } }
], { allowDiskUse: true })

Advanced Security

Role-Based Access Control

// Create custom role
db.createRole({
    role: "dataAnalyst",
    privileges: [
        {
            resource: { db: "analytics", collection: "reports" },
            actions: ["find", "aggregate"]
        },
        {
            resource: { db: "analytics", collection: "metrics" },
            actions: ["find", "aggregate"]
        }
    ],
    roles: []
})

// Create user with custom role
db.createUser({
    user: "analyst1",
    pwd: "securepassword",
    roles: [
        { role: "dataAnalyst", db: "analytics" },
        { role: "read", db: "reports" }
    ]
})

Field-Level Security

// Create user with field restrictions
db.createUser({
    user: "limitedUser",
    pwd: "password",
    roles: [{ role: "read", db: "myapp" }],
    restrictions: {
        clientSource: ["192.168.1.0/24"],
        serverAddress: ["127.0.0.1"]
    }
})

Advanced Monitoring

Performance Monitoring

// Get detailed operation stats
db.currentOp({
    $or: [
        { "op": "query", "secs_running": { $gt: 5 } },
        { "op": "update", "secs_running": { $gt: 10 } }
    ]
})

// Get database profiler status
db.getProfilingStatus()

// Set profiling with custom threshold
db.setProfilingLevel(1, { slowms: 50 })

// Get slow query analysis
db.system.profile.find({
    millis: { $gt: 100 },
    op: { $in: ["query", "update", "remove"] }
}).sort({ ts: -1 }).limit(10)

Index Usage Analysis

// Get index usage statistics
db.users.aggregate([{ $indexStats: {} }])

// Find unused indexes
db.users.aggregate([
    { $indexStats: {} },
    { $match: { "accesses.ops": { $lt: 100 } } }
])

// Get index size information
db.users.stats().indexSizes

Advanced Data Operations

Change Streams

// Watch for changes in collection
const changeStream = db.users.watch([
    { $match: { "operationType": { $in: ["insert", "update", "delete"] } } }
])

// Watch with pipeline
const changeStream = db.orders.watch([
    {
        $match: {
            "fullDocument.status": "completed",
            "operationType": "update"
        }
    }
])

// Watch with resume token
const changeStream = db.users.watch([], {
    resumeAfter: { _data: "resumeToken" }
})

Bulk Operations

// Ordered bulk operations
const bulk = db.users.initializeOrderedBulkOp()
bulk.insert({ name: "User1", email: "user1@example.com" })
bulk.insert({ name: "User2", email: "user2@example.com" })
bulk.find({ email: "old@example.com" }).updateOne({ $set: { email: "new@example.com" } })
bulk.find({ status: "inactive" }).remove()
const result = bulk.execute()

// Unordered bulk operations
const bulk = db.users.initializeUnorderedBulkOp()
bulk.insert({ name: "User3", email: "user3@example.com" })
bulk.find({ email: "user4@example.com" }).upsert().updateOne({ $set: { name: "User4" } })
const result = bulk.execute()

MapReduce (Legacy)

// MapReduce for complex aggregation
db.orders.mapReduce(
    function() {
        emit(this.customerId, {
            count: 1,
            total: this.amount,
            orders: [this.orderNumber]
        });
    },
    function(key, values) {
        var result = { count: 0, total: 0, orders: [] };
        values.forEach(function(value) {
            result.count += value.count;
            result.total += value.total;
            result.orders = result.orders.concat(value.orders);
        });
        return result;
    },
    {
        out: "customer_summary",
        query: { status: "completed" },
        finalize: function(key, value) {
            value.average = value.total / value.count;
            return value;
        }
    }
)

Last updated