summaryrefslogtreecommitdiff
path: root/jstests/aggregation/bugs/server8141.js
blob: 3b1477e9ac4e3b139d66a9421808e7dca28ab2a5 (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
// SERVER-8141 Avoid treating arrays as literals in aggregation pipeline.
(function() {
'use strict';
var coll = db.exprs_in_arrays;
coll.drop();

assert.commandWorked(coll.insert({_id: 0, a: ['foo', 'bar', 'baz'], b: 'bar', c: 'Baz'}));

// An array of constants should still evaluate to an array of constants.
var pipeline = [{$project: {_id: 0, d: ['constant', 1]}}];
assert.eq(coll.aggregate(pipeline).toArray(), [{d: ['constant', 1]}]);

// A field name inside an array should take on the value of that field.
pipeline = [{$project: {_id: 0, d: ['$b']}}];
assert.eq(coll.aggregate(pipeline).toArray(), [{d: ['bar']}]);

// An expression inside an array should be evaluated.
pipeline = [{$project: {_id: 0, d: [{$toLower: 'FoO'}]}}];
assert.eq(coll.aggregate(pipeline).toArray(), [{d: ['foo']}]);

// Both an expression and a field name inside an array should be evaluated.
pipeline = [{$project: {_id: 0, d: ['$b', {$toLower: 'FoO'}]}}];
assert.eq(coll.aggregate(pipeline).toArray(), [{d: ['bar', 'foo']}]);

// A nested array should still be evaluated.
pipeline = [{$project: {_id: 0, d: ['$b', 'constant', [1, {$toLower: 'FoO'}]]}}];
assert.eq(coll.aggregate(pipeline).toArray(), [{d: ['bar', 'constant', [1, 'foo']]}]);

// Should still evaluate array elements inside arguments to an expression.
pipeline = [{$project: {_id: 0, d: {$setIntersection: ['$a', ['$b']]}}}];
assert.eq(coll.aggregate(pipeline).toArray(), [{d: ['bar']}]);

pipeline = [{$project: {_id: 0, d: {$setIntersection: ['$a', [{$toLower: 'FoO'}]]}}}];
assert.eq(coll.aggregate(pipeline).toArray(), [{d: ['foo']}]);

// Nested arrays.
pipeline = [{
    $project:
        {_id: 0, d: {$setIntersection: [[[1, 'foo', 'bar']], [[1, {$toLower: 'FoO'}, '$b']]]}}
}];
assert.eq(coll.aggregate(pipeline).toArray(), [{d: [[1, 'foo', 'bar']]}]);

coll.drop();

// Should replace missing values with NULL to preserve indices.
assert.commandWorked(coll.insert({_id: 1, x: 1, z: 2}));

pipeline = [{$project: {_id: 0, coordinate: ['$x', '$y', '$z']}}];
assert.eq(coll.aggregate(pipeline).toArray(), [{coordinate: [1, null, 2]}]);
}());