summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/index_partial_no_explain_cmds.js
blob: 10cdaf932d152aa57b08390b8d75bf77ecd68e20 (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
// Test partial indexes with commands that don't use explain.  These commands are tested against
// mongod with the --notablescan flag set, so that they fail if the index is not used.
(function() {
    "use strict";
    var runner = MongoRunner.runMongod({setParameter: "notablescan=1"});
    var coll = runner.getDB("test").index_partial_no_explain_cmds;
    var ret;

    coll.drop();

    assert.commandWorked(coll.ensureIndex({x: 1}, {partialFilterExpression: {a: 1}}));

    assert.writeOK(coll.insert({_id: 1, x: 5, a: 2})); // Not in index.
    assert.writeOK(coll.insert({_id: 2, x: 6, a: 1})); // In index.

    // Verify we will throw if the partial index can't be used.
    assert.throws(function() { coll.find({x: {$gt: 1}, a: 2}).itcount(); });

    //
    // Test mapReduce.
    //

    var mapFunc = function() { emit(this._id, 1); };
    var reduceFunc = function (keyId, countArray) { return Array.sum(countArray); };

    ret = coll.mapReduce(mapFunc, reduceFunc, {out: "inline", query: {x: {$gt: 1}, a: 1}});
    assert.eq(1, ret.counts.input);

    //
    // Test distinct.
    //

    ret = coll.distinct("a", {x: {$gt: 1}, a: 1});
    assert.eq(1, ret.length);
    ret = coll.distinct("x", {x: {$gt: 1}, a: 1});
    assert.eq(1, ret.length);
    assert.throws(function() { printjson(coll.distinct("a", {a: 0})); });
    assert.throws(function() { printjson(coll.distinct("x", {a: 0})); });

    // SERVER-19511 regression test: distinct with no query predicate should return the correct
    // number of results.  This query should not be allowed to use the partial index, so it should
    // use a collection scan instead.  Although this test enables --notablescan, this does not cause
    // operations to fail if they have no query predicate.
    ret = coll.distinct("x");
    assert.eq(2, ret.length);
})();