summaryrefslogtreecommitdiff
path: root/jstests/core/wildcard_index_validindex.js
blob: f647bbcc969a1c52b332413fbeac675e3e64c5ee (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
 * Tests parsing and validation of wildcard indexes.
 * @tags: [
 *  # Uses index building in background
 *  requires_background_index,
 * ]
 */
(function() {
"use strict";

const kCollectionName = "wildcard_validindex";
const coll = db.getCollection(kCollectionName);

const kIndexName = "wildcard_validindex";

const createIndexHelper = function(key, parameters) {
    return db.runCommand(
        {createIndexes: kCollectionName, indexes: [Object.assign({key: key}, parameters)]});
};

const createIndexAndVerifyWithDrop = function(key, parameters) {
    coll.dropIndexes();
    createIndexHelper(key, parameters);
    assert.eq(coll.getIndexes()
                  .filter((index) => {
                      return index.name == parameters.name;
                  })
                  .length,
              1);
};

// Can create a valid wildcard index.
createIndexAndVerifyWithDrop({"$**": 1}, {name: kIndexName});

// Can create a valid wildcard index with subpaths.
createIndexAndVerifyWithDrop({"a.$**": 1}, {name: kIndexName});

// Can create a wildcard index with partialFilterExpression.
createIndexAndVerifyWithDrop({"$**": 1},
                             {name: kIndexName, partialFilterExpression: {a: {"$gt": 0}}});

// Can create a wildcard index with foreground & background construction.
createIndexAndVerifyWithDrop({"$**": 1}, {background: false, name: kIndexName});
createIndexAndVerifyWithDrop({"$**": 1}, {background: true, name: kIndexName});

// Can create a wildcard index with index level collation.
createIndexAndVerifyWithDrop({"$**": 1}, {collation: {locale: "fr"}, name: kIndexName});

// Can create a wildcard index with an inclusion projection.
createIndexAndVerifyWithDrop({"$**": 1},
                             {wildcardProjection: {a: 1, b: 1, c: 1}, name: kIndexName});
// Can create a wildcard index with an exclusion projection.
createIndexAndVerifyWithDrop({"$**": 1},
                             {wildcardProjection: {a: 0, b: 0, c: 0}, name: kIndexName});
// Can include _id in an exclusion.
createIndexAndVerifyWithDrop({"$**": 1},
                             {wildcardProjection: {_id: 1, a: 0, b: 0, c: 0}, name: kIndexName});
// Can exclude _id in an exclusion.
createIndexAndVerifyWithDrop({"$**": 1},
                             {wildcardProjection: {_id: 0, a: 1, b: 1, c: 1}, name: kIndexName});

// Cannot create a wildcard index with a non-positive numeric key value.
coll.dropIndexes();
assert.commandFailedWithCode(coll.createIndex({"$**": 0}), ErrorCodes.CannotCreateIndex);
assert.commandFailedWithCode(coll.createIndex({"$**": -1}), ErrorCodes.CannotCreateIndex);
assert.commandFailedWithCode(coll.createIndex({"$**": -2}), ErrorCodes.CannotCreateIndex);

// Cannot create a wildcard index with sparse option.
assert.commandFailedWithCode(coll.createIndex({"$**": 1}, {sparse: true}),
                             ErrorCodes.CannotCreateIndex);

// Cannot create a wildcard index with a v0 or v1 index.
assert.commandFailedWithCode(coll.createIndex({"$**": 1}, {v: 0}), ErrorCodes.CannotCreateIndex);
assert.commandFailedWithCode(coll.createIndex({"$**": 1}, {v: 1}), ErrorCodes.CannotCreateIndex);

// Cannot create a unique index.
assert.commandFailedWithCode(coll.createIndex({"$**": 1}, {unique: true}),
                             ErrorCodes.CannotCreateIndex);

// Cannot create a hashed wildcard index.
assert.commandFailedWithCode(coll.createIndex({"$**": "hashed"}), ErrorCodes.CannotCreateIndex);

// Cannot create a TTL wildcard index.
assert.commandFailedWithCode(coll.createIndex({"$**": 1}, {expireAfterSeconds: 3600}),
                             ErrorCodes.CannotCreateIndex);

// Cannot create a geoSpatial wildcard index.
assert.commandFailedWithCode(coll.createIndex({"$**": "2dsphere"}), ErrorCodes.CannotCreateIndex);
assert.commandFailedWithCode(coll.createIndex({"$**": "2d"}), ErrorCodes.CannotCreateIndex);

// Cannot create a text wildcard index using single sub-path syntax.
assert.commandFailedWithCode(coll.createIndex({"a.$**": "text"}), ErrorCodes.CannotCreateIndex);

// Cannot specify plugin by string.
assert.commandFailedWithCode(coll.createIndex({"a": "wildcard"}), ErrorCodes.CannotCreateIndex);
assert.commandFailedWithCode(coll.createIndex({"$**": "wildcard"}), ErrorCodes.CannotCreateIndex);

// Cannot create a compound wildcard index.
assert.commandFailedWithCode(coll.createIndex({"$**": 1, "a": 1}), ErrorCodes.CannotCreateIndex);
assert.commandFailedWithCode(coll.createIndex({"a": 1, "$**": 1}), ErrorCodes.CannotCreateIndex);

// Cannot create an wildcard index with an invalid spec.
assert.commandFailedWithCode(coll.createIndex({"a.$**.$**": 1}), ErrorCodes.CannotCreateIndex);
assert.commandFailedWithCode(coll.createIndex({"$**.$**": 1}), ErrorCodes.CannotCreateIndex);
assert.commandFailedWithCode(coll.createIndex({"$**": "hello"}), ErrorCodes.CannotCreateIndex);

// Cannot create an wildcard index with mixed inclusion exclusion.
assert.commandFailedWithCode(
    createIndexHelper({"$**": 1}, {name: kIndexName, wildcardProjection: {a: 1, b: 0}}), 40178);
// Cannot create an wildcard index with computed fields.
assert.commandFailedWithCode(
    createIndexHelper({"$**": 1}, {name: kIndexName, wildcardProjection: {a: 1, b: "string"}}),
    ErrorCodes.FailedToParse);
// Cannot create an wildcard index with an empty projection.
assert.commandFailedWithCode(
    createIndexHelper({"$**": 1}, {name: kIndexName, wildcardProjection: {}}),
    ErrorCodes.FailedToParse);
// Cannot create another index type with "wildcardProjection" projection.
assert.commandFailedWithCode(
    createIndexHelper({"a": 1}, {name: kIndexName, wildcardProjection: {a: 1, b: 1}}),
    ErrorCodes.BadValue);
// Cannot create a text index with a "wildcardProjection" projection.
assert.commandFailedWithCode(
    createIndexHelper({"$**": "text"}, {name: kIndexName, wildcardProjection: {a: 1, b: 1}}),
    ErrorCodes.BadValue);
// Cannot create an wildcard index with a non-object "wildcardProjection" projection.
assert.commandFailedWithCode(
    createIndexHelper({"a.$**": 1}, {name: kIndexName, wildcardProjection: "string"}),
    ErrorCodes.TypeMismatch);
// Cannot exclude an subfield of _id in an inclusion.
assert.commandFailedWithCode(createIndexHelper({"_id.id": 0, a: 1, b: 1, c: 1}),
                             ErrorCodes.CannotCreateIndex);
// Cannot include an subfield of _id in an exclusion.
assert.commandFailedWithCode(createIndexHelper({"_id.id": 1, a: 0, b: 0, c: 0}),
                             ErrorCodes.CannotCreateIndex);

// Cannot specify both a subpath and a projection.
assert.commandFailedWithCode(
    createIndexHelper({"a.$**": 1}, {name: kIndexName, wildcardProjection: {a: 1}}),
    ErrorCodes.FailedToParse);
assert.commandFailedWithCode(
    createIndexHelper({"a.$**": 1}, {name: kIndexName, wildcardProjection: {b: 0}}),
    ErrorCodes.FailedToParse);
})();