summaryrefslogtreecommitdiff
path: root/jstests/core/timeseries/timeseries_collation.js
blob: 5faa7dd6f2339117895657928d26d53f93eca6cd (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
/**
 * Tests that time-series collections respect collations for metadata and min/max.
 *
 * @tags: [
 *   assumes_no_implicit_collection_creation_after_drop,
 *   does_not_support_stepdowns,
 *   does_not_support_transactions,
 *   requires_fcv_49,
 *   requires_getmore,
 * ]
 */
(function() {
'use strict';

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

TimeseriesTest.run((insert) => {
    const coll = db.timeseries_collation;
    const bucketsColl = db.getCollection('system.buckets.' + coll.getName());

    const timeFieldName = 'time';
    const metaFieldName = 'meta';

    coll.drop();
    assert.commandWorked(db.createCollection(coll.getName(), {
        timeseries: {timeField: timeFieldName, metaField: metaFieldName},
        collation: {locale: 'en', strength: 1, numericOrdering: true}
    }));
    assert.contains(bucketsColl.getName(), db.getCollectionNames());

    const docs = [
        {
            _id: 0,
            [timeFieldName]: ISODate(),
            [metaFieldName]: {a: ['b'], c: 'D'},
            x: '10',
            y: {z: ['2']}
        },
        {
            _id: 1,
            [timeFieldName]: ISODate(),
            [metaFieldName]: {a: ['B'], c: 'd'},
            r: "s",
            x: '5',
            y: {z: ['5']}
        },
        {_id: 2, [timeFieldName]: ISODate(), [metaFieldName]: {a: ['B'], c: 'D'}},
        {
            _id: 3,
            [timeFieldName]: ISODate(),
            [metaFieldName]: {a: ['B'], c: 'd'},
            r: "S",
            X: '10',
            Y: {z: ['2']}
        },
    ];

    assert.commandWorked(insert(coll, [docs[0], docs[1]]));
    assert.commandWorked(insert(coll, docs[2]));
    assert.commandWorked(insert(coll, docs[3]));

    // The metadata of all of the inserted documents matches based on the collation. If we were to
    // take collation into account when bucketing, we would end up getting back documents which all
    // share the same metadata, which wouldn't match their original data. So let's make sure all
    // the documents match their original data that we inserted.
    const results = coll.find().sort({_id: 1}).toArray();
    assert.eq(docs.length, results.length);
    for (let i = 0; i < results.length; i++) {
        assert.docEq(results[i], docs[i]);
    }

    // Now let's check that min and max appropriately ignore collation for field names, but not
    // values.
    const buckets = bucketsColl.find().sort({'control.min._id': 1}).toArray();
    jsTestLog('Checking buckets: ' + tojson(buckets));
    assert.eq(buckets.length, 3);
    assert.eq(buckets[0].control.min.x, '10');
    assert.eq(buckets[0].control.min.y, {z: ['2']});
    assert.eq(buckets[0].control.max.x, '10');
    assert.eq(buckets[0].control.max.y, {z: ['2']});
    assert.eq(buckets[1].control.min.r, 's');
    assert.eq(buckets[1].control.min.x, '5');
    assert.eq(buckets[1].control.min.X, '10');
    assert.eq(buckets[1].control.min.y, {z: ['5']});
    assert.eq(buckets[1].control.min.Y, {z: ['2']});
    assert.eq(buckets[1].control.max.r, 's');
    assert.eq(buckets[1].control.max.x, '5');
    assert.eq(buckets[1].control.max.X, '10');
    assert.eq(buckets[1].control.max.y, {z: ['5']});
    assert.eq(buckets[1].control.max.Y, {z: ['2']});
    assert.eq(buckets[2].control.min.x, null);
    assert.eq(buckets[2].control.min.y, null);
    assert.eq(buckets[2].control.max.x, null);
    assert.eq(buckets[2].control.max.y, null);
});
})();