We already had a look at a couple of pipeline stages and operators, now I want to dive into some stages
in operators that help you with working with arrays and therefore, I prepared some new data which you
can either import with mongo import or you just go into your mongo shell, create a new collection on
the fly,
I'll name it friends and I'll insert many documents and then add an opening parentheses,
no closing parentheses,
copy in the values and then close it and hit enter
and this should now also add the three elements.
So in the friends collection, I now have these three documents, three persons with name, age and hobbies
and then some exam scores.
And I just need some arrays here because you can do quite a lot of things with arrays in the aggregation
framework.
Now what can we do with these arrays?
Well one thing you often want to do is that you want to kind of merge or combine arrays in a grouping
stage,
so let's start with that.
So for that, I prepared a brand new aggregation pipeline and we'll start with grouping our results.
I will group by age, let's say and by the way, you can also group if you just group by one field, like this
but I'm a fan of the document syntax because there you can assign your own names and make it clearer
by what you grouped.
So I will group by age and of course I do have an age on my documents,
I only got three, so I will only have, well not that many groups but still let's do that. Let's group by age
and let's then say we want to combine the hobby arrays, so that we know which hobbies exist for this age group
and for that age group. And we can do that, we can add an array, we can name it hobbies or you give it
a brand new name, like all hobbies, whatever you want
and now there are two operators mainly that help you with combining array values because you have to
keep in mind that in the group stage, multiple documents are merged into one,
so we all see the way of handling multiple array values in one array. And we can do that here with the
push operator, the push operator allows you to push a new element into the all hobbies array for every
incoming document.
Now every incoming document here will have a hobbies array,
this one, this hobbies field and you could push any field value into the array, not just existing arrays,
so what I will do is I will now push $hobbies.
Now let's give this a try,
let's take this aggregation pipeline and run it,
however not on persons but on friends,
so let's rerun it, like this.
Now what you see is that we have two groups, age 30 and age 29 and all hobbies is an array of arrays because
we push hobbies into our array and hobbies happens to be an array itself, here
hobbies is an array and we just push that value into our all hobbies array here.
That is what I meant, you can push any value of course, not just existing arrays but what if you wanted
to push the values of existing arrays in there but not as one array but you want to pull these values
out of this array and then add them to all hobbies.
Well that is also something you can do and I will show you how it works in the next lecture.