MongoDB: Advanced queries using elemMatch and aggregate
After doing some basic CRUD operations in my previous post, we’ll now have a look at little more advanced queries.
First we want to match all countries that have Turkish as official language. Let’s insert two countries where Turkish is spoken to a new collection:
Now we have two countries inserted, Germany and Turkey. Now if we want to get the name of all countries with Turkish as official language, we can try following query:
Surprise: it returns Germany as well! So let’s have a closer look:
we can access nested property by chaining them with a dot. This seemed to work.
we search for all documents where we have a language with a name “Turkish”
we search for all documents where we have a language with note “official”
The problem is: we never specified that 2. and 3. must happen on the same element of the language array, that’s why the query matches for Germany as we have a language with note “official” and a language with name “Turkish” in the languages array.
So we need to specify that the two conditions must match on the same query element. For this Mongo has an operator called “$elemMatch”. Using it in our query from above results in the following:
This time the result is what we wanted. Note that as we have a condition on the attribute languages attribute directly, we don’t chain the attributes anymore.
Next we will add a few more countries to make our next query more interesting:
Now that we have more countries, we want to return a list of all languages together with the countries in which they are an official language. For this we will call “aggregate” instead of “find” to the collection. With this function we can perform a sequence of of pipeline stages on the collection. You can find the full list of pipeline steps here.
As we can have more than one language, we first need an unwind statement, so that we get a single document for every language:
Now we have one document per language, we can filter for official languages. We do this with a $match pipeline step
Now we only have only countries with one of their official languages. The next step is to add an attribute called “language” which contains the language name. This will replace our current nested “languages” attribute. We also want a the “name” attribute to be called country, so we add this field as well
As we only want to see the “language” and “country” attributes, we add a projection step next:
Finally we need to group by country using the $group step.
In a group by, you have to specify the attributes on which to group using the _id attribute. The other attributes specify how we do the aggregation. Here we used the $push aggregation operator which adds the attribute value to an array. Now we have a final result. Of course it can be made more beautiful, by doing renaming the _id field to language, but I’ll leave this as extra exercise to you as we already did something similar before.
Now you saw how to do a bit more complex query and especially the power of the aggregate function.
Actually we did some map reduce here: with the steps $unwind, $addFields and $project we did the map part and the group step was the reduce part, so we’ve just learned how MapReduce is done in MongoDB. Note that MongoDB also has explicit map and reduce functions, but it is usually better to use aggregation functions.