summaryrefslogtreecommitdiff
path: root/jstests/core/bindata_indexonly.js
blob: d8f8bf931b941b36b3a3b239e0bb7db37acd130b (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
/**
 * This test ensures that range predicates with a BinData value:
 * 1) Return the correct documents.
 * 2) Can perform index-only data access.
 */
(function() {
    'use strict';

    load("jstests/libs/analyze_plan.js");

    var coll = db.jstests_bindata_indexonly;

    coll.drop();
    assert.writeOK(coll.insert({_id: BinData(0, "AAAAAAAAAAAAAAAAAAAAAAAAAAAA"), a: 1}));
    assert.writeOK(coll.insert({_id: BinData(0, "AQAAAAEBAAVlbl9VSwAAAAAAAAhv"), a: 2}));
    assert.writeOK(coll.insert({_id: BinData(0, "AQAAAAEBAAVlbl9VSwAAAAAAAAhz"), a: 3}));
    assert.writeOK(coll.insert({_id: BinData(0, "////////////////////////////"), a: 4}));
    assert.commandWorked(coll.createIndex({_id: 1, a: 1}));

    assert.throws(function() {
        db.mycoll.insert({_id: 0, a: BinData.prototype});
    }, [], "bindata getter did not fail");

    function testIndexOnlyBinData(blob) {
        var explain =
            coll.find({$and: [{_id: {$lte: BinData(0, blob)}}, {_id: {$gte: BinData(0, blob)}}]},
                      {_id: 1, a: 1})
                .hint({_id: 1, a: 1})
                .explain("executionStats");

        assert(isIndexOnly(explain.queryPlanner.winningPlan),
               "indexonly.BinData(0, " + blob + ") - must be index-only");
        assert.eq(1,
                  explain.executionStats.nReturned,
                  "EXACTone.BinData(0, " + blob + ") - should only return one in unique set");
    }

    testIndexOnlyBinData("AAAAAAAAAAAAAAAAAAAAAAAAAAAA");
    testIndexOnlyBinData("AQAAAAEBAAVlbl9VSwAAAAAAAAhv");
    testIndexOnlyBinData("AQAAAAEBAAVlbl9VSwAAAAAAAAhz");
    testIndexOnlyBinData("////////////////////////////");

    var explain;

    explain = coll.find({_id: {$lt: BinData(0, "AAAAAAAAAAAAAAAAAAAAAAAAAAAA")}}, {_id: 1, a: 1})
                  .hint({_id: 1, a: 1})
                  .explain("executionStats");
    assert(isIndexOnly(explain), "indexonly.$lt.1 - must be index-only");
    assert.eq(0,
              explain.executionStats.nReturned,
              "correctcount.$lt.1 - not returning correct documents");

    explain = coll.find({_id: {$gt: BinData(0, "////////////////////////////")}}, {_id: 1, a: 1})
                  .hint({_id: 1, a: 1})
                  .explain("executionStats");
    assert(isIndexOnly(explain), "indexonly.$gt.2 - must be index-only");
    assert.eq(0,
              explain.executionStats.nReturned,
              "correctcount.$gt.2 - not returning correct documents");

    explain = coll.find({_id: {$lte: BinData(0, "AQAAAAEBAAVlbl9VSwAAAAAAAAhv")}}, {_id: 1, a: 1})
                  .hint({_id: 1, a: 1})
                  .explain("executionStats");
    assert(isIndexOnly(explain), "indexonly.$lte.3 - must be index-only");
    assert.eq(2,
              explain.executionStats.nReturned,
              "correctcount.$lte.3 - not returning correct documents");

    explain = coll.find({_id: {$gte: BinData(0, "AQAAAAEBAAVlbl9VSwAAAAAAAAhz")}}, {_id: 1, a: 1})
                  .hint({_id: 1, a: 1})
                  .explain("executionStats");
    assert(isIndexOnly(explain), "indexonly.$gte.3 - must be index-only");
    assert.eq(2,
              explain.executionStats.nReturned,
              "correctcount.$gte.3 - not returning correct documents");

    coll.drop();
})();