summaryrefslogtreecommitdiff
path: root/jstests/aggregation/bugs/server4588.js
blob: be04773c0ff9688dee668ff02827daafcb4d2c92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// SERVER-4588 Add option to $unwind to emit array index.
(function() {
"use strict";

const coll = db.server4588;
coll.drop();

assert.writeOK(coll.insert({_id: 0}));
assert.writeOK(coll.insert({_id: 1, x: null}));
assert.writeOK(coll.insert({_id: 2, x: []}));
assert.writeOK(coll.insert({_id: 3, x: [1, 2, 3]}));
assert.writeOK(coll.insert({_id: 4, x: 5}));

// Without includeArrayIndex.
let actualResults = coll.aggregate([{$unwind: {path: "$x"}}, {$sort: {_id: 1, x: 1}}]).toArray();
let expectedResults = [
    {_id: 3, x: 1},
    {_id: 3, x: 2},
    {_id: 3, x: 3},
    {_id: 4, x: 5},
];
assert.eq(expectedResults, actualResults, "Incorrect results for normal $unwind");

// With includeArrayIndex, index inserted into a new field.
actualResults =
    coll.aggregate([{$unwind: {path: "$x", includeArrayIndex: "index"}}, {$sort: {_id: 1, x: 1}}])
        .toArray();
expectedResults = [
    {_id: 3, x: 1, index: NumberLong(0)},
    {_id: 3, x: 2, index: NumberLong(1)},
    {_id: 3, x: 3, index: NumberLong(2)},
    {_id: 4, x: 5, index: null},
];
assert.eq(expectedResults, actualResults, "Incorrect results $unwind with includeArrayIndex");

// With both includeArrayIndex and preserveNullAndEmptyArrays.
actualResults =
    coll.aggregate([
            {$unwind: {path: "$x", includeArrayIndex: "index", preserveNullAndEmptyArrays: true}},
            {$sort: {_id: 1, x: 1}}
        ])
        .toArray();
expectedResults = [
    {_id: 0, index: null},
    {_id: 1, x: null, index: null},
    {_id: 2, index: null},
    {_id: 3, x: 1, index: NumberLong(0)},
    {_id: 3, x: 2, index: NumberLong(1)},
    {_id: 3, x: 3, index: NumberLong(2)},
    {_id: 4, x: 5, index: null},
];
assert.eq(expectedResults,
          actualResults,
          "Incorrect results $unwind with includeArrayIndex and preserveNullAndEmptyArrays");
}());