summaryrefslogtreecommitdiff
path: root/jstests/core/field_name_empty.js
blob: 260256de7c6437c06291540a3a25f67008b533ac (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
/**
 * Test the behavior of match expressions with empty field names.
 * @tags: [
 *   assumes_read_concern_local,
 * ]
 */
(function() {
"use strict";

load("jstests/aggregation/extras/utils.js");

const coll = db.field_name_empty;
coll.drop();

assert.commandWorked(coll.insertMany([
    {_id: 0, "": 1},
    {_id: 1, "": {"": 1}},
    {_id: 2, "": {"": {"": 1}}},
    {_id: 3, "": {"": {"": {"": 1}}}},
    {_id: 4, "": 1, a: 1},
    {_id: 5, x: 3},
    {_id: 6, x: [3]},
    {_id: 7, x: {"": 3}},
    {_id: 8, x: {"": [3]}},
    {_id: 9, x: [{"": 3}]},
    {_id: 10, x: [{"": [3]}]},
    {_id: 11, x: {"": [{"": 3}]}},
    {_id: 12, x: {"": {y: 3}}},
    {_id: 13, "": [1]},
]));

function runTest({filter, expected} = {}) {
    const result = coll.find(filter).toArray();
    const explain = coll.explain("executionStats").find(filter).finish();
    assertArrayEq({actual: result, expected: expected, extraErrorMsg: tojson(explain)});
}

runTest({filter: {".": 1}, expected: [{_id: 1, "": {"": 1}}, {_id: 13, "": [1]}]});
runTest({filter: {"..": 1}, expected: [{_id: 2, "": {"": {"": 1}}}]});
runTest({filter: {"...": 1}, expected: [{_id: 3, "": {"": {"": {"": 1}}}}]});
runTest({filter: {"": 1}, expected: [{_id: 0, "": 1}, {_id: 4, "": 1, a: 1}, {_id: 13, "": [1]}]});
runTest({filter: {"": 1, a: 1}, expected: [{_id: 4, "": 1, a: 1}]});
runTest({filter: {"": 1, a: 2}, expected: []});
runTest({
    filter: {'x.': 3},
    expected: [{_id: 6, x: [3]}, {_id: 7, x: {"": 3}}, {_id: 8, x: {"": [3]}}]
});
runTest({filter: {'x..y': 3}, expected: [{_id: 12, x: {"": {y: 3}}}]});
runTest({filter: {'x..': 3}, expected: [{_id: 8, x: {"": [3]}}, {_id: 10, x: [{"": [3]}]}]});
runTest({
    filter: {$and: [{'x.': 3}, {_id: {$lt: 8}}]},
    expected: [{_id: 6, x: [3]}, {_id: 7, x: {"": 3}}]
});
runTest({
    filter: {"x.": {$exists: false}},
    expected: [
        {_id: 0, "": 1},
        {_id: 1, "": {"": 1}},
        {_id: 2, "": {"": {"": 1}}},
        {_id: 3, "": {"": {"": {"": 1}}}},
        {_id: 4, "": 1, a: 1},
        {_id: 5, x: 3},
        {_id: 13, "": [1]},
    ]
});
runTest({
    filter: {x: {$elemMatch: {"": 3}}},
    expected: [{_id: 9, x: [{"": 3}]}, {_id: 10, x: [{"": [3]}]}]
});
runTest({filter: {x: {$elemMatch: {"": {$type: "array"}}}}, expected: [{_id: 10, x: [{"": [3]}]}]});
runTest({
    filter: {"x.": {$elemMatch: {"": 3}}},
    expected: [{_id: 9, x: [{"": 3}]}, {_id: 10, x: [{"": [3]}]}, {_id: 11, x: {"": [{"": 3}]}}]
});
})();