summaryrefslogtreecommitdiff
path: root/jstests/aggregation/sources/project/project_with_expressions.js
blob: 0bff4ad1b22be8136261401e706b9a77abfe7949 (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
/**
 * Test that a $project with a combination of expressions and field projections gets evaluted
 * correctly, and overwrites the data present in the input document when necessary.
 */
(function() {
const coll = db.project_with_expressions;
coll.drop();

assert.commandWorked(coll.insert({_id: 0, a: {subObj1: {p: 1}, subObj2: {p: 1}, subObj3: {p: 1}}}));

function assertProjectionResultForFindAndAgg(projection, expectedResults) {
    const aggResults = coll.aggregate([{$project: projection}]).toArray();
    const aggNoPushdownResults =
        coll.aggregate([{$_internalInhibitOptimization: {}}, {$project: projection}]).toArray();
    const findResults = coll.find({}, projection).toArray();

    assert.eq(aggResults, expectedResults);
    assert.eq(aggNoPushdownResults, expectedResults);
    assert.eq(findResults, expectedResults);
}

// Case where a project with a valid sub-object, a project with missing sub-object and a project
// with sub-expression share a common parent, and the projection is represented using a dotted path.
assertProjectionResultForFindAndAgg(
    {_id: 0, "a.subObj1": {$literal: 1}, "a.subObj2": 1, "a.subObj3.q": 1},
    [{a: {subObj2: {p: 1}, subObj3: {}, subObj1: 1}}]);
assertProjectionResultForFindAndAgg(
    {_id: 0, "a.subObj2": 1, "a.subObj1": {$literal: 1}, "a.subObj3.q": {r: 1}},
    [{a: {subObj2: {p: 1}, subObj3: {}, subObj1: 1}}]);

// Case where a project with a valid sub-object, a project with missing sub-object and a project
// with sub-expression share a common parent, and the projection is represented using sub-objects.
assertProjectionResultForFindAndAgg(
    {_id: 0, a: {subObj1: {$literal: 1}, subObj2: 1, subObj3: {q: {r: 1}}}},
    [{a: {subObj2: {p: 1}, subObj3: {}, subObj1: 1}}]);
assertProjectionResultForFindAndAgg(
    {_id: 0, a: {subObj2: 1, subObj1: {$literal: 1}, subObj3: {q: 1}}},
    [{a: {subObj2: {p: 1}, subObj3: {}, subObj1: 1}}]);
})();