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
|
/**
* Test out ElemMatchObjectMatchExpression
*/
(function() {
"use strict";
load("jstests/aggregation/extras/utils.js"); // arrayEq
const coll = db.jstests_elemmatch_object;
coll.drop();
assert.commandWorked(coll.insert([
{a: {b: 1, c: 4}},
{a: [{b: 1, c: 4}]},
{a: [{b: 2, c: 3}]},
{a: [{b: 1, c: 2}, {b: 3, c: 4}]},
{a: [{b: 1, c: 2}, {b: 3, c: 4}, {b: 1, c: 4}]},
{a: [{b: [], c: []}]},
{a: [{b: [12, 2], c: [13, 3]}]},
{a: [{b: [11, 1], c: [14, 4]}]},
{a: [{b: {w: 1, x: 2}, c: {y: 3, z: 4}}]},
{a: [{b: [{w: 5, x: 6}, {w: 1, x: 2}], c: [{y: 7, z: 8}, {y: 3, z: 4}]}]},
{a: [{b: [{w: 5, x: 2}, {w: 1, x: 6}], c: [{y: 7, z: 4}, {y: 3, z: 8}]}]},
{a: [{b: [{w: 5, x: 6}, {w: 1, x: 2}], c: [{y: 7, z: 4}, {y: 3, z: 8}]}]},
{a: [{b: [{w: [11, 1], x: [12, 2]}], c: [{y: [13, 3], z: [14, 4]}]}]},
]));
assert(arrayEq(coll.find({a: {$elemMatch: {b: 2}}}, {_id: 0}).toArray(), [
{a: [{b: 2, c: 3}]},
{a: [{b: [12, 2], c: [13, 3]}]},
]));
assert(arrayEq(coll.find({a: {$elemMatch: {b: 1}}}, {_id: 0}).toArray(), [
{a: [{b: 1, c: 4}]},
{a: [{b: 1, c: 2}, {b: 3, c: 4}]},
{a: [{b: 1, c: 2}, {b: 3, c: 4}, {b: 1, c: 4}]},
{a: [{b: [11, 1], c: [14, 4]}]},
]));
assert(arrayEq(coll.find({a: {$elemMatch: {b: 1, c: 4}}}, {_id: 0}).toArray(), [
{a: [{b: 1, c: 4}]},
{a: [{b: 1, c: 2}, {b: 3, c: 4}, {b: 1, c: 4}]},
{a: [{b: [11, 1], c: [14, 4]}]},
]));
assert(arrayEq(
coll.find({a: {$elemMatch: {b: {$elemMatch: {}}, c: {$elemMatch: {}}}}}, {_id: 0}).toArray(), [
{a: [{b: [{w: 5, x: 6}, {w: 1, x: 2}], c: [{y: 7, z: 8}, {y: 3, z: 4}]}]},
{a: [{b: [{w: 5, x: 2}, {w: 1, x: 6}], c: [{y: 7, z: 4}, {y: 3, z: 8}]}]},
{a: [{b: [{w: 5, x: 6}, {w: 1, x: 2}], c: [{y: 7, z: 4}, {y: 3, z: 8}]}]},
{a: [{b: [{w: [11, 1], x: [12, 2]}], c: [{y: [13, 3], z: [14, 4]}]}]},
]));
assert(arrayEq(
coll.find({a: {$elemMatch: {b: {$elemMatch: {w: 1, x: 2}}, c: {$elemMatch: {}}}}}, {_id: 0})
.toArray(),
[
{a: [{b: [{w: 5, x: 6}, {w: 1, x: 2}], c: [{y: 7, z: 8}, {y: 3, z: 4}]}]},
{a: [{b: [{w: 5, x: 6}, {w: 1, x: 2}], c: [{y: 7, z: 4}, {y: 3, z: 8}]}]},
{a: [{b: [{w: [11, 1], x: [12, 2]}], c: [{y: [13, 3], z: [14, 4]}]}]},
]));
assert(arrayEq(
coll.find({a: {$elemMatch: {b: {$elemMatch: {w: 1, x: 2}}, c: {$elemMatch: {y: 3, z: 4}}}}},
{_id: 0})
.toArray(),
[
{a: [{b: [{w: 5, x: 6}, {w: 1, x: 2}], c: [{y: 7, z: 8}, {y: 3, z: 4}]}]},
{a: [{b: [{w: [11, 1], x: [12, 2]}], c: [{y: [13, 3], z: [14, 4]}]}]},
]));
assert(coll.drop());
assert.commandWorked(coll.insert([
{},
{a: 1},
{a: "foo"},
{a: []},
{a: {}},
{a: [1]},
{a: ["foo"]},
{a: [[]]},
{a: [{}]},
{a: [[1]]},
{a: [1, []]},
{a: [1, {}]},
{a: [{b: 1}]},
]));
assert(arrayEq(coll.find({a: {$elemMatch: {}}}, {_id: 0}).toArray(), [
{a: [[]]},
{a: [{}]},
{a: [[1]]},
{a: [1, []]},
{a: [1, {}]},
{a: [{b: 1}]},
]));
assert(arrayEq(coll.find({a: {$elemMatch: {$or: [{}, {}]}}}, {_id: 0}).toArray(), [
{a: [[]]},
{a: [{}]},
{a: [[1]]},
{a: [1, []]},
{a: [1, {}]},
{a: [{b: 1}]},
]));
})();
|