summaryrefslogtreecommitdiff
path: root/jstests/query_golden/elemMatch.js
blob: bb6fb526257185265b02c5a757a268e255a132c5 (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
(function() {
"use strict";

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

jsTestLog('Inserting docs:');

const docs = [
    {a: [1, 2, 3, 4, 5, 6]},
    {a: [5, 6, 7, 8, 9]},
    {a: [1, 2, 3]},
    {a: []},
    {a: [1]},
    {a: [10]},
    {a: 5},
    {a: 6},
    {a: [[6]]},
    {a: [[[6]]]},
    {a: [{b: [6]}]},
    {a: [[{b: [6]}]]},
];
show(docs);

assert.commandWorked(coll.insert(docs));

function runPipeline(pipeline) {
    pipeline.push({$project: {_id: 0}});
    jsTestLog(`Pipeline: ${tojsononeline(pipeline)}`);
    show(coll.aggregate(pipeline));
}

// Value elemMatch.
let pipeline = [{$match: {a: {$elemMatch: {$gte: 5, $lte: 6}}}}];
runPipeline(pipeline);

pipeline = [{$match: {a: {$elemMatch: {$lt: 11, $gt: 9}}}}];
runPipeline(pipeline);

// Contradiction.
pipeline = [{$match: {a: {$elemMatch: {$lt: 5, $gt: 6}}}}];
runPipeline(pipeline);

// Nested elemMatch.
pipeline = [{$match: {a: {$elemMatch: {$elemMatch: {$gte: 5, $lte: 6}}}}}];
runPipeline(pipeline);

pipeline = [{$match: {a: {$elemMatch: {$elemMatch: {$elemMatch: {$gte: 5, $lte: 6}}}}}}];
runPipeline(pipeline);

// Various expressions under $elemMatch.
pipeline = [{$match: {a: {$elemMatch: {$size: 1}}}}];
runPipeline(pipeline);

pipeline = [{$match: {a: {$elemMatch: {$exists: true}}}}];
runPipeline(pipeline);

// Test for a value $elemMatch nested under an object $elemMatch.
pipeline = [{$match: {a: {$elemMatch: {b: {$elemMatch: {$gt: 5}}}}}}];
runPipeline(pipeline);

pipeline = [{$match: {a: {$elemMatch: {$elemMatch: {b: {$elemMatch: {$gt: 5}}}}}}}];
runPipeline(pipeline);
}());