summaryrefslogtreecommitdiff
path: root/jstests/aggregation/variables/search_meta.js
blob: d6b4afa2ba33d28be4355a5022702f0075a9aee5 (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
/**
 * Verify the error behavior of '$$SEARCH_META' when it is not properly configured. $$SEARCH_META
 * throws if used in aggregations on sharded collections.
 * @tags: [
 *   # $search/$searchMeta cannot be used within a facet
 *   do_not_wrap_aggregations_in_facets,
 *   # $search/$searchMeta do not support any read concern other than "local"
 *   assumes_read_concern_unchanged
 * ]
 */
(function() {
"use strict";
const getSearchMetaParam = db.adminCommand({getParameter: 1, featureFlagSearchMeta: 1});
const isSearchMetaEnabled = getSearchMetaParam.hasOwnProperty("featureFlagSearchMeta") &&
    getSearchMetaParam.featureFlagSearchMeta.value;
if (!isSearchMetaEnabled) {
    return;
}

const coll = db.searchCollector;
coll.drop();
assert.commandWorked(coll.insert({"_id": 1, "title": "cakes"}));

// Check that a query without a search stage gets errors if SEARCH_META is accessed.
assert.commandFailedWithCode(
    db.runCommand({
        aggregate: coll.getName(),
        cursor: {},
        pipeline: [{$project: {_id: 1, meta: "$$SEARCH_META"}}]
    }),
    [6347902, 6347903]);  // Error code depends on presence of the enterprise module.

// Check that users cannot assign values to SEARCH_META.
assert.commandFailedWithCode(db.runCommand({
    aggregate: coll.getName(),
    pipeline: [
        {$lookup: {from: coll.getName(), let : {SEARCH_META: "$title"}, as: "joined", pipeline: []}}
    ],
    cursor: {}
}),
                             ErrorCodes.FailedToParse);

assert.throwsWithCode(
    () => db.non_existent_namespace.aggregate([{$searchMeta: {query: {nonsense: true}}}]),
    [6448001, 31082]);  // Error code may change on mongos
})();