summaryrefslogtreecommitdiff
path: root/jstests/core/create_collection.js
blob: 2ebbd4c03353c72fb38cc4c7f177265f9cfd9725 (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
// Cannot implicitly shard accessed collections because of collection existing when none
// expected.
// @tags: [assumes_no_implicit_collection_creation_after_drop]

// Tests for the "create" command.
(function() {
    "use strict";

    load("jstests/libs/get_index_helpers.js");

    // "create" command rejects invalid options.
    db.create_collection.drop();
    assert.commandFailedWithCode(db.createCollection("create_collection", {unknown: 1}),
                                 ErrorCodes.InvalidOptions);

    //
    // Tests for "idIndex" field.
    //

    // "idIndex" field not allowed with "viewOn".
    db.create_collection.drop();
    assert.commandWorked(db.createCollection("create_collection"));
    assert.commandFailedWithCode(db.runCommand({
        create: "create_view",
        viewOn: "create_collection",
        idIndex: {key: {_id: 1}, name: "_id_"}
    }),
                                 ErrorCodes.InvalidOptions);

    // "idIndex" field not allowed with "autoIndexId".
    db.create_collection.drop();
    assert.commandFailedWithCode(
        db.createCollection("create_collection",
                            {autoIndexId: false, idIndex: {key: {_id: 1}, name: "_id_"}}),
        ErrorCodes.InvalidOptions);

    // "idIndex" field must be an object.
    db.create_collection.drop();
    assert.commandFailedWithCode(db.createCollection("create_collection", {idIndex: 1}),
                                 ErrorCodes.TypeMismatch);

    // "idIndex" field cannot be empty.
    db.create_collection.drop();
    assert.commandFailedWithCode(db.createCollection("create_collection", {idIndex: {}}),
                                 ErrorCodes.FailedToParse);

    // "idIndex" field must be a specification for an _id index.
    db.create_collection.drop();
    assert.commandFailedWithCode(
        db.createCollection("create_collection", {idIndex: {key: {a: 1}, name: "a_1"}}),
        ErrorCodes.BadValue);

    // "idIndex" field must have "key" equal to {_id: 1}.
    db.create_collection.drop();
    assert.commandFailedWithCode(
        db.createCollection("create_collection", {idIndex: {key: {a: 1}, name: "_id_"}}),
        ErrorCodes.BadValue);

    // The name of an _id index gets corrected to "_id_".
    db.create_collection.drop();
    assert.commandWorked(
        db.createCollection("create_collection", {idIndex: {key: {_id: 1}, name: "a_1"}}));
    var indexSpec = GetIndexHelpers.findByKeyPattern(db.create_collection.getIndexes(), {_id: 1});
    assert.neq(indexSpec, null);
    assert.eq(indexSpec.name, "_id_", tojson(indexSpec));

    // "idIndex" field must only contain fields that are allowed for an _id index.
    db.create_collection.drop();
    assert.commandFailedWithCode(
        db.createCollection("create_collection",
                            {idIndex: {key: {_id: 1}, name: "_id_", sparse: true}}),
        ErrorCodes.InvalidIndexSpecificationOption);

    // "create" creates v=2 _id index when "v" is not specified in "idIndex".
    db.create_collection.drop();
    assert.commandWorked(
        db.createCollection("create_collection", {idIndex: {key: {_id: 1}, name: "_id_"}}));
    indexSpec = GetIndexHelpers.findByName(db.create_collection.getIndexes(), "_id_");
    assert.neq(indexSpec, null);
    assert.eq(indexSpec.v, 2, tojson(indexSpec));

    // "create" creates v=1 _id index when "idIndex" has "v" equal to 1.
    db.create_collection.drop();
    assert.commandWorked(
        db.createCollection("create_collection", {idIndex: {key: {_id: 1}, name: "_id_", v: 1}}));
    indexSpec = GetIndexHelpers.findByName(db.create_collection.getIndexes(), "_id_");
    assert.neq(indexSpec, null);
    assert.eq(indexSpec.v, 1, tojson(indexSpec));

    // "create" creates v=2 _id index when "idIndex" has "v" equal to 2.
    db.create_collection.drop();
    assert.commandWorked(
        db.createCollection("create_collection", {idIndex: {key: {_id: 1}, name: "_id_", v: 2}}));
    indexSpec = GetIndexHelpers.findByName(db.create_collection.getIndexes(), "_id_");
    assert.neq(indexSpec, null);
    assert.eq(indexSpec.v, 2, tojson(indexSpec));

    // "collation" field of "idIndex" must match collection default collation.
    db.create_collection.drop();
    assert.commandFailedWithCode(
        db.createCollection("create_collection",
                            {idIndex: {key: {_id: 1}, name: "_id_", collation: {locale: "en_US"}}}),
        ErrorCodes.BadValue);

    db.create_collection.drop();
    assert.commandFailedWithCode(db.createCollection("create_collection", {
        collation: {locale: "fr_CA"},
        idIndex: {key: {_id: 1}, name: "_id_", collation: {locale: "en_US"}}
    }),
                                 ErrorCodes.BadValue);

    db.create_collection.drop();
    assert.commandFailedWithCode(db.createCollection("create_collection", {
        collation: {locale: "fr_CA"},
        idIndex: {key: {_id: 1}, name: "_id_", collation: {locale: "simple"}}
    }),
                                 ErrorCodes.BadValue);

    db.create_collection.drop();
    assert.commandWorked(db.createCollection("create_collection", {
        collation: {locale: "en_US", strength: 3},
        idIndex: {key: {_id: 1}, name: "_id_", collation: {locale: "en_US"}}
    }));
    indexSpec = GetIndexHelpers.findByName(db.create_collection.getIndexes(), "_id_");
    assert.neq(indexSpec, null);
    assert.eq(indexSpec.collation.locale, "en_US", tojson(indexSpec));

    // If "collation" field is not present in "idIndex", _id index inherits collection default
    // collation.
    db.create_collection.drop();
    assert.commandWorked(db.createCollection(
        "create_collection",
        {collation: {locale: "en_US"}, idIndex: {key: {_id: 1}, name: "_id_"}}));
    indexSpec = GetIndexHelpers.findByName(db.create_collection.getIndexes(), "_id_");
    assert.neq(indexSpec, null);
    assert.eq(indexSpec.collation.locale, "en_US", tojson(indexSpec));
})();