30
loading...
This website collects cookies to deliver better user experience
const numbers = [2, 8, 15];
const greaterThanFive = (num) => num > 5;
const multiplyBy2 = (num) => num * 2;
const sum = (acc, num) => acc + num;
const filtered = numbers.filter(greaterThanFive);
const mapped = numbers.map(multiplyBy2);
const reduced = numbers.reduce(sum);
console.log(filtered); // [8, 15]
console.log(mapped); // [4, 16, 30]
console.log(reduced); // 25
MongoDB’s document data model naturally supports JSON and its expressive query language is simple for developers to learn and use.
numbers
array data used in the javascript example to create a new document in a generic collection. For improve the understanding, I will use MongoDB Playground to test our queries:[
{
"numbers": [
2,
8,
15
]
},
]
db.collection.aggregate([
{
$project: {
_id: 0,
filtered: {
$filter: {
input: "$numbers",
as: "num",
cond: {
$gt: [
"$$num",
5
]
}
}
}
}
}
])
aggregate
method to submit the query. That method enables aggregation framework;$project
aggregation pipeline stage. The specified fields inside it can be existing fields from the input documents or newly computed fields. In our case, filtered
field will be created and added to response;filtered
field will be given by
$filter
aggregation pipeline operator;$numbers
. That's our array to be iterated;num
to get each array value to test in filter condition. You could use any name here, just like you did in javascript filter method;cond
using $gt
expression to return a boolean if current array value $$num
is greater than 5;[
{
"filtered": [
8,
15
]
}
]
db.collection.aggregate([
{
$project: {
_id: 0,
mapped: {
$map: {
input: "$numbers",
as: "num",
in: {
$multiply: [
"$$num",
2
]
}
}
}
}
}
])
$multiply
expression to return all array values multiplied by 2.[
{
"mapped": [
4,
16,
30
]
}
]
db.collection.aggregate([
{
$project: {
_id: 0,
reduced: {
$reduce: {
input: "$numbers",
initialValue: 0,
in: {
$sum: [
"$$value",
"$$this"
]
}
}
}
}
}
])
$numbers
array as input
to iterate;in
is applied to the first element of the input array, initialValue
is set to 0;in
expression give us two special variables: $$value
is the variable that represents the cumulative value of the expression (acc
in javascript example) and $$this
is the variable that refers to the element being processed (num
in javascript example). In case, using $sum
expression to return the new accumulated value.[
{
"reduced": 25
}
]
db.collection.aggregate([
{
$project: {
_id: 0,
filtered: {
$filter: {
input: "$numbers",
as: "num",
cond: {
$gte: [
"$$num",
5
]
},
}
},
mapped: {
$map: {
input: "$numbers",
as: "num",
in: {
$multiply: [
"$$num",
2
]
}
}
},
reduced: {
$reduce: {
input: "$numbers",
initialValue: 0,
in: {
$sum: [
"$$value",
"$$this"
]
}
}
}
}
}
])
[
{
"filtered": [
8,
15
],
"mapped": [
4,
16,
30
],
"reduced": 25
}
]
[
{
"numbers": [
2,
8,
15
]
},
{
"numbers": [
4,
8,
9,
13
]
},
{
"numbers": [
1,
3,
7
]
}
]
[
{
"filtered": [
8,
15
],
"mapped": [
4,
16,
30
],
"reduced": 25
},
{
"filtered": [
8,
9,
13
],
"mapped": [
8,
16,
18,
26
],
"reduced": 34
},
{
"filtered": [
7
],
"mapped": [
2,
6,
14
],
"reduced": 11
}
]