summaryrefslogtreecommitdiff
path: root/jstests/aggregation/sources/facet/use_cases.js
blob: fed4c0b0c5c7dd7bfa7c2f314f0621dca3326f75 (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
/**
 * Tests some practical use cases of the $facet stage.
 */
(function() {
    "use strict";
    const dbName = "test";
    const collName = jsTest.name();
    const testNs = dbName + "." + collName;

    Random.setRandomSeed();

    /**
     * Helper to get a random entry out of an array.
     */
    function randomChoice(array) {
        return array[Random.randInt(array.length)];
    }

    /**
     * Helper to generate a randomized document with the following schema:
     * {
     *   manufacturer: <string>,
     *   price: <double>,
     *   screenSize: <double>
     * }
     */
    function generateRandomDocument(docId) {
        const manufacturers =
            ["Sony", "Samsung", "LG", "Panasonic", "Mitsubishi", "Vizio", "Toshiba", "Sharp"];
        const minPrice = 100;
        const maxPrice = 4000;
        const minScreenSize = 18;
        const maxScreenSize = 40;

        return {
            _id: docId,
            manufacturer: randomChoice(manufacturers),
            price: Random.randInt(maxPrice - minPrice + 1) + minPrice,
            screenSize: Random.randInt(maxScreenSize - minScreenSize + 1) + minScreenSize,
        };
    }

    /**
     * Inserts 'nDocs' documents into collection given by 'dbName' and 'collName'. Documents will
     * have _ids in the range [0, nDocs).
     */
    function populateData(conn, nDocs) {
        var coll = conn.getDB(dbName).getCollection(collName);
        coll.remove({});  // Don't drop the collection, since it might be sharded.

        var bulk = coll.initializeUnorderedBulkOp();
        for (var i = 0; i < nDocs; i++) {
            const doc = generateRandomDocument(i);
            bulk.insert(doc);
        }
        assert.writeOK(bulk.execute());
    }

    function doExecutionTest(conn) {
        var coll = conn.getDB(dbName).getCollection(collName);
        //
        // Compute the most common manufacturers, and the number of TVs in each price range.
        //

        // First compute each separately, to make sure we have the correct results.
        const manufacturerPipe = [
            {$sortByCount: "$manufacturer"},
            // Sort by count and then by _id in case there are two manufacturers with an equal
            // count.
            {$sort: {count: -1, _id: 1}},
        ];
        const bucketedPricePipe = [
            {
              $bucket: {groupBy: "$price", boundaries: [0, 500, 1000, 1500, 2000], default: 2000},
            },
            {$sort: {count: -1}}
        ];
        const automaticallyBucketedPricePipe = [{$bucketAuto: {groupBy: "$price", buckets: 5}}];

        const mostCommonManufacturers = coll.aggregate(manufacturerPipe).toArray();
        const numTVsBucketedByPriceRange = coll.aggregate(bucketedPricePipe).toArray();
        const numTVsAutomaticallyBucketedByPriceRange =
            coll.aggregate(automaticallyBucketedPricePipe).toArray();

        const facetPipe = [{
            $facet: {
                manufacturers: manufacturerPipe,
                bucketedPrices: bucketedPricePipe,
                autoBucketedPrices: automaticallyBucketedPricePipe
            }
        }];

        // Then compute the results using $facet.
        const facetResult = coll.aggregate(facetPipe).toArray();
        assert.eq(facetResult.length, 1);
        const facetManufacturers = facetResult[0].manufacturers;
        const facetBucketedPrices = facetResult[0].bucketedPrices;
        const facetAutoBucketedPrices = facetResult[0].autoBucketedPrices;

        // Then assert they are the same.
        assert.eq(facetManufacturers, mostCommonManufacturers);
        assert.eq(facetBucketedPrices, numTVsBucketedByPriceRange);
        assert.eq(facetAutoBucketedPrices, numTVsAutomaticallyBucketedByPriceRange);
    }

    // Test against the standalone started by resmoke.py.
    const nDocs = 1000 * 10;
    const conn = db.getMongo();
    populateData(conn, nDocs);
    doExecutionTest(conn);

    // Test against a sharded cluster.
    const st = new ShardingTest({shards: 2});
    populateData(st.s0, nDocs);
    doExecutionTest(st.s0);

    // Test that $facet stage propagates information about involved collections.
    const shardedDBName = "sharded";
    const shardedCollName = "collection";
    const shardedColl = st.getDB(shardedDBName).getCollection(shardedCollName);
    const unshardedColl = st.getDB(shardedDBName).getCollection(collName);

    assert.commandWorked(st.admin.runCommand({enableSharding: shardedDBName}));
    assert.commandWorked(
        st.admin.runCommand({shardCollection: shardedColl.getFullName(), key: {_id: 1}}));

    // Test $lookup inside a $facet stage on a sharded collection.
    assert.commandWorked(unshardedColl.runCommand({
        aggregate: unshardedColl.getName(),
        pipeline: [{
            $facet: {
                a: [{
                    $lookup: {
                        from: shardedCollName,
                        localField: "_id",
                        foreignField: "_id",
                        as: "results"
                    }
                }]
            }
        }],
        cursor: {}
    }));
    // Then run the assertions against a sharded collection.
    assert.commandWorked(st.admin.runCommand({enableSharding: dbName}));
    assert.commandWorked(st.admin.runCommand({shardCollection: testNs, key: {_id: 1}}));

    // Make sure there is a chunk on each shard, so that our aggregations are targeted to multiple
    // shards.
    assert.commandWorked(st.admin.runCommand({split: testNs, middle: {_id: nDocs / 2}}));
    assert.commandWorked(st.admin.runCommand({moveChunk: testNs, find: {_id: 0}, to: "shard0000"}));
    assert.commandWorked(
        st.admin.runCommand({moveChunk: testNs, find: {_id: nDocs - 1}, to: "shard0001"}));

    doExecutionTest(st.s0);

    st.stop();
}());