What Is New in MongoDB 2.2
MongoDB 2.2 introduces significant enhancements across the database engine, concurrency, and tooling. This release focuses on improving performance for large-scale deployments and providing more granular control over operations.
| Category | Key Changes |
|---|---|
| New Features | Tag-Aware Sharding, TTL Collections, Concurrent Yielding, Aggregation Framework |
| Improvements | Working Set Memory Tracking, Database Profiler Enhancements, Better Index Building |
| Security | v8 JavaScript Engine, Improved Authentication |
| Tools | Enhanced mongorestore, mongostat, and bsondump |
How did concurrency improve in MongoDB 2.2?
The database kernel now uses a new locking system with reader-writer locks and a technique called concurrent yielding. This allows read operations to yield their locks during slow I/O, preventing them from blocking writes.
In practice, this means significantly better multi-core performance under mixed read/write workloads. The global lock is still present, but its negative impact is greatly reduced because operations aren't stuck holding it while waiting on disk.
What is the new Aggregation Framework?
This is a new native framework for performing aggregation tasks directly in the database, similar to SQL's GROUP BY. It uses a pipeline of operations to process and transform documents.
You build a pipeline with stages like $match, $group, $sort, and $project. This is a game-changer for analytics and data processing because it's faster and more flexible than using MapReduce for many common tasks.
db.orders.aggregate([
{ $match: { status: "A" } },
{ $group: { _id: "$cust_id", total: { $sum: "$amount" } } },
{ $sort: { total: -1 } }
])
How does Tag-Aware Sharding work?
This feature lets you control data placement across your sharded cluster by associating tags with shards and then defining ranges of shard key values that map to those tags.
This matters because you can now ensure that data for a specific geographic region or a specific application tenant lives only on shards in the desired location or with the appropriate hardware. It's the foundation for building multi-tenant architectures and meeting data locality requirements.
What are TTL Collections?
Time-To-Live (TTL) Collections automatically remove documents from a collection after a specified amount of time. You create a TTL index on a date field, and the database handles the expiration.
This is perfect for managing session data, logs, or any other transient information. It's far more efficient than writing your own application-level cron jobs to clean up old data.
// Documents will be removed 3600 seconds after the 'createdAt' value
db.eventlog.createIndex( { "createdAt": 1 }, { expireAfterSeconds: 3600 } )
What changed with the JavaScript engine?
MongoDB swapped the SpiderMonkey JavaScript engine for Google's V8 engine. The primary impact is on server-side scripting, like MapReduce and db.eval().
V8 is generally faster and has better performance characteristics. However, this is a breaking change. You must test any existing server-side JavaScript code because the engines have different semantics and support different extensions.
FAQ
Is the global lock finally gone in 2.2?
No, the global lock is still present. However, the new concurrent yielding mechanism dramatically reduces its contention. Operations now yield the lock during I/O waits, making the system feel much more concurrent in practice.
Should I use the Aggregation Framework instead of MapReduce now?
For most aggregation tasks like grouping, sorting, and filtering, yes. The Aggregation Framework is declarative and runs natively in C++, making it significantly faster than JavaScript-based MapReduce for these operations. Reserve MapReduce for complex custom processing that can't be expressed in the aggregation pipeline.
My MapReduce jobs broke after upgrading. What happened?
This is likely due to the switch from SpiderMonkey to the V8 JavaScript engine. The two engines have different behavior for certain edge cases and non-standard JavaScript. You'll need to review and test your MR code for compatibility with V8.
How do I start using TTL Collections?
Create a special index on a field that contains a date. Use the `expireAfterSeconds` option to define the time-to-live. The database's background thread will automatically purge documents older than the specified time interval from the indexed date.
What's the main use case for Tag-Aware Sharding?
Data locality. You can pin specific data to specific shards. This is crucial for ensuring data resides in a certain geographic region for compliance or latency reasons, or for isolating noisy tenants onto their own hardware in a multi-tenant system.