summaryrefslogtreecommitdiff
path: root/jstests/query_golden/match_with_exists.js
blob: 6b46cf6df7aff42f555503673ef106c6cbe49ebd (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
/**
 * Test $match with $exists is supported and returns correct results.
 */

(function() {
"use strict";

const coll = db.cqf_golden_match_with_exists;

const runTest = (filter) => {
    const pipeline = [{$match: filter}];
    jsTestLog(`Query: ${tojsononeline(pipeline)}`);
    show(coll.aggregate(pipeline));
};

const runWithData = (docs, filters) => {
    coll.drop();
    jsTestLog("Inserting docs:");
    show(docs);
    assert.commandWorked(coll.insert(docs));
    print(`Collection count: ${coll.count()}`);
    for (const filter of filters) {
        runTest(filter);
    }
};

runWithData(
    [
        {_id: 0},
        {_id: 1, a: null},
        {_id: 2, a: 1},
        {_id: 3, b: null},
        {_id: 4, b: 2},
        {_id: 5, 'a': {'b': 3}},
        {_id: 6, 'a': [{'b': 4}]}
    ],
    [
        {a: {$exists: true}},
        {a: {$not: {$exists: false}}},
        {a: {$exists: false}},
        {b: {$exists: true}},
        {'a.b': {$exists: true}},
        {'a.b': {$exists: false}},
    ]);

runWithData(
    [
        {_id: 1, a: []},
    ],
    [{'a': {$exists: true}}, {'a': {$exists: false}}]);

runWithData(
    [
        {_id: 1, a: false},
    ],
    [{'a': {$exists: true}}, {'a': {$exists: false}}]);

runWithData([{_id: 1, a: [{'b': 2}, {'a': 1}]}],
            [{'a.a': {$exists: true}}, {'a.a': {$exists: false}}, {'a.b': {$exists: true}}]);

runWithData([{_id: 1, a: [[{b: 1}]]}], [{'a.b': {$exists: false}}, {'a.b': {$exists: true}}]);

runWithData(
    [
        {_id: 1, a: [1]},
        {_id: 2, a: [2]},
    ],
    [{'a': {$elemMatch: {$exists: true}}}, {'a': {$elemMatch: {$exists: false}}}]);
})();