summaryrefslogtreecommitdiff
path: root/jstests/core/collation_find_and_modify.js
blob: 6c0fd704dcc4cb7b6b21865069d4e86b24f42047 (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
// Cannot implicitly shard accessed collections because of collection existing when none
// expected.
// @tags: [assumes_no_implicit_collection_creation_after_drop]

// Integration tests for collation-aware findAndModify.
(function() {
'use strict';
var coll = db.getCollection("find_and_modify_update_test");

const caseInsensitive = {
    locale: "en_US",
    strength: 2
};
const caseSensitive = {
    locale: "en_US",
    strength: 3
};

// We restrict testing pipeline-style update to commands as they are not supported for OP_UPDATE
// which cannot differentiate an update object from an array.
if (db.getMongo().writeMode() === "commands") {
    //
    // Pipeline-style update respects collection default collation.
    //

    coll.drop();
    assert.commandWorked(db.createCollection(coll.getName(), {collation: caseInsensitive}));
    assert.commandWorked(coll.insert({x: [1, 2, "a", "b", "c", "B"]}));
    let doc =
        coll.findAndModify({update: [{$set: {newField: {$indexOfArray: ["$x", "B"]}}}], new: true});
    assert.eq(doc.newField, 3, doc);

    //
    // Pipeline-style findAndModify respects query collation.
    //

    // Case sensitive $indexOfArray on "B" matches "B".
    assert(coll.drop());
    assert.commandWorked(coll.insert({x: [1, 2, "a", "b", "c", "B"]}));
    doc = coll.findAndModify({
        update: [{$set: {newField: {$indexOfArray: ["$x", "B"]}}}],
        collation: caseSensitive,
        new: true
    });
    assert.eq(doc.newField, 5, doc);

    assert(coll.drop());
    assert.commandWorked(coll.insert({x: [1, 2, "a", "b", "c", "B"]}));
    doc = coll.findAndModify({
        update: [{$project: {newField: {$indexOfArray: ["$x", "B"]}}}],
        collation: caseSensitive,
        new: true
    });
    assert.eq(doc.newField, 5, doc);

    assert(coll.drop());
    assert.commandWorked(coll.insert({x: [1, 2, "a", "b", "c", "B"]}));
    doc = coll.findAndModify({
        update: [{$replaceWith: {newField: {$indexOfArray: ["$x", "B"]}}}],
        collation: caseSensitive,
        new: true
    });
    assert.eq(doc.newField, 5, doc);

    // Case insensitive $indexOfArray on "B" matches "b".
    assert(coll.drop());
    assert.commandWorked(coll.insert({x: [1, 2, "a", "b", "c", "B"]}));
    doc = coll.findAndModify({
        update: [{$set: {newField: {$indexOfArray: ["$x", "B"]}}}],
        collation: caseInsensitive,
        new: true
    });
    assert.eq(doc.newField, 3, doc);

    assert(coll.drop());
    assert.commandWorked(coll.insert({x: [1, 2, "a", "b", "c", "B"]}));
    doc = coll.findAndModify({
        update: [{$project: {newField: {$indexOfArray: ["$x", "B"]}}}],
        collation: caseInsensitive,
        new: true
    });
    assert.eq(doc.newField, 3, doc);

    assert(coll.drop());
    assert.commandWorked(coll.insert({x: [1, 2, "a", "b", "c", "B"]}));
    doc = coll.findAndModify({
        update: [{$replaceWith: {newField: {$indexOfArray: ["$x", "B"]}}}],
        collation: caseInsensitive,
        new: true
    });
    assert.eq(doc.newField, 3, doc);
}
})();