summaryrefslogtreecommitdiff
path: root/jstests/core/elemmatch_value.js
blob: d4e116f3bf981b2527539fd0f4cdef10b302c713 (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
/**
 * Test out ElemMatchValueMatchExpression
 */
(function() {
"use strict";

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

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

assert.commandWorked(coll.insert([
    {a: 5},
    {a: [5]},
    {a: [3, 7]},
    {a: [[5]]},
    {a: [[3, 7]]},
    {a: [[[5]]]},
    {a: [[[3, 7]]]},
    {a: {b: 5}},
    {a: {b: [5]}},
    {a: {b: [3, 7]}},
    {a: [{b: 5}]},
    {a: [{b: 3}, {b: 7}]},
    {a: [{b: [5]}]},
    {a: [{b: [3, 7]}]},
    {a: [[{b: 5}]]},
    {a: [[{b: 3}, {b: 7}]]},
    {a: [[{b: [5]}]]},
    {a: [[{b: [3, 7]}]]}
]));

assert(arrayEq(coll.find({a: {$elemMatch: {$lt: 6, $gt: 4}}}, {_id: 0}).toArray(), [{a: [5]}]));

assert(arrayEq(coll.find({"a.b": {$elemMatch: {$lt: 6, $gt: 4}}}, {_id: 0}).toArray(), [
    {a: {b: [5]}},
    {a: [{b: [5]}]},
]));

assert(arrayEq(coll.find({a: {$elemMatch: {$elemMatch: {$lt: 6, $gt: 4}}}}, {_id: 0}).toArray(), [
    {a: [[5]]},
]));

assert(arrayEq(coll.find({a: {$elemMatch: {$type: "number"}}}, {_id: 0}).toArray(), [
    {a: [5]},
    {a: [3, 7]},
]));

assert(arrayEq(coll.find({a: {$elemMatch: {$type: "array"}}}, {_id: 0}).toArray(), [
    {a: [[5]]},
    {a: [[3, 7]]},
    {a: [[[5]]]},
    {a: [[[3, 7]]]},
    {a: [[{b: 5}]]},
    {a: [[{b: 3}, {b: 7}]]},
    {a: [[{b: [5]}]]},
    {a: [[{b: [3, 7]}]]},
]));

assert(arrayEq(coll.find({a: {$elemMatch: {b: {$lt: 6, $gt: 4}}}}, {_id: 0}).toArray(), [
    {a: [{b: 5}]},
    {a: [{b: [5]}]},
    {a: [{b: [3, 7]}]},
]));

assert(
    arrayEq(coll.find({a: {$elemMatch: {b: {$elemMatch: {$lt: 6, $gt: 4}}}}}, {_id: 0}).toArray(), [
        {a: [{b: [5]}]},
    ]));

assert(
    arrayEq(coll.find({a: {$elemMatch: {$elemMatch: {b: {$lt: 6, $gt: 4}}}}}, {_id: 0}).toArray(), [
        {a: [[{b: 5}]]},
        {a: [[{b: [5]}]]},
        {a: [[{b: [3, 7]}]]},
    ]));
})();