What Is New in MongoDB 4.5
MongoDB 4.5 delivers a set of enhancements focused on improving developer productivity and operational resilience. The key updates include new aggregation operators, refinements to sharding, and significant improvements to change streams.
| Category | Key Changes |
|---|---|
| Aggregation Framework | New operators: $getField, $setField, $rand, $sampleRate |
| Sharding | Refinable shard keys, hedged reads for reduced latency |
| Change Streams | Support for new aggregation stages, resume tokens as documents |
| Security | Key Management Interoperability, user-defined roles for $external |
| Operational Improvements | Faster time series initial sync, new currentOp and killOp commands |
How did the aggregation framework get more powerful?
MongoDB 4.5 introduced several new operators that make complex data transformations simpler. The $getField and $setField operators allow for dynamic field access and manipulation based on the value of another field, which is huge for handling documents with variable schemas.
The $rand operator provides a random value between 0 and 1, and $sampleRate offers a performant way to randomly select a percentage of documents. In practice, this is much more efficient than using $sample on large collections when you just need a statistical subset.
What sharding improvements should I know about?
Refinable shard keys are a game-changer for managing data growth. You can now add a suffix to your existing shard key, which helps redistribute data more evenly without the pain of a full resharding operation. This matters because it addresses the common issue of jumbo chunks that can cause balancing headaches.
Hedged reads are another big win for latency-sensitive applications. They allow a mongos to send a read operation to two shards simultaneously and return the result from whichever replies first. This smooths out performance when individual shard replicas might be slow.
How are change streams better now?
Change streams now support all aggregation pipeline stages, not just a limited few. This means you can filter, transform, and reshape the change events right as you receive them, reducing the amount of application-side processing needed.
Resume tokens are now full BSON documents instead of strings. This makes them more reliable for long-running consumers, as the new format is future-proofed against internal changes. You will need to update any code that manually handles resume tokens.
Were there any security updates?
Yes, the Key Management Interoperability Protocol (KMIP) is now generally available. This provides a standardized way to integrate with external enterprise key management services, which is essential for shops with strict security requirements.
You can also now create user-defined roles for users authenticated via external mechanisms (like LDAP). This gives admins more granular control over permissions for these users, closing a previous functionality gap.
FAQ
Can I change my shard key after starting?
Yes, with refinable shard keys. You can't change the entire key, but you can add new fields as a suffix to refine the distribution of your data and fix hotspots.
What's the main use case for $getField and $setField?
They are perfect for documents where the field names themselves are dynamic or stored as values in other fields, a common pattern in event-sourcing or flexible schema designs.
Do I need to change my change stream consumer code?
If you hardcoded logic that parses the old string-based resume token, then yes. The new document-based tokens are a breaking change for manual token handling.
Is $sampleRate faster than $sample?
Absolutely. $sampleRate is a lightweight probabilistic filter, while $sample does a more expensive random cursor selection. Use $sampleRate for a quick, random percentage of docs.
What's the benefit of hedged reads?
They reduce tail latency for read operations in a sharded cluster. By sending requests to multiple replicas, you get the result from the fastest one, making application performance more consistent.