Operators
Array operators
Operators used to perform array operations.
$arrayElemAt
Returns the element at the specified array index.
{ "$arrayElemAt": ["array", "idx"] }
expry({ $arrayElemAt: [[1, 2, 3], 0] }); // 1
expry({ $arrayElemAt: [[1, 2, 3], 1] }); // 2
expry({ $arrayElemAt: [[1, 2, 3], 3] }); // null
$concatArrays
Concatenates arrays to return the concatenated array.
{ "$concatArrays": ["array", "array", "..."] }
expry({ $concatArrays: [["hello", " "], ["world"]] }); // ["hello", " ", "world"]
expry({ $concatArrays: [["hello", " "], [["world"]]] }); // ["hello", " ", ["world"]]
$filter
Selects a subset of an array to return based on the specified condition. Returns an array with only those elements that match the condition. The returned elements are in the original order.
{ "$filter": { "input": "array", "cond": "boolean", "as": "string" } }
expry({
$filter: { input: [1, 2, 3, 4], as: "num", cond: { $gt: ["$$num", 2] } },
}); // [3, 4]
$firstN
Returns a specified number of elements from the beginning of an array.
{ "$firstN": { "n": "number", "input": "array" } }
expry({ $firstN: { n: 2, input: [1, 2, 3] } }); // [1, 2]
expry({ $firstN: { n: 3, input: [1, 2] } }); // [1, 2]
expry({ $firstN: { n: 2, input: [1] } }); // [1]
$in
Returns a boolean indicating whether a specified value is in an array.
{ "$in": ["any", "array"] }
expry({ $in: [2, [1, 2, 3]] }); // true
expry({ $in: ["abc", ["xyc", "abc"]] }); // true
expry({ $in: ["xy", ["xyc", "abc"]] }); // false
$indexOfArray
Searches an array for an occurrence of a specified value and returns the array index of the first occurrence. Array indexes start at zero.
{ "$indexOfArray": ["array", "any"] }
expry({ $indexOfArray: [["a", "abc"], "a"] }); // 0
expry({ $indexOfArray: [[1, 2], 5] }); // -1
$lastN
Returns a specified number of elements from the end of an array.
{ "$lastN": { "n": "number", "input": "array" } }
expry({ $lastN: { n: 2, input: [1, 2, 3] } }); // [2, 3]
expry({ $lastN: { n: 3, input: [1, 2] } }); // [1, 2]
expry({ $lastN: { n: 2, input: [1] } }); // [1]
$map
Applies an expression to each item in an array and returns an array with the applied results.
{ "$map": { "input": "array", "as": "string", "in": "expression" } }
expry({ $map: { input: [1, 2, 3], as: "num", in: { $add: ["$$num", 1] } } }); // [2, 3, 4]
expry({ $map: { input: ["a", "b"], as: "str", in: { $toUpper: "$$str" } } }); // ['A', 'B']
$maxN
Returns the n largest values in an array.
{ "$maxN": { "n": "number", "input": "array" } }
expry({ $maxN: { n: 2, input: [3, 7, 2, 4] } }); // [7, 4]
expry({ $maxN: { n: 3, input: [3, 7, 2, 4] } }); // [7, 4, 3]
expry({ $maxN: { n: 5, input: [3, 7, 2, 4] } }); // [7, 4, 3, 2]
$minN
Returns the n smallest values in an array.
{ "$minN": { "n": "number", "input": "array" } }
expry({ $minN: { n: 2, input: [3, 7, 2, 4] } }); // [2, 3]
expry({ $minN: { n: 3, input: [3, 7, 2, 4] } }); // [2, 3, 4]
expry({ $minN: { n: 5, input: [3, 7, 2, 4] } }); // [2, 3, 4, 7]
$reduce
Applies an expression to each element in an array and combines them into a single value.
{ "$reduce": { "input": "array", "initialValue": "any", "in": "expression" } }
expry({
$reduce: {
input: ["a", "b", "c"],
initialValue: "",
in: { $concat: ["$$value", "$$this"] },
},
}); // abc
expry({
$reduce: {
input: [1, 2, 3],
initialValue: 0,
in: { $add: ["$$value", "$$this"] },
},
}); // 6
$reverseArray
Accepts an array expression as an argument and returns an array with the elements in reverse order.
{ "$reverseArray": "array" }
expry({ $reverseArray: [4, 2, 3] }); // [3, 2, 4]
expry({ $reverseArray: ["a", "c", "b"] }); // ["b", "c", "a"]
$size
Counts and returns the total number of items in an array.
{ "$size": "array" }
expry({ $size: [1, 2, 3] }); // 3
expry({ $size: ["a", "b", "c", "d"] }); // 4
expry({ $size: [] }); // 0
$slice
Returns a subset of an array.
{ "$slice": ["array", "position", "n"] }
expry({ $slice: [[1, 2, 3], 1, 1] }); // [2]
expry({ $slice: [[1, 2, 3], 1, 2] }); // [2, 3]
expry({ $slice: [[1, 2, 3], 1, 3] }); // [2, 3]
expry({ $slice: [[1, 2, 3], 3, 2] }); // []
$sortArray
Sorts an array based on its elements. The sort order is user specified.
{ "$sortArray": { "input": "array", "sortBy": "expression" } }
expry({
$sortArray: { input: [3, 4, 2], sortBy: { $cmp: ["$$first", "$$second"] } },
}); // [2, 3, 4]
expry({
$sortArray: { input: [3, 4, 2], sortBy: { $cmp: ["$$second", "$$first"] } },
}); // [4, 3, 2]