summaryrefslogtreecommitdiff
path: root/jstests/core/distinct_array1.js
blob: 2d3a96a6af69f1a8dc8f506fd790cacca42e77fd (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
t = db.distinct_array1;
t.drop();

t.save({a: [1, 2, 3]});
t.save({a: [2, 3, 4]});
t.save({a: [3, 4, 5]});
t.save({a: 9});

// Without index.
res = t.distinct("a").sort();
assert.eq("1,2,3,4,5,9", res.toString());

// Array element 0 without index.
res = t.distinct("a.0").sort();
assert.eq("1,2,3", res.toString());

// Array element 1 without index.
res = t.distinct("a.1").sort();
assert.eq("2,3,4", res.toString());

// With index.
t.ensureIndex({a: 1});
res = t.distinct("a").sort();
assert.eq("1,2,3,4,5,9", res.toString());

// Array element 0 with index.
res = t.distinct("a.0").sort();
assert.eq("1,2,3", res.toString());

// Array element 1 with index.
res = t.distinct("a.1").sort();
assert.eq("2,3,4", res.toString());

t.save({a: [{b: "a"}, {b: "d"}], c: 12});
t.save({a: [{b: "b"}, {b: "d"}], c: 12});
t.save({a: [{b: "c"}, {b: "e"}], c: 12});
t.save({a: [{b: "c"}, {b: "f"}], c: 12});
t.save({a: [], c: 12});
t.save({a: {b: "z"}, c: 12});

// Without index.
res = t.distinct("a.b").sort();
assert.eq("a,b,c,d,e,f,z", res.toString());

// Array element 0 without index
res = t.distinct("a.0.b").sort();
assert.eq("a,b,c", res.toString());

// Array element 1 without index
res = t.distinct("a.1.b").sort();
assert.eq("d,e,f", res.toString());

// With index.
t.ensureIndex({"a.b": 1});
res = t.distinct("a.b");
res.sort();
// TODO SERVER-14832 The presence of an index may change results, but only if the index is not
// multikey.
// In a sharded scenario, an unlucky distribution of data will cause all the arrays to go to one
// shard, and one shard be left with only non-multikey documents, including one with {a: 9}, which
// will generate a null result.
if (res.includes(null)) {
    // The default sorting of an array is by string, so null will appear in an odd position.
    assert.eq(res, ["a", "b", "c", "d", "e", "f", null, "z"]);
} else {
    assert.eq("a,b,c,d,e,f,z", res.toString());
}

// _id as an document containing an array
t.save({_id: {a: [1, 2, 3]}});
t.save({_id: {a: [2, 3, 4]}});
t.save({_id: {a: [3, 4, 5]}});
t.save({_id: {a: 9}});

// Without index.
res = t.distinct("_id.a").sort();
assert.eq("1,2,3,4,5,9", res.toString());

// Array element 0 without index.
res = t.distinct("_id.a.0").sort();
assert.eq("1,2,3", res.toString());

// Array element 1 without index.
res = t.distinct("_id.a.1").sort();
assert.eq("2,3,4", res.toString());

// With index.
t.ensureIndex({"_id.a": 1});
res = t.distinct("_id.a").sort();
// TODO SERVER-14832: distinct() may incorrectly return null in presence of index.
assert.eq([1, 2, 3, 4, 5, 9, null], res);

// Array element 0 with index.
res = t.distinct("_id.a.0").sort();
assert.eq("1,2,3", res.toString());

// Array element 1 with index.
res = t.distinct("_id.a.1").sort();
assert.eq("2,3,4", res.toString());