-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathcompound_wildcard_index_validation.js
More file actions
184 lines (161 loc) · 8.91 KB
/
Copy pathcompound_wildcard_index_validation.js
File metadata and controls
184 lines (161 loc) · 8.91 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* Tests that compound wildcard indexes can be created. The specification of a compound wildcard
* index should be validated correctly. This test also tests that CWI can be validated by validate
* command.
*
* @tags: [
* does_not_support_stepdowns,
* featureFlagCompoundWildcardIndexes,
* requires_fcv_70,
* uses_full_validation,
* ]
*/
(function() {
"use strict";
const coll = db.compound_wildcard_index_validation;
coll.drop();
// Tests that a subtree-indexing compound wildcard index can be created.
assert.commandWorked(coll.createIndex({a: 1, "b.$**": 1}));
assert.commandWorked(coll.createIndex({"a.$**": 1, b: 1}));
assert.commandWorked(coll.createIndex({a: 1, "b.$**": -1, c: 1}));
// Tests that an all-indexing compound wildcard index can be created only if 'wildcardProjection'
// is specified.
assert.commandWorked(coll.createIndex({a: 1, "$**": 1}, {"wildcardProjection": {a: 0}}));
assert.commandWorked(coll.createIndex({"$**": 1, a: 1}, {"wildcardProjection": {a: 0}}));
assert.commandWorked(
coll.createIndex({a: 1, "$**": -1, b: 1}, {"wildcardProjection": {a: 0, b: 0}}));
// Tests that _id can be excluded in an inclusion projection statement.
assert.commandWorked(
coll.createIndex({"$**": 1, "other": 1}, {"wildcardProjection": {"_id": 0, "a": 1}}));
// Tests that _id can be included in an exclusion projection statement.
assert.commandWorked(coll.createIndex({"$**": 1, "another": 1},
{"wildcardProjection": {"_id": 1, "a": 0, "another": 0}}));
assert.commandWorked(
coll.createIndex({"$**": 1, "yetAnother": 1}, {"wildcardProjection": {"_id": 1}}));
// Tests wildcard projections allow nested objects.
assert.commandWorked(
coll.createIndex({"$**": 1, "d": 1}, {"wildcardProjection": {"a": {"b": 1, "c": 1}}}));
//
// Invalid CWI specification.
//
// Tests that collision is not allowed.
assert.commandFailedWithCode(coll.createIndex({a: 1, "a.$**": 1}), 7246204);
assert.commandFailedWithCode(coll.createIndex({"a.b.c": 1, "a.b.$**": 1}), 7246204);
assert.commandFailedWithCode(
coll.createIndex({"$**": 1, c: 1}, {"wildcardProjection": {"a.b": 1, "a.b.c": 1}}), 7246200);
// Tests that only one wildcard component is allowed.
assert.commandFailedWithCode(coll.createIndex({"a.$**": 1, "b.$**": 1}), 7246201);
// Tests that a compound wildcard index cannot contain other special type of index.
assert.commandFailedWithCode(coll.createIndex({"a.$**": 1, b: "text"}), 67);
assert.commandFailedWithCode(coll.createIndex({"a.$**": 1, b: "hashed"}), 67);
assert.commandFailedWithCode(coll.createIndex({"a.$**": 1, b: "2dsphere"}), 67);
// Tests that unsupported property cannot be specified.
assert.commandFailedWithCode(coll.createIndex({"a.$**": 1, b: 1}, {unique: true}), 67);
assert.commandFailedWithCode(coll.createIndex({"a.$**": 1, b: 1}, {sparse: true}), 67);
assert.commandFailedWithCode(coll.createIndex({"a.$**": 1, b: 1}, {expireAfterSeconds: 60}), 67);
// Tests that createIndex creating a compound wildcard index on all fields failed if
// 'wildcardProjection' is not specified.
assert.commandFailedWithCode(coll.createIndex({a: 1, "$**": 1}), 67);
// Tests that the wildcardProjection cannot be empty.
assert.commandFailedWithCode(coll.createIndex({a: 1, "$**": 1}, {wildcardProjection: {}}),
ErrorCodes.FailedToParse);
// Tests that a wildcardProjection is not allowed for non-wildcard indexes.
assert.commandFailedWithCode(coll.createIndex({a: 1, b: 1}, {wildcardProjection: {c: 1}}),
ErrorCodes.BadValue);
// Tests that wildcardProjection cannot include regular index fields.
assert.commandFailedWithCode(coll.createIndex({a: 1, "$**": 1}, {wildcardProjection: {a: 1}}),
7246208);
assert.commandFailedWithCode(coll.createIndex({a: 1, "$**": 1}, {wildcardProjection: {a: 1, b: 1}}),
7246208);
assert.commandFailedWithCode(
coll.createIndex({a: 1, "$**": 1, b: 1}, {wildcardProjection: {a: 1, b: 1}}), 7246208);
assert.commandFailedWithCode(coll.createIndex({_id: 1, "$**": 1}, {wildcardProjection: {_id: 1}}),
7246208);
// Tests that a wildcardProjection can only mix inclusion/exclusion projections with _id.
assert.commandFailedWithCode(
coll.createIndex({"$**": 1, c: 1}, {wildcardProjection: {_id: 0, a: 0, b: 1}}), 7246211);
assert.commandFailedWithCode(
coll.createIndex({"$**": 1, c: 1}, {wildcardProjection: {_id: 1, a: 0, b: 1}}), 7246211);
assert.commandWorked(coll.createIndex({"$**": 1, "c": 1}, {wildcardProjection: {"_id": 0, b: 1}}));
assert.commandWorked(coll.createIndex({"$**": 1, "e": 1}, {wildcardProjection: {"_id": 1, e: 0}}));
assert.commandFailedWithCode(
coll.createIndex({"$**": 1, "_id": 1, "e": 1}, {wildcardProjection: {"_id": 1, e: 0}}),
7246209,
);
assert.commandWorked(
coll.createIndex({"$**": 1, "_id": 1, "e": 1}, {wildcardProjection: {"_id": 0, a: 1}}));
// Tests that wildcard projections accept only numeric values.
assert.commandFailedWithCode(
coll.createIndex({"st": 1, "$**": 1}, {wildcardProjection: {"a": "something"}}), 51271);
// Tests that just excluding _id is not valid in the wildcardProjection, unless the regular part is
// _id.
assert.commandFailedWithCode(coll.createIndex({"a": 1, "$**": 1}, {wildcardProjection: {"_id": 0}}),
7246210);
assert.commandFailedWithCode(coll.createIndex({"$**": 1, "a": 1}, {wildcardProjection: {"_id": 0}}),
7246210);
assert.commandFailedWithCode(
coll.createIndex({"b": 1, "$**": 1, "a": 1}, {wildcardProjection: {"_id": 0}}), 7246210);
assert.commandWorked(coll.createIndex({"_id": 1, "$**": 1}, {wildcardProjection: {"_id": 0}}));
assert.commandWorked(coll.createIndex({"$**": 1, "_id": 1}, {wildcardProjection: {"_id": 0}}));
assert.commandFailedWithCode(
coll.createIndex({"_id": 1, "$**": 1, "a": 1}, {wildcardProjection: {"_id": 0}}), 7246210);
// Tests that all compound wildcard indexes in the catalog can be validated by running validate()
// command.
// Create more wildcard indexes with various forms.
assert.commandWorked(coll.createIndex({a: 1, "b.$**": 1, str: 1}));
assert.commandWorked(coll.createIndex({"b.$**": 1, str: 1}));
assert.commandWorked(coll.createIndex({a: 1, "b.$**": 1}));
assert.commandWorked(coll.createIndex({"$**": 1}));
assert.commandWorked(
coll.createIndex({"b": 1, "$**": 1}, {wildcardProjection: {"_id": 0, "a": 1}}));
assert.commandWorked(
coll.createIndex({"_id": 1, "a": 1, "$**": 1}, {wildcardProjection: {"_id": 0, "a": 0}}));
assert.commandFailedWithCode(
coll.createIndex({"a": 1, "_id": 1, "$**": 1}, {wildcardProjection: {"a": 0}}), 7246209);
assert.commandFailedWithCode(
coll.createIndex({"a": 1, "b": 1, "$**": 1}, {wildcardProjection: {"b": 0}}), 7246209);
assert.commandWorked(
coll.createIndex({"a": 1, "$**": 1}, {wildcardProjection: {"_id": 0, "a": 0}}));
assert.commandWorked(
coll.createIndex({"e": 1, "$**": 1}, {wildcardProjection: {"_id": 1, "f": 1}}));
assert.commandFailedWithCode(
coll.createIndex({"b": 1, "$**": 1}, {wildcardProjection: {"_id": 0, "a": 0}}), 7246210);
assert.commandFailedWithCode(
coll.createIndex({"a": 1, "$**": 1}, {wildcardProjection: {"_id": 0, "a": 1}}), 7246208);
assert.commandFailedWithCode(
coll.createIndex({"a": 1, "$**": 1}, {wildcardProjection: {"_id": 1, "a": 1}}), 7246208);
assert.commandFailedWithCode(
coll.createIndex({"_id": 1, "$**": 1}, {wildcardProjection: {"_id": 1, "a": 1}}), 7246208);
assert.commandFailedWithCode(
coll.createIndex({"$**": 1, "d": 1}, {wildcardProjection: {"_id": 1, e: 0}}), 7246209);
// Dotted paths
assert.commandWorked(
coll.createIndex({"_id": 1, "a.b": 1, "$**": 1}, {wildcardProjection: {"_id": 0, "a": 0}}));
assert.commandFailedWithCode(
coll.createIndex({"_id": 1, "a": 1, "$**": 1}, {wildcardProjection: {"_id": 0, "a.b": 0}}),
7246209,
);
assert.commandWorked(
coll.createIndex({"a.b": 1, "$**": 1}, {wildcardProjection: {"_id": 0, "a": 0}}));
assert.commandFailedWithCode(
coll.createIndex({"a": 1, "$**": 1}, {wildcardProjection: {"_id": 0, "a.b": 0}}), 7246209);
assert.commandWorked(
coll.createIndex({"$**": 1, "_id": 1, "e.b": 1}, {wildcardProjection: {"_id": 0, "a.b": 1}}));
assert.commandWorked(
coll.createIndex({"b.c": 1, "$**": 1}, {wildcardProjection: {"_id": 0, "a": 1}}));
assert.commandWorked(
coll.createIndex({"b.d": 1, "$**": 1}, {wildcardProjection: {"_id": 0, "a.b": 1}}));
assert.commandWorked(
coll.createIndex({"$**": 1, "e.b": 1}, {wildcardProjection: {"_id": 1, e: 0}}));
assert.commandFailedWithCode(
coll.createIndex({"$**": 1, "e": 1}, {wildcardProjection: {"_id": 1, "e.b": 0}}), 7246209);
assert.commandFailedWithCode(
coll.createIndex({"$**": 1, "e.c": 1}, {wildcardProjection: {"_id": 1, "e.b": 0}}),
7246210,
);
// Insert documents to index.
for (let i = 0; i < 10; i++) {
coll.insert({a: i, b: {a: i * 2, c: i * i}, str: 'aa'});
}
assert.commandWorked(coll.validate({full: true}));
})();