-
Type: Bug
-
Resolution: Done
-
Priority: Major - P3
-
Affects Version/s: None
-
Component/s: Aggregation Framework
-
None
-
ALL
The aggregation framework appears to distribute sorts to each shard and return sorted results from each shard sequentially, without ensuring that the combined result set is sorted.
Group, in contrast to sort, seems to be handled specifically within mongos.
Test:
// Set up a sharding test. s = new ShardingTest( "aggregation_sort1", 2, 0, 2 ); s.adminCommand( { enablesharding:"test" } ); s.adminCommand( { shardcollection:"test.data", key:{ _id:1 } } ); db = s.getDB( "test" ); // Insert _id values 0 - 99. N = 100; for( i = 0; i < N; ++i ) { db.data.insert( { _id:i } ) } db.getLastError(); // Split the data into 3 chunks. s.adminCommand( { split:"test.data", middle:{ _id:33 } } ); s.adminCommand( { split:"test.data", middle:{ _id:66 } } ); // Migrate the middle chunk to another shard. s.adminCommand( { movechunk:"test.data", find:{ _id:50 }, to:s.getOther( s.getServer( "test" ) ).name } ); // Check that the results are in order. result = db.data.aggregate( { $sort: { _id:1 } } ).result; for( i = 0; i < N; ++i ) { assert.eq( i, result[ i ]._id ); }
Observed behavior: The results of a sharded sort aggregation may not be properly sorted.
Expected behavior: A merge sort is performed by mongos to sort the results.