summaryrefslogtreecommitdiff
path: root/jstests/core/or1.js
blob: e7c417800b66f18627435c43fbe0296cdbf3e6fd (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
t = db.jstests_or1;
t.drop();

checkArrs = function(a, b) {
    assert.eq(a.length, b.length);
    aStr = [];
    bStr = [];
    a.forEach(function(x) {
        aStr.push(tojson(x));
    });
    b.forEach(function(x) {
        bStr.push(tojson(x));
    });
    for (i = 0; i < aStr.length; ++i) {
        assert.neq(-1, bStr.indexOf(aStr[i]));
    }
};

doTest = function() {

    t.save({_id: 0, a: 1});
    t.save({_id: 1, a: 2});
    t.save({_id: 2, b: 1});
    t.save({_id: 3, b: 2});
    t.save({_id: 4, a: 1, b: 1});
    t.save({_id: 5, a: 1, b: 2});
    t.save({_id: 6, a: 2, b: 1});
    t.save({_id: 7, a: 2, b: 2});

    assert.throws(function() {
        t.find({$or: "a"}).toArray();
    });
    assert.throws(function() {
        t.find({$or: []}).toArray();
    });
    assert.throws(function() {
        t.find({$or: ["a"]}).toArray();
    });

    a1 = t.find({$or: [{a: 1}]}).toArray();
    checkArrs([{_id: 0, a: 1}, {_id: 4, a: 1, b: 1}, {_id: 5, a: 1, b: 2}], a1);

    a1b2 = t.find({$or: [{a: 1}, {b: 2}]}).toArray();
    checkArrs(
        [
          {_id: 0, a: 1},
          {_id: 3, b: 2},
          {_id: 4, a: 1, b: 1},
          {_id: 5, a: 1, b: 2},
          {_id: 7, a: 2, b: 2}
        ],
        a1b2);

    t.drop();
    t.save({a: [0, 1], b: [0, 1]});
    assert.eq(1, t.find({$or: [{a: {$in: [0, 1]}}]}).toArray().length);
    assert.eq(1, t.find({$or: [{b: {$in: [0, 1]}}]}).toArray().length);
    assert.eq(1, t.find({$or: [{a: {$in: [0, 1]}}, {b: {$in: [0, 1]}}]}).toArray().length);

};

doTest();

// not part of SERVER-1003, but good check for subseq. implementations
t.drop();
t.ensureIndex({a: 1});
doTest();

t.drop();
t.ensureIndex({b: 1});
doTest();

t.drop();
t.ensureIndex({a: 1, b: 1});
doTest();