summaryrefslogtreecommitdiff
path: root/jstests/core/timeseries/timeseries_delete_one.js
blob: 39ad152a7d2159d6805898377d24949aa111d701 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
 * Tests running the deleteOne command on a time-series collection.
 *
 * @tags: [
 *   # We need a timeseries collection.
 *   requires_timeseries,
 *   featureFlagUpdateOneWithoutShardKey,
 * ]
 */

(function() {
"use strict";

const timeFieldName = "time";
const metaFieldName = "tag";
const dateTime = ISODate("2021-07-12T16:00:00Z");
const collNamePrefix = "timeseries_delete_one_";
let testCaseId = 0;

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

/**
 * Confirms that a deleteOne() returns the expected set of documents.
 */
function testDeleteOne({initialDocList, filter, expectedResultDocs, nDeleted}) {
    const coll = testDB.getCollection(collNamePrefix + testCaseId++);
    assert.commandWorked(testDB.createCollection(
        coll.getName(), {timeseries: {timeField: timeFieldName, metaField: metaFieldName}}));

    assert.commandWorked(coll.insert(initialDocList));

    const res = assert.commandWorked(coll.deleteOne(filter));
    assert.eq(nDeleted, res.deletedCount);

    const resultDocs = coll.find().toArray();
    assert.eq(resultDocs.length, initialDocList.length - nDeleted, tojson(resultDocs));

    // Validate the collection's exact contents if we were given the expected results. We may skip
    // this step in some cases, if the delete doesn't pinpoint a specific document.
    if (expectedResultDocs) {
        assert.eq(expectedResultDocs.length, resultDocs.length, resultDocs);
        expectedResultDocs.forEach(expectedDoc => {
            assert.docEq(
                expectedDoc,
                coll.findOne({_id: expectedDoc._id}),
                `Expected document (_id = ${expectedDoc._id}) not found in result collection: ${
                    tojson(resultDocs)}`);
        });
    }
}

const doc1_a_nofields = {
    _id: 1,
    [timeFieldName]: dateTime,
    [metaFieldName]: "A",
};
const doc2_a_f101 = {
    _id: 2,
    [timeFieldName]: dateTime,
    [metaFieldName]: "A",
    f: 101
};
const doc3_a_f102 = {
    _id: 3,
    [timeFieldName]: dateTime,
    [metaFieldName]: "A",
    f: 102
};
const doc4_b_f103 = {
    _id: 4,
    [timeFieldName]: dateTime,
    [metaFieldName]: "B",
    f: 103
};
const doc5_b_f104 = {
    _id: 5,
    [timeFieldName]: dateTime,
    [metaFieldName]: "B",
    f: 104
};
const doc6_c_f105 = {
    _id: 6,
    [timeFieldName]: dateTime,
    [metaFieldName]: "C",
    f: 105
};
const doc7_c_f106 = {
    _id: 7,
    [timeFieldName]: dateTime,
    [metaFieldName]: "C",
    f: 106,
};

// Query on the 'f' field leads to zero measurement delete.
(function testZeroMeasurementDelete() {
    jsTestLog("Running testZeroMeasurementDelete()");
    testDeleteOne({
        initialDocList: [doc1_a_nofields, doc4_b_f103, doc6_c_f105],
        filter: {f: 17},
        expectedDocList: [doc1_a_nofields, doc4_b_f103, doc6_c_f105],
        nDeleted: 0,
    });
})();

// Query on the 'f' field leads to a partial bucket delete.
(function testPartialBucketDelete() {
    jsTestLog("Running testPartialBucketDelete()");
    testDeleteOne({
        initialDocList: [doc1_a_nofields, doc2_a_f101, doc3_a_f102],
        filter: {f: 101},
        expectedDocList: [doc1_a_nofields, doc3_a_f102],
        nDeleted: 1,
    });
})();

// Query on the 'f' field leads to a full (single document) bucket delete.
(function testFullBucketDelete() {
    jsTestLog("Running testFullBucketDelete()");
    testDeleteOne({
        initialDocList: [doc2_a_f101],
        filter: {f: 101},
        expectedDocList: [],
        nDeleted: 1,
    });
})();

// Query on the 'tag' field matches all docs and deletes one.
(function testMatchFullBucketOnlyDeletesOne() {
    jsTestLog("Running testMatchFullBucketOnlyDeletesOne()");
    testDeleteOne({
        initialDocList: [doc1_a_nofields, doc2_a_f101, doc3_a_f102],
        filter: {[metaFieldName]: "A"},
        // Don't validate exact results as we could delete any doc.
        nDeleted: 1,
    });
})();

// Query on the 'tag' and metric field.
(function testMetaAndMetricFilterOnlyDeletesOne() {
    jsTestLog("Running testMetaAndMetricFilterOnlyDeletesOne()");
    testDeleteOne({
        initialDocList: [doc1_a_nofields, doc2_a_f101, doc3_a_f102],
        filter: {[metaFieldName]: "A", f: {$gt: 100}},
        // Don't validate exact results as we could delete any doc.
        nDeleted: 1,
    });
})();

// Query on the 'f' field matches docs in multiple buckets but only deletes from one.
(function testMatchMultiBucketOnlyDeletesOne() {
    jsTestLog("Running testMatchMultiBucketOnlyDeletesOne()");
    testDeleteOne({
        initialDocList: [
            doc1_a_nofields,
            doc2_a_f101,
            doc3_a_f102,
            doc4_b_f103,
            doc5_b_f104,
            doc6_c_f105,
            doc7_c_f106
        ],
        filter: {f: {$gt: 101}},
        // Don't validate exact results as we could delete one of a few docs.
        nDeleted: 1,
    });
})();

// Empty filter matches all docs but only deletes one.
(function testEmptyFilterOnlyDeletesOne() {
    jsTestLog("Running testEmptyFilterOnlyDeletesOne()");
    testDeleteOne({
        initialDocList: [
            doc1_a_nofields,
            doc2_a_f101,
            doc3_a_f102,
            doc4_b_f103,
            doc5_b_f104,
            doc6_c_f105,
            doc7_c_f106
        ],
        filter: {},
        // Don't validate exact results as we could delete any doc.
        nDeleted: 1,
    });
})();
})();