summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/collation_clone_collection.js
blob: 1db8fec3841436c1efad42cd625303d972143f07 (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
/**
 * Tests that the "cloneCollection" command inherits the collection-default collation and that it is
 * used when filtering the source collection.
 */
(function() {
    "use strict";

    var source = MongoRunner.runMongod({});
    assert.neq(null, source, "mongod was unable to start up");

    var dest = MongoRunner.runMongod({});
    assert.neq(null, dest, "mongod was unable to start up");

    var sourceColl = source.getDB("test").collation;
    var destColl = dest.getDB("test").collation;

    assert.commandWorked(sourceColl.getDB().runCommand(
        {create: sourceColl.getName(), collation: {locale: "en", strength: 2}}));
    var sourceCollectionInfos = sourceColl.getDB().getCollectionInfos({name: sourceColl.getName()});

    assert.writeOK(sourceColl.insert({_id: "FOO"}));
    assert.writeOK(sourceColl.insert({_id: "bar"}));
    assert.eq([{_id: "FOO"}],
              sourceColl.find({_id: "foo"}).toArray(),
              "query should have performed a case-insensitive match");

    assert.commandWorked(
        sourceColl.createIndex({withSimpleCollation: 1}, {collation: {locale: "simple"}}));
    assert.commandWorked(sourceColl.createIndex({withDefaultCollation: 1}));
    assert.commandWorked(
        sourceColl.createIndex({withNonDefaultCollation: 1}, {collation: {locale: "fr"}}));
    var sourceIndexInfos = sourceColl.getIndexes().map(function(indexInfo) {
        // We remove the "ns" field from the index specification when comparing whether the indexes
        // that were cloned are equivalent because they were built on a different namespace.
        delete indexInfo.ns;
        return indexInfo;
    });

    // Test that the "cloneCollection" command respects the collection-default collation.
    destColl.drop();
    assert.commandWorked(destColl.getDB().runCommand({
        cloneCollection: sourceColl.getFullName(),
        from: sourceColl.getMongo().host,
        query: {_id: "foo"}
    }));

    var destCollectionInfos = destColl.getDB().getCollectionInfos({name: destColl.getName()});
    assert.eq(sourceCollectionInfos, destCollectionInfos);
    assert.eq([{_id: "FOO"}], destColl.find({}).toArray());

    var destIndexInfos = destColl.getIndexes().map(function(indexInfo) {
        // We remove the "ns" field from the index specification when comparing whether the indexes
        // that were cloned are equivalent because they were built on a different namespace.
        delete indexInfo.ns;
        return indexInfo;
    });

    assert.eq(sourceIndexInfos.length,
              destIndexInfos.length,
              "Number of indexes don't match; source: " + tojson(sourceIndexInfos) + ", dest: " +
                  tojson(destIndexInfos));
    for (var i = 0; i < sourceIndexInfos.length; ++i) {
        assert.contains(sourceIndexInfos[i], destIndexInfos);
    }
})();