summaryrefslogtreecommitdiff
path: root/jstests/core/timeseries/clustered_index_options.js
blob: f741e7800652e2b13a1a3e13a75fe5273beb96aa (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
/**
 * Tests that time-series buckets collections can be created with clusteredIndex options directly,
 * independent of the time-series collection creation command. This supports tools that clone
 * collections using the output of listCollections, which includes the clusteredIndex option.
 *
 * @tags: [
 *   assumes_against_mongod_not_mongos,
 *   assumes_no_implicit_collection_creation_after_drop,
 *   does_not_support_stepdowns,
 * ]
 */
(function() {
"use strict";

load("jstests/core/timeseries/libs/timeseries.js");

if (!TimeseriesTest.timeseriesCollectionsEnabled(db.getMongo())) {
    jsTestLog("Skipping test because the time-series collection feature flag is disabled");
    return;
}

const testDB = db.getSiblingDB(jsTestName());
const tsColl = testDB.clustered_index_options;
const tsCollName = tsColl.getName();
const bucketsCollName = 'system.buckets.' + tsCollName;

assert.commandWorked(testDB.createCollection(bucketsCollName, {clusteredIndex: false}));
assert.commandWorked(testDB.dropDatabase());

assert.commandWorked(testDB.createCollection(bucketsCollName, {clusteredIndex: true}));
assert.commandWorked(testDB.dropDatabase());

assert.commandWorked(
    testDB.createCollection(bucketsCollName, {clusteredIndex: true, expireAfterSeconds: 10}));
assert.commandWorked(testDB.dropDatabase());

// Round-trip creating a time-series collection.  Use the output of listCollections to re-create
// the buckets collection.
assert.commandWorked(
    testDB.createCollection(tsCollName, {timeseries: {timeField: 'time'}, expireAfterSeconds: 10}));

let res =
    assert.commandWorked(testDB.runCommand({listCollections: 1, filter: {name: bucketsCollName}}));
let options = res.cursor.firstBatch[0].options;
assert(options.clusteredIndex);
assert(tsColl.drop());

assert.commandWorked(testDB.createCollection(bucketsCollName, options));
res =
    assert.commandWorked(testDB.runCommand({listCollections: 1, filter: {name: bucketsCollName}}));
assert.eq(options, res.cursor.firstBatch[0].options);
assert.commandWorked(testDB.dropDatabase());

assert.commandFailedWithCode(testDB.createCollection(bucketsCollName, {clusteredIndex: {}}),
                             ErrorCodes.TypeMismatch);
assert.commandFailedWithCode(testDB.createCollection(bucketsCollName, {clusteredIndex: 'a'}),
                             ErrorCodes.TypeMismatch);
assert.commandFailedWithCode(
    testDB.createCollection(bucketsCollName,
                            {clusteredIndex: true, idIndex: {key: {_id: 1}, name: '_id_'}}),
    ErrorCodes.InvalidOptions);

// Using the 'clusteredIndex' option on any namespace other than a buckets namespace should fail.
assert.commandFailedWithCode(testDB.createCollection(tsCollName, {clusteredIndex: true}),
                             ErrorCodes.InvalidOptions);
assert.commandFailedWithCode(testDB.createCollection('test', {clusteredIndex: true}),
                             ErrorCodes.InvalidOptions);

// Using the 'expireAfterSeconds' option on any namespace other than a time-series namespace or a
// clustered time-series buckets namespace should fail.
assert.commandFailedWithCode(testDB.createCollection('test', {expireAfterSeconds: 10}),
                             ErrorCodes.InvalidOptions);
assert.commandFailedWithCode(testDB.createCollection(bucketsCollName, {expireAfterSeconds: 10}),
                             ErrorCodes.InvalidOptions);
})();