Unlocking the Power of Compound Wildcard Indexes in MongoDB 7.0
MongoDB 7.0 introduces significant enhancements to wildcard indexing capabilities, with compound wildcard indexes representing one of the most powerful features for optimizing queries across dynamic schemas. This comprehensive guide explores how to leverage these indexes for maximum performance gains.
Understanding Compound Wildcard Indexes
Compound wildcard indexes combine the flexibility of wildcard indexes with the performance benefits of compound indexing. Unlike traditional wildcard indexes that operate on a single field pattern, compound wildcard indexes allow you to create indexes that span multiple fields, including both specific fields and wildcard patterns.
Key Benefits
- Enhanced Query Performance: Optimize queries that filter on both specific and dynamic fields
- Schema Flexibility: Support evolving document structures without index maintenance
- Reduced Index Overhead: Single index covers multiple query patterns
- Improved Aggregation Pipeline Performance: Accelerate complex analytical queries
Creating Compound Wildcard Indexes
// Basic compound wildcard index
db.products.createIndex({
"category": 1,
"attributes.$**": 1
})
// Multi-field compound wildcard
db.analytics.createIndex({
"timestamp": 1,
"userId": 1,
"metadata.$**": 1
})
// Mixed ascending/descending with wildcards
db.events.createIndex({
"eventType": 1,
"timestamp": -1,
"properties.$**": 1
})
Advanced Configuration Options
Wildcard Projection
Control which fields are included in the wildcard portion:
db.collection.createIndex(
{
"status": 1,
"data.$**": 1
},
{
"wildcardProjection": {
"data.sensitive": 0,
"data.internal": 0
}
}
)
Partial Indexes with Wildcards
Combine compound wildcards with partial index filters:
db.orders.createIndex(
{
"customerId": 1,
"items.$**": 1
},
{
"partialFilterExpression": {
"status": { "$in": ["pending", "processing"] }
}
}
)
Query Optimization Patterns
Multi-Criteria Filtering
// Optimized query using compound wildcard index
db.products.find({
"category": "electronics",
"attributes.brand": "Samsung",
"attributes.color": "black"
})
// Index: { "category": 1, "attributes.$**": 1 }
Time-Series Data Optimization
// Efficient time-based queries with dynamic metadata
db.metrics.find({
"timestamp": { "$gte": ISODate("2025-01-01") },
"source": "api",
"tags.environment": "production"
})
// Index: { "timestamp": 1, "source": 1, "tags.$**": 1 }
Aggregation Pipeline Enhancement
db.analytics.aggregate([
{
"$match": {
"eventType": "user_action",
"properties.page": "/checkout"
}
},
{
"$group": {
"_id": "$properties.action",
"count": { "$sum": 1 }
}
}
])
// Index: { "eventType": 1, "properties.$**": 1 }
Performance Considerations
Index Size Management
Monitor index size and selectivity:
// Check index statistics
db.collection.aggregate([
{ "$indexStats": {} }
])
// Analyze index usage
db.collection.explain("executionStats").find({
"category": "books",
"metadata.author": "John Doe"
})
Memory Usage Optimization
// Configure index build options
db.collection.createIndex(
{
"type": 1,
"attributes.$**": 1
},
{
"background": true,
"maxTimeMS": 300000
}
)
Best Practices
Index Design Strategy
- Prefix Optimization: Place high-selectivity fields first
- Cardinality Analysis: Consider field value distribution
- Query Pattern Alignment: Match index structure to common queries
- Resource Monitoring: Track index performance metrics
Schema Design Considerations
// Optimal document structure for compound wildcards
{
"_id": ObjectId("..."),
"category": "electronics", // High selectivity prefix
"subcategory": "smartphones", // Secondary filter
"attributes": { // Dynamic fields
"brand": "Apple",
"model": "iPhone 15",
"storage": "256GB",
"color": "blue"
},
"metadata": {
"created": ISODate("..."),
"tags": ["premium", "5G"]
}
}
Monitoring and Maintenance
Performance Metrics
Further Reading: