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
|
(function() {
'use strict';
var isMongos = ("isdbgrid" == db.runCommand("ismaster").msg);
var extractResult = function(obj) {
if (!isMongos)
return obj;
// Sample mongos format:
// {
// raw: {
// "localhost:30000": {
// createdCollectionAutomatically: false,
// numIndexesBefore: 3,
// numIndexesAfter: 5,
// ok: 1
// }
// },
// ok: 1
// }
var numFields = 0;
var result = null;
for (var field in obj.raw) {
result = obj.raw[field];
numFields++;
}
assert.neq(null, result);
assert.eq(1, numFields);
return result;
};
var dbTest = db.getSisterDB('create_indexes_db');
dbTest.dropDatabase();
// Database does not exist
var collDbNotExist = dbTest.create_indexes_no_db;
var res = assert.commandWorked(
collDbNotExist.runCommand('createIndexes', {indexes: [{key: {x: 1}, name: 'x_1'}]}));
res = extractResult(res);
assert(res.createdCollectionAutomatically);
assert.eq(1, res.numIndexesBefore);
assert.eq(2, res.numIndexesAfter);
assert.isnull(res.note,
'createIndexes.note should not be present in results when adding a new index: ' +
tojson(res));
// Collection does not exist, but database does
var t = dbTest.create_indexes;
var res = assert.commandWorked(
t.runCommand('createIndexes', {indexes: [{key: {x: 1}, name: 'x_1'}]}));
res = extractResult(res);
assert(res.createdCollectionAutomatically);
assert.eq(1, res.numIndexesBefore);
assert.eq(2, res.numIndexesAfter);
assert.isnull(res.note,
'createIndexes.note should not be present in results when adding a new index: ' +
tojson(res));
// Both database and collection exist
res = assert.commandWorked(
t.runCommand('createIndexes', {indexes: [{key: {x: 1}, name: 'x_1'}]}));
res = extractResult(res);
assert(!res.createdCollectionAutomatically);
assert.eq(2, res.numIndexesBefore);
assert.eq(2,
res.numIndexesAfter,
'numIndexesAfter missing from createIndexes result when adding a duplicate index: ' +
tojson(res));
assert(res.note,
'createIndexes.note should be present in results when adding a duplicate index: ' +
tojson(res));
res = t.runCommand("createIndexes",
{indexes: [{key: {"x": 1}, name: "x_1"}, {key: {"y": 1}, name: "y_1"}]});
res = extractResult(res);
assert(!res.createdCollectionAutomatically);
assert.eq(2, res.numIndexesBefore);
assert.eq(3, res.numIndexesAfter);
res = assert.commandWorked(t.runCommand(
'createIndexes', {indexes: [{key: {a: 1}, name: 'a_1'}, {key: {b: 1}, name: 'b_1'}]}));
res = extractResult(res);
assert(!res.createdCollectionAutomatically);
assert.eq(3, res.numIndexesBefore);
assert.eq(5, res.numIndexesAfter);
assert.isnull(res.note,
'createIndexes.note should not be present in results when adding new indexes: ' +
tojson(res));
res = assert.commandWorked(t.runCommand(
'createIndexes', {indexes: [{key: {a: 1}, name: 'a_1'}, {key: {b: 1}, name: 'b_1'}]}));
res = extractResult(res);
assert.eq(5, res.numIndexesBefore);
assert.eq(5,
res.numIndexesAfter,
'numIndexesAfter missing from createIndexes result when adding duplicate indexes: ' +
tojson(res));
assert(res.note,
'createIndexes.note should be present in results when adding a duplicate index: ' +
tojson(res));
res = t.runCommand("createIndexes", {indexes: [{}]});
assert(!res.ok);
res = t.runCommand("createIndexes", {indexes: [{}, {key: {m: 1}, name: "asd"}]});
assert(!res.ok);
assert.eq(5, t.getIndexes().length);
res = t.runCommand("createIndexes", {indexes: [{key: {"c": 1}, sparse: true, name: "c_1"}]});
assert.eq(6, t.getIndexes().length);
assert.eq(1,
t.getIndexes()
.filter(function(z) {
return z.sparse;
})
.length);
res = t.runCommand("createIndexes", {indexes: [{key: {"x": "foo"}, name: "x_1"}]});
assert(!res.ok);
assert.eq(6, t.getIndexes().length);
res = t.runCommand("createIndexes", {indexes: [{key: {"x": 1}, name: ""}]});
assert(!res.ok);
assert.eq(6, t.getIndexes().length);
// Test that v0 indexes cannot be created.
res = t.runCommand('createIndexes', {indexes: [{key: {d: 1}, name: 'd_1', v: 0}]});
assert.commandFailed(res, 'v0 index creation should fail');
// Test that v1 indexes can be created explicitly.
res = t.runCommand('createIndexes', {indexes: [{key: {d: 1}, name: 'd_1', v: 1}]});
assert.commandWorked(res, 'v1 index creation should succeed');
// Test that index creation fails with an invalid top-level field.
res = t.runCommand('createIndexes', {indexes: [{key: {e: 1}, name: 'e_1'}], 'invalidField': 1});
assert.commandFailedWithCode(res, ErrorCodes.BadValue);
// Test that index creation fails with an invalid field in the index spec for index version V2.
res = t.runCommand('createIndexes',
{indexes: [{key: {e: 1}, name: 'e_1', 'v': 2, 'invalidField': 1}]});
assert.commandFailedWithCode(res, ErrorCodes.InvalidIndexSpecificationOption);
// Test that index creation fails with an invalid field in the index spec for index version V1.
res = t.runCommand('createIndexes',
{indexes: [{key: {e: 1}, name: 'e_1', 'v': 1, 'invalidField': 1}]});
assert.commandFailedWithCode(res, ErrorCodes.InvalidIndexSpecificationOption);
}());
|