I have a documents in MongoDB, one of them looks like this:
{
\"_id\" : 100,
\"name\" : \"Something\",
\"items\" : [
{
\"item\" : 47,
\"c
Starting in Mongo 4.4, the $function aggregation operator allows applying a custom javascript function to implement behaviour not supported by the MongoDB Query Language.
And coupled with improvements made to db.collection.update() in Mongo 4.2 that can accept an aggregation pipeline, allowing the update of a field based on its own value,
We can manipulate and update an array in ways the language doesn't easily permit:
// {
// "name" : "Something",
// "items" : [
// { "item" : 47, "color" : "red" },
// { "item" : 39, "color" : "blue" },
// { "item" : 44, "color" : "green" }
// ]
// }
db.collection.update(
{},
[{ $set:
{ "items":
{ $function: {
body: function(items) {
var min = Math.min(...items.map(x => x.item));
return items.filter(x => x.item != min);
},
args: ["$items"],
lang: "js"
}}
}
}],
{ multi: true }
)
// {
// "name" : "Something",
// "items" : [
// { "item" : 47, "color" : "red" },
// { "item" : 44, "color" : "green" }
// ]
// }
$function takes 3 parameters:
body, which is the function to apply, whose parameter is the array to modify. The function here simply consists in first finding the minimum item from the array in order to then filter it out.args, which contains the fields from the record that the body function takes as parameter. In our case, "$items".lang, which is the language in which the body function is written. Only js is currently available.