summaryrefslogtreecommitdiff
path: root/jstests/aggregation/expressions/size.js
blob: 3aa1c5d61eb1e28f10c9d42d1e158d5e97df779a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
 * Test the $size expression.
 */
(function() {
"use strict";
load("jstests/aggregation/extras/utils.js");

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

assert.commandWorked(coll.insert({_id: 0, arr: []}));
assert.commandWorked(coll.insert({_id: 1, arr: [1]}));
assert.commandWorked(coll.insert({_id: 2, arr: ["asdf", "asdfasdf"]}));
assert.commandWorked(coll.insert({_id: 3, arr: [1, "asdf", 1234, 4.3, {key: 23}]}));
assert.commandWorked(coll.insert({_id: 4, arr: [3, [31, 31, 13, 13]]}));

const result = coll.aggregate([{$sort: {_id: 1}}, {$project: {_id: 0, length: {$size: "$arr"}}}]);
assert.eq(result.toArray(), [{length: 0}, {length: 1}, {length: 2}, {length: 5}, {length: 2}]);

assert.commandWorked(coll.insert({arr: 231}));
assertErrorCode(coll, {$project: {_id: 0, length: {$size: "$arr"}}}, 17124);
}());