diff options
| author | Lucas de Castro Borges <lucas@gnuabordo.com.br> | 2025-02-18 17:02:53 -0300 |
|---|---|---|
| committer | Lucas de Castro Borges <lucas@gnuabordo.com.br> | 2025-02-18 17:02:53 -0300 |
| commit | 959575a5ca598bf5f37fb5cebe7ed1d80d3d71f7 (patch) | |
| tree | acc8d60aedb12b70048e676e8a7349deb0010db8 /jstests/aggregation/unwind_sort.js | |
| parent | 76588293975fc059cf076779e4283e6ffaf8afff (diff) | |
New upstream version 6.0.20upstream
Diffstat (limited to 'jstests/aggregation/unwind_sort.js')
| -rw-r--r-- | jstests/aggregation/unwind_sort.js | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/jstests/aggregation/unwind_sort.js b/jstests/aggregation/unwind_sort.js new file mode 100644 index 00000000000..858320bec0d --- /dev/null +++ b/jstests/aggregation/unwind_sort.js @@ -0,0 +1,27 @@ +// Test that we can sort on fields, produces by unwind + +const coll = db.agg_unwind_sort; +coll.drop(); +assert.commandWorked(coll.insertOne({a: [3, 4, 5]})); +assert.commandWorked(coll.insertOne({a: [1, 2]})); + +let result = coll.aggregate([{$unwind: "$a"}, {$sort: {a: 1}}]).toArray(); +assert.eq([1, 2, 3, 4, 5], result.map(function(z) { + return z.a; +})); +result = coll.aggregate([{$unwind: "$a"}, {$sort: {a: -1}}]).toArray(); +assert.eq([5, 4, 3, 2, 1], result.map(function(z) { + return z.a; +})); +result = + coll.aggregate([{$unwind: {path: "$a", includeArrayIndex: "i"}}, {$sort: {i: 1}}]).toArray(); +assert.eq([NumberLong(0), NumberLong(0), NumberLong(1), NumberLong(1), NumberLong(2)], + result.map(function(z) { + return z.i; + })); +result = + coll.aggregate([{$unwind: {path: "$a", includeArrayIndex: "i"}}, {$sort: {i: -1}}]).toArray(); +assert.eq([NumberLong(2), NumberLong(1), NumberLong(1), NumberLong(0), NumberLong(0)], + result.map(function(z) { + return z.i; + })); |
