summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/timeseries_change_streams.js
blob: 63a0ebed68b02279436fe8abff59767d279c5fd3 (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
// Tests that $changeStream aggregations against time-series collections fail cleanly.
// @tags: [
//  requires_timeseries,
//  requires_replication,
// ]
(function() {
"use strict";

load("jstests/libs/fixture_helpers.js");     // For FixtureHelpers.
load("jstests/libs/change_stream_util.js");  // For ChangeStreamTest and
                                             // assert[Valid|Invalid]ChangeStreamNss.

const rst = new ReplSetTest({nodes: 1});
rst.startSet();
rst.initiate();

const timeFieldName = "time";
const metaFieldName = "tags";
const testDB = rst.getPrimary().getDB(jsTestName());
assert.commandWorked(testDB.dropDatabase());

const tsColl = testDB.getCollection("ts_point_data");
tsColl.drop();

assert.commandWorked(testDB.createCollection(
    tsColl.getName(), {timeseries: {timeField: timeFieldName, metaField: metaFieldName}}));

const nMeasurements = 10;

for (let i = 0; i < nMeasurements; i++) {
    const docToInsert = {
        time: ISODate(),
        tags: i.toString(),
        value: i + nMeasurements,
    };
    assert.commandWorked(tsColl.insert(docToInsert));
}

// Test that a changeStream cannot be opened on 'system.buckets.X' collections.
assert.commandFailedWithCode(testDB.runCommand({
    aggregate: "system.buckets." + tsColl.getName(),
    pipeline: [{$changeStream: {}}],
    cursor: {}
}),
                             ErrorCodes.InvalidNamespace);

// Test that a changeStream cannot be opened on a time-series collection because it's a view.
assert.commandFailedWithCode(
    testDB.runCommand({aggregate: tsColl.getName(), pipeline: [{$changeStream: {}}], cursor: {}}),
    ErrorCodes.CommandNotSupportedOnView);

rst.stopSet();
})();