From the course: MongoDB Essential Training

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

$function

$function

- [Instructor] There is one pipeline operator, which you can program to be whatever you need it to be. The $function pipeline operator allows you to write JavaScript functions that operate on the field values in your documents. Let's try that out. I'm going to use the movies collection for this example. So let's briefly remind ourselves what the documents in that one look like. All right, as you can see, the actors field value is an array of actors. However, that array is not alphabetically sorted. If we wanted it to be alphabetically sorted, that's where we can use $function, for example. All right, so let's try that. I'm going to specify the movies collection and then call the aggregate method on it. As usual, I'm going to pass an array to the aggregate method and the array will contain our stages, in our case, just the one stage and we're going to use the project stage. The $function operator is just an operator…

Contents