summaryrefslogtreecommitdiff
path: root/jstests/core/create_indexes_with_unknown_field_names.js
blob: 2a3a0cbc9bcf6f1a5a54868522954f0760e94c0a (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
/**
 * Tests that we can have unknown field names in the index spec passed to the createIndexes command
 * if 'ignoreUnknownIndexOptions: true' is set on the createIndexes command.
 */
(function() {
"use strict";

db.unknown_field_names_create_indexes.drop();
assert.commandFailedWithCode(db.runCommand({
    createIndexes: "unknown_field_names_create_indexes",
    indexes: [{key: {x: 1}, name: "myindex", someField: "someValue"}]
}),
                             ErrorCodes.InvalidIndexSpecificationOption);

assert.commandFailedWithCode(db.runCommand({
    createIndexes: "unknown_field_names_create_indexes",
    indexes: [{key: {x: 1}, name: "myindex", someField: "someValue"}],
    ignoreUnknownIndexOptions: false
}),
                             ErrorCodes.InvalidIndexSpecificationOption);

assert.commandFailedWithCode(db.runCommand({
    createIndexes: "unknown_field_names_create_indexes",
    indexes: [{key: {x: 1}, name: "myindex", someField: "someValue"}],
    ignoreUnknownIndexOptions: "badValue"
}),
                             ErrorCodes.TypeMismatch);

assert.commandWorked(db.runCommand({
    createIndexes: "unknown_field_names_create_indexes",
    indexes: [{key: {x: 1}, name: "myindex", someField: "someValue"}],
    ignoreUnknownIndexOptions: true
}));

// Make sure 'someField' is not in the index spec.
let indexes = db.unknown_field_names_create_indexes.getIndexes();
for (let index in indexes) {
    if (0 === bsonWoCompare(indexes[index].key, {x: 1})) {
        assert.eq(indexes[index].someField, undefined);
    }
}
})();