2012-07-13 14:57:59 -07:00
|
|
|
// $unwind applied to an empty array field drops the field from the source document. SERVER-6131
|
|
|
|
|
|
|
|
|
|
t = db.jstests_aggregation_server6131;
|
|
|
|
|
t.drop();
|
|
|
|
|
|
2016-03-09 12:17:50 -05:00
|
|
|
function assertAggregationResults(expected, aggregation) {
|
2013-10-08 13:29:04 -04:00
|
|
|
assert.eq(expected, t.aggregate(aggregation).toArray());
|
2012-07-13 14:57:59 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t.drop();
|
|
|
|
|
|
|
|
|
|
// An empty array document is dropped.
|
2016-03-09 12:17:50 -05:00
|
|
|
t.save({_id: 0, a: 1, b: [], c: 2});
|
2016-05-28 17:55:12 -04:00
|
|
|
assertAggregationResults([], {$unwind: '$b'});
|
2012-07-13 14:57:59 -07:00
|
|
|
|
|
|
|
|
// Values from a nonempty array in another document are unwound.
|
2016-03-09 12:17:50 -05:00
|
|
|
t.save({_id: 1, b: [4, 5]});
|
|
|
|
|
assertAggregationResults([{_id: 1, b: 4}, {_id: 1, b: 5}], {$unwind: '$b'});
|
2012-07-13 14:57:59 -07:00
|
|
|
|
|
|
|
|
// Another empty array document is dropped.
|
2016-03-09 12:17:50 -05:00
|
|
|
t.save({_id: 2, b: []});
|
|
|
|
|
assertAggregationResults([{_id: 1, b: 4}, {_id: 1, b: 5}], {$unwind: '$b'});
|
2012-07-13 14:57:59 -07:00
|
|
|
|
|
|
|
|
t.drop();
|
|
|
|
|
|
|
|
|
|
// A nested empty array document is dropped.
|
2016-03-09 12:17:50 -05:00
|
|
|
t.save({_id: 0, a: 1, b: {x: 10, y: [], z: 20}, c: 2});
|
2016-05-28 17:55:12 -04:00
|
|
|
assertAggregationResults([], {$unwind: '$b.y'});
|
2012-07-13 14:57:59 -07:00
|
|
|
|
|
|
|
|
t.drop();
|
|
|
|
|
|
|
|
|
|
// A null value document is dropped.
|
2016-03-09 12:17:50 -05:00
|
|
|
t.save({_id: 0, a: 1, b: null, c: 2});
|
2016-05-28 17:55:12 -04:00
|
|
|
assertAggregationResults([], {$unwind: '$b'});
|
2012-07-13 14:57:59 -07:00
|
|
|
|
|
|
|
|
t.drop();
|
|
|
|
|
|
|
|
|
|
// A missing value causes the document to be dropped.
|
2016-03-09 12:17:50 -05:00
|
|
|
t.save({_id: 0, a: 1, c: 2});
|
2016-05-28 17:55:12 -04:00
|
|
|
assertAggregationResults([], {$unwind: '$b'});
|
2012-07-13 14:57:59 -07:00
|
|
|
|
|
|
|
|
t.drop();
|
|
|
|
|
|
|
|
|
|
// A missing value in an existing nested object causes the document to be dropped.
|
2016-03-09 12:17:50 -05:00
|
|
|
t.save({_id: 0, a: 1, b: {d: 4}, c: 2});
|
2016-05-28 17:55:12 -04:00
|
|
|
assertAggregationResults([], {$unwind: '$b.y'});
|
2012-07-13 14:57:59 -07:00
|
|
|
|
|
|
|
|
t.drop();
|
|
|
|
|
|
|
|
|
|
// A missing value in a missing nested object causes the document to be dropped.
|
2016-03-09 12:17:50 -05:00
|
|
|
t.save({_id: 0, a: 1, b: 10, c: 2});
|
2016-05-28 17:55:12 -04:00
|
|
|
assertAggregationResults([], {$unwind: '$b.y'});
|