Now to make this a bit easier to follo, I will write my queries in a javascript helper file for now
and then just copy them over into the console,
since I believe this is a bit easier to see than if I directly typed them in there, as we will add more and
more lines or stages and adding stages is easier here too because I can enter them in-between without sending
everything
by hitting enter.
So we saw the match stage in the last lecture and match is essentially just taking a filter as you
define it as an argument to the find method.
More interesting is the group stage, the group stage allows you to well group your data by a certain
field or by multiple fields, for that let's have a look at our data again.
We got our persons there, only females right now and now by what could we group them?
Now let's say we want to group by this state here and we want to see the sum of persons living in that
state, with the aggregate method and the aggregation framework,
this is easy to do. In group here,
we need to define a couple of parameters,
the first one always is
_id. Now _id defines by which fields you want to group
and now we will use _id in a way we haven't seen it before,
the value for _id will be a document.
Thus far, we always used an objectid, a string or maybe a number but we never use the document
but just as with any other field, you can assign a document to _id.
It's just not that common to be honest
but for the group method here, for the group stage, you often see that syntax because that will be interpreted
in a special way and it will basically allow you to define multiple fields by which you want to group.
So in my case, I want to group by location state and I can do this by assigning a key here which I give
any name I want,
state for example and then $location.state. The dollar sign is important here because
it tells mongodb that I'm referring to a field of our document which is passed into the group stage,
so I'm referring to a field of this document,
the location field and then I can access a nested field just with a dot, here
no dollar sign is required. So this should now group our results by the state.
Now we can add a new key to each document and you can name this key however you want, like total persons.
Now here you would pass a document where you now describe the kind of aggregation function you want
to execute,
now these aggregation functions can also be found in the official docs of course.
There if you go to the group stage, you'll see all the aggregation or accumulator operators that are
supported
and for us, the sum operator is interesting.
Now as always, read through all these docs and all these examples to learn all about the niche and edge
cases of each stage or each operator,
we will use the sum here by using $sum and then a value
you want to add for every document that is grouped together.
So if we have three people from the same location state, sum would be incremented by 1 times 3
and the interesting thing here is that mongodb
will basically do this summing up for us,
it will keep the aggregated sum in memory until it's done with a group and then writes the total sum into
this field.
It's also important to understand that group does accumulate data,
now that simply means that you might have multiple documents with the same state and group will only
output one, so three documents with the same state will be merged into one because you are aggregating,
you're building a sum in this case.
OK so enough of the talking,
let's now run this function.
Let's simply copy it here,
move over into the console and then paste it in and you see I get back some results and I should move
pretty one line higher so that pretty is also understood,
so now I can execute this and I get back some results.
Now what we see is we still have a lot of different states here in our data but we can already see the
aggregation seemed to work,
we get a totally different output,
we no longer have any person data because we changed it.
We used group to merge our documents into new documents with totally different data with the total persons
and that ID, that ID as you can tell is that object we defined with the state in which it's grouped
and even though most states only have one person which is simply related to the structure of our demo data
set
here, we see there are some states which seem to have more persons like sinop. And we can simply prove
that our aggregation is working correctly here by manually reaching out to our persons and finding all
persons where the location state is equal to sinop.
So if I add pretty here, I should find three females, important I might find more persons but three females.
So if I scroll up, get one female, here's a male so we can't count him in because we filtered for females in our aggregation
pipeline,
here's another female and this is the last record and also a female,
so we get three females in this state. And this is the group stage in action,
now let's play around with that a bit more.