Fix MongoDB performance issue for case-insensitive SCIM filtering #927
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
SCIM filtering on MongoDB was generating inefficient queries for case-insensitive string operations, causing severe performance degradation:
{ "Attribute.SchemaAttributeId": "26d51050-4962-4348-a6cb-310c198eeee3", "$expr": { "$eq": [ { "$toLower": { "$ifNull": ["$Attribute.ValueString", ""] } }, "150017355" ] } }This query pattern forced MongoDB into full collection scans because
$exprwith$toLowerand$ifNullfunctions prevent index usage, resulting in ~2.11 minutes execution time for 859k documents.Solution
Implemented MongoDB-specific optimizations that replace problematic
$exprqueries with index-friendly regex patterns for case-insensitive string operations.Key Changes
1. Optimized Expression Engine
MongoDbOptimizedExpressionExtensionsclass generates regex-based queries instead of$exprqueries^value$) that MongoDB can optimize with indexeseq), starts-with (sw), ends-with (ew), and contains (co)2. Enhanced Index Strategy
SchemaAttributeId_1_ValueString_1for efficient attribute filteringValueStringfield3. Selective Optimization
CaseExact = false)Performance Impact
The optimization specifically targets the problematic query pattern:
$exprfunctions → Full collection scan → Poor performanceExample Query Transformation
Testing
Backward Compatibility
✅ Fully backward compatible
The fix addresses the specific performance bottleneck reported in the issue while maintaining complete compatibility with existing SCIM implementations.
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.