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
|
// Test different directions for compound indexes
function eq(one, two) {
assert.eq(one.a, two.a);
assert.eq(one.b, two.b);
}
function check(indexed) {
var hint;
if (indexed) {
hint = {a: 1, b: -1};
} else {
hint = {$natural: 1};
}
f = r.find().sort({a: 1, b: 1}).hint(hint);
eq(z[0], f[0]);
eq(z[1], f[1]);
eq(z[2], f[2]);
eq(z[3], f[3]);
f = r.find().sort({a: 1, b: -1}).hint(hint);
eq(z[1], f[0]);
eq(z[0], f[1]);
eq(z[3], f[2]);
eq(z[2], f[3]);
f = r.find().sort({a: -1, b: 1}).hint(hint);
eq(z[2], f[0]);
eq(z[3], f[1]);
eq(z[0], f[2]);
eq(z[1], f[3]);
f = r.find({a: {$gte: 2}}).sort({a: 1, b: -1}).hint(hint);
eq(z[3], f[0]);
eq(z[2], f[1]);
f = r.find({a: {$gte: 2}}).sort({a: -1, b: 1}).hint(hint);
eq(z[2], f[0]);
eq(z[3], f[1]);
f = r.find({a: {$gte: 2}}).sort({a: 1, b: 1}).hint(hint);
eq(z[2], f[0]);
eq(z[3], f[1]);
f = r.find().sort({a: -1, b: -1}).hint(hint);
eq(z[3], f[0]);
eq(z[2], f[1]);
eq(z[1], f[2]);
eq(z[0], f[3]);
}
r = db.ed_db_cursor6;
r.drop();
z = [{a: 1, b: 1}, {a: 1, b: 2}, {a: 2, b: 1}, {a: 2, b: 2}];
for (i = 0; i < z.length; ++i)
r.save(z[i]);
r.createIndex({a: 1, b: -1});
check(false);
check(true);
|