summaryrefslogtreecommitdiff
path: root/jstests/change_streams/change_stream_collation.js
blob: 5093bbe4ed0a7e207de65659ea765e09819a9605 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
 * Tests that a change stream can use a user-specified, or collection-default collation.
 *
 * This test assumes that it will be able to drop and then re-create a collection with non-default
 * options.
 * @tags: [assumes_no_implicit_collection_creation_after_drop]
 */
(function() {
    "use strict";

    load("jstests/libs/change_stream_util.js");  // For 'ChangeStreamTest'.

    let cst = new ChangeStreamTest(db);

    const caseInsensitive = {locale: "en_US", strength: 2};
    const caseInsensitiveCollection = db.change_stream_case_insensitive;
    caseInsensitiveCollection.drop();

    // Test that you can open a change stream before the collection exists, and it will use the
    // simple collation.
    const simpleCollationStream = cst.startWatchingChanges(
        {pipeline: [{$changeStream: {}}], collection: caseInsensitiveCollection});

    // Create the collection with a non-default collation - this should invalidate the stream we
    // opened before it existed.
    assert.commandWorked(
        db.runCommand({create: caseInsensitiveCollection.getName(), collation: caseInsensitive}));
    cst.assertNextChangesEqual({
        cursor: simpleCollationStream,
        expectedChanges: [{operationType: "invalidate"}],
        expectInvalidate: true
    });

    const implicitCaseInsensitiveStream = cst.startWatchingChanges({
        pipeline: [
            {$changeStream: {}},
            {$match: {"fullDocument.text": "abc"}},
            // Be careful not to use _id in this projection, as startWatchingChanges() will exclude
            // it by default, assuming it is the resume token.
            {$project: {docId: "$documentKey._id"}}
        ],
        collection: caseInsensitiveCollection
    });
    const explicitCaseInsensitiveStream = cst.startWatchingChanges({
        pipeline: [
            {$changeStream: {}},
            {$match: {"fullDocument.text": "abc"}},
            {$project: {docId: "$documentKey._id"}}
        ],
        collection: caseInsensitiveCollection,
        aggregateOptions: {collation: caseInsensitive}
    });

    assert.writeOK(caseInsensitiveCollection.insert({_id: 0, text: "aBc"}));
    assert.writeOK(caseInsensitiveCollection.insert({_id: 1, text: "abc"}));

    cst.assertNextChangesEqual(
        {cursor: implicitCaseInsensitiveStream, expectedChanges: [{docId: 0}, {docId: 1}]});
    cst.assertNextChangesEqual(
        {cursor: explicitCaseInsensitiveStream, expectedChanges: [{docId: 0}, {docId: 1}]});

    // Test that the collation does not apply to the scan over the oplog.
    const similarNameCollection = db.cHaNgE_sTrEaM_cAsE_iNsEnSiTiVe;
    similarNameCollection.drop();
    assert.commandWorked(
        db.runCommand({create: similarNameCollection.getName(), collation: {locale: "en_US"}}));

    assert.writeOK(similarNameCollection.insert({_id: 0, text: "aBc"}));

    assert.writeOK(caseInsensitiveCollection.insert({_id: 2, text: "ABC"}));

    // The existing stream should not see the first insert (to the other collection), but should see
    // the second.
    cst.assertNextChangesEqual(
        {cursor: implicitCaseInsensitiveStream, expectedChanges: [{docId: 2}]});
    cst.assertNextChangesEqual(
        {cursor: explicitCaseInsensitiveStream, expectedChanges: [{docId: 2}]});

    // Test that creating a collection without a collation does not invalidate any change streams
    // that were opened before the collection existed.
    (function() {
        const noCollationCollection = db.change_stream_no_collation;
        noCollationCollection.drop();

        const streamCreatedBeforeNoCollationCollection = cst.startWatchingChanges({
            pipeline: [{$changeStream: {}}, {$project: {docId: "$documentKey._id"}}],
            collection: noCollationCollection
        });

        assert.commandWorked(db.runCommand({create: noCollationCollection.getName()}));
        assert.writeOK(noCollationCollection.insert({_id: 0}));

        cst.assertNextChangesEqual(
            {cursor: streamCreatedBeforeNoCollationCollection, expectedChanges: [{docId: 0}]});
    }());

    // Test that creating a collection and explicitly specifying the simple collation does not
    // invalidate any change streams that were opened before the collection existed.
    (function() {
        const simpleCollationCollection = db.change_stream_simple_collation;
        simpleCollationCollection.drop();

        const streamCreatedBeforeSimpleCollationCollection = cst.startWatchingChanges({
            pipeline: [{$changeStream: {}}, {$project: {docId: "$documentKey._id"}}],
            collection: simpleCollationCollection
        });

        assert.commandWorked(db.runCommand(
            {create: simpleCollationCollection.getName(), collation: {locale: "simple"}}));
        assert.writeOK(simpleCollationCollection.insert({_id: 0}));

        cst.assertNextChangesEqual(
            {cursor: streamCreatedBeforeSimpleCollationCollection, expectedChanges: [{docId: 0}]});
    }());

    // Test that creating a change stream with a non-default collation, then creating a collection
    // with the same collation will not invalidate the change stream.
    (function() {
        const frenchCollection = db.change_stream_french_collation;
        frenchCollection.drop();

        const frenchChangeStream = cst.startWatchingChanges({
            pipeline: [{$changeStream: {}}, {$project: {docId: "$documentKey._id"}}],
            aggregateOptions: {collation: {locale: "fr"}},
            collection: frenchCollection
        });

        assert.commandWorked(
            db.runCommand({create: frenchCollection.getName(), collation: {locale: "fr"}}));
        assert.writeOK(frenchCollection.insert({_id: 0}));

        cst.assertNextChangesEqual({cursor: frenchChangeStream, expectedChanges: [{docId: 0}]});
    }());

    // Test that creating a change stream with a non-default collation, then creating a collection
    // with *a different* collation will not invalidate the change stream.
    (function() {
        const germanCollection = db.change_stream_german_collation;
        germanCollection.drop();

        const englishCaseInsensitiveStream = cst.startWatchingChanges({
            pipeline: [
                {$changeStream: {}},
                {$match: {"fullDocument.text": "abc"}},
                {$project: {docId: "$documentKey._id"}}
            ],
            aggregateOptions: {collation: caseInsensitive},
            collection: germanCollection
        });

        assert.commandWorked(
            db.runCommand({create: germanCollection.getName(), collation: {locale: "de"}}));
        assert.writeOK(germanCollection.insert({_id: 0, text: "aBc"}));

        cst.assertNextChangesEqual(
            {cursor: englishCaseInsensitiveStream, expectedChanges: [{docId: 0}]});
    }());

    // Test that creating a change stream with a non-default collation against a collection that has
    // a non-simple default collation will use the collation specified on the operation.
    (function() {
        const caseInsensitiveCollection = db.change_stream_case_insensitive;
        caseInsensitiveCollection.drop();
        assert.commandWorked(db.runCommand(
            {create: caseInsensitiveCollection.getName(), collation: caseInsensitive}));

        const englishCaseSensitiveStream = cst.startWatchingChanges({
            pipeline: [
                {$changeStream: {}},
                {$match: {"fullDocument.text": "abc"}},
                {$project: {docId: "$documentKey._id"}}
            ],
            aggregateOptions: {collation: {locale: "en_US"}},
            collection: caseInsensitiveCollection
        });

        assert.writeOK(caseInsensitiveCollection.insert({_id: 0, text: "aBc"}));
        assert.writeOK(caseInsensitiveCollection.insert({_id: 1, text: "abc"}));

        cst.assertNextChangesEqual(
            {cursor: englishCaseSensitiveStream, expectedChanges: [{docId: 1}]});
    }());

    // Test that collation is supported by the shell helper.
    // Test that creating a change stream with a non-default collation against a collection that has
    // a simple default collation will use the collation specified on the operation.
    (function() {
        const noCollationCollection = db.change_stream_no_collation;
        noCollationCollection.drop();
        assert.commandWorked(db.runCommand({create: noCollationCollection.getName()}));

        const cursor = noCollationCollection.watch(
            [
              {$match: {"fullDocument.text": "abc"}},
              {$project: {docId: "$documentKey._id", _id: 0}}
            ],
            {collation: caseInsensitive});
        assert(!cursor.hasNext());
        assert.writeOK(noCollationCollection.insert({_id: 0, text: "aBc"}));
        assert.writeOK(noCollationCollection.insert({_id: 1, text: "abc"}));
        assert(cursor.hasNext());
        assert.docEq(cursor.next(), {docId: 0});
        assert(cursor.hasNext());
        assert.docEq(cursor.next(), {docId: 1});
        assert(!cursor.hasNext());
    }());

})();