Community Day Trivia #28: What aggregation operator allows defining a custom aggregration function or expression in JavaScript?

We’ll be posting trivia challenges throughout the Community Day event!

The best answer for each challenge will be marked as a solution and earn the poster a promo code for $100 of Atlas credits and a Community Day 2021 Trivia Winner forum badge that can also be used as a title on the forums.

A great answer will include some information such as a link to more information or a tip on how you figured out the solution.

You can help us decide the best answer to a trivia challenge by liking great responses! To give everyone a chance at earning Atlas credits, we will use “have you already had a trivia solution accepted” to break ties in favour of those who haven’t had a solution yet.

What aggregation operator allows defining a custom aggregration function or expression in JavaScript?

The $function operator Defines a custom aggregation function or expression in JavaScript, but not supported by the MongoDB Query Language.

The $accumulator operator to execute your own JavaScript functions to implement behavior not supported by the MongoDB Query Language.

Alternative to $where

The query operator $where can also be used to specify JavaScript expression. However:

Given the available aggregation operators:

  • The use of $expr with aggregation operators that do not use JavaScript (i.e. non- $function and non- $accumulator operators) is faster than $where because it does not execute JavaScript and should be preferred if possible.
  • However, if you must create custom expressions, $function is preferred over $where .
2 Likes

Welcome back to the MongoDB Community Forums @mansi_joshi and thanks for a helpful answer to this trivia challenge!

I’ll be following up with a $100 Atlas promo code :slight_smile:

Regards,
Stennie

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.