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:
Starting in MongoDB 3.6, the $expr operator allows the use of aggregation expressions within the query language.
Starting in MongoDB 4.4, the $function and $accumulator allows users to define custom aggregation expressions in JavaScript if the provided pipeline operators cannot fulfill your application’s needs.
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.