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
|
// SERVER-4588 Add option to $unwind to emit array index.
(function() {
"use strict";
var 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.
var actualResults = coll.aggregate([{$unwind: {path: "$x"}}]).toArray();
var 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"}}
]).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.
// TODO: update this test when SERVER-20168 is resolved.
actualResults = coll.aggregate([
{$unwind: {path: "$x", includeArrayIndex: "index", preserveNullAndEmptyArrays: true}}
]).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");
}());
|