summaryrefslogtreecommitdiff
path: root/jstests/core/timeseries/timeseries_create_collection.js
blob: f23b7b49d19b7a864c35042fd9dbc525a62c8ab1 (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
/**
 * Tests basic create and drop timeseries Collection behavior. Also test that we fail with
 * NamespaceExists when namespace is already used and that we don't leave orphaned bucket collection
 * in that case.
 *
 * @tags: [
 *   does_not_support_stepdowns,
 *   does_not_support_transactions,
 * ]
 */
(function() {
'use strict';

const testDB = db.getSiblingDB(jsTestName());
assert.commandWorked(testDB.dropDatabase());

const timeFieldName = 'time';
const coll = testDB.t;

// Create a timeseries collection, listCollection should show view and bucket collection
assert.commandWorked(
    testDB.createCollection(coll.getName(), {timeseries: {timeField: timeFieldName}}));
let collections = assert.commandWorked(testDB.runCommand({listCollections: 1})).cursor.firstBatch;
jsTestLog('Checking listCollections result: ' + tojson(collections));
assert(collections.find(entry => entry.name === 'system.buckets.' + coll.getName()));
assert(collections.find(entry => entry.name === coll.getName()));

// Drop timeseries collection, both view and bucket collection should be dropped
coll.drop();
collections = assert.commandWorked(testDB.runCommand({listCollections: 1})).cursor.firstBatch;
jsTestLog('Checking listCollections result: ' + tojson(collections));
assert.isnull(collections.find(entry => entry.name === 'system.buckets.' + coll.getName()));
assert.isnull(collections.find(entry => entry.name === coll.getName()));

// Create a regular collection on the same namespace and verify result
assert.commandWorked(testDB.createCollection(coll.getName()));
collections = assert.commandWorked(testDB.runCommand({listCollections: 1})).cursor.firstBatch;
jsTestLog('Checking listCollections result: ' + tojson(collections));
assert.isnull(collections.find(entry => entry.name === 'system.buckets.' + coll.getName()));
assert(collections.find(entry => entry.name === coll.getName()));

// Create timeseries collection when regular collection already exist on namespace. Command should
// fail with NamespaceExists and no bucket collection should be created
assert.commandFailedWithCode(
    testDB.createCollection(coll.getName(), {timeseries: {timeField: timeFieldName}}),
    ErrorCodes.NamespaceExists);
collections = assert.commandWorked(testDB.runCommand({listCollections: 1})).cursor.firstBatch;
jsTestLog('Checking listCollections result: ' + tojson(collections));
assert.isnull(collections.find(entry => entry.name === 'system.buckets.' + coll.getName()));
assert(collections.find(entry => entry.name === coll.getName()));
coll.drop();

// Create a regular view on the same namespace and verify result
testDB.getCollection("other");
assert.commandWorked(testDB.runCommand(
    {create: coll.getName(), viewOn: "other", pipeline: [{$match: {field: "A"}}]}));
collections = assert.commandWorked(testDB.runCommand({listCollections: 1})).cursor.firstBatch;
jsTestLog('Checking listCollections result: ' + tojson(collections));
assert.isnull(collections.find(entry => entry.name === 'system.buckets.' + coll.getName()));
assert(collections.find(entry => entry.name === coll.getName()));

// Create timeseries collection when view already exist on namespace. Command should fail with
// NamespaceExists and no bucket collection should be created
assert.commandFailedWithCode(
    testDB.createCollection(coll.getName(), {timeseries: {timeField: timeFieldName}}),
    ErrorCodes.NamespaceExists);
collections = assert.commandWorked(testDB.runCommand({listCollections: 1})).cursor.firstBatch;
jsTestLog('Checking listCollections result: ' + tojson(collections));
assert.isnull(collections.find(entry => entry.name === 'system.buckets.' + coll.getName()));
assert(collections.find(entry => entry.name === coll.getName()));
})();