Cara menggunakan mongodb aggregate match month

Aggregation is at the same time a powerful resource for those who use MongoDB as it can at times appear confusing or even intimidate some inexperienced developers. And that's why, before we continue, I strongly recommend reading this post if you don’t know or have any difficulty understanding how Aggregation works.

The idea of this post is to be really simple, but with the intention of presenting some operators that helped me to quickly solve what is in the title of this post.

To test/execute our aggregates, I will start from the idea that you already have the Robo 3T configured (or other similar software), in addition, I decided to use a collection called User that will contain the following fields:

Aggregate birthdays of the day

With everything already set up, it is time to run our first Aggregate, in it we will return all users who have a birthday on the specified date. For this, we will need to use the new Date() which returns the current date and time:

Aggregate to return the current day’s birthdays

Operators

Mongo operators allow us to create complex queries that make easy the expected return, below I decided to list which ones were needed for this first Aggregate:

$match operator: Filters the documents that match the specified criteria.

$expr operator: Allows the use of expressions within a query.

$and operator: Validates one or more expressions and returns true if all are true.

$eq operator: Validates documents where the value of a field matches the specified value.

These are some of the most common operators in aggregate, with them you can already create a lot of validations, but as the focus of this post is on dates, let’s talk more about these two: $dayOfMonth and $month.

$dayOfMonth: Returns the day of the month of a date as a number, between 1 to 31. For example, let’s imagine that the value of our $ birthDate field is 04/12/2020, the expected return will be: 12.

$month: Returns the month of a date as a number, between 1 and 12. Following the example above, the expected return for $birthDate will be: 4 (04/12/2020).

Running our Aggregate:

Current day: 04–06–2020

It worked! With just a few operators you can already filter the day’s birthdays without much effort.

Taking advantage of the $month operator mentioned earlier, how would Aggregate look to return the birthdays of the current month? In other words, who has an anniversary from 04/01 to 04/30?

Aggregate birthdays of the month:

So far, nothing new, even simpler actually, since we want to test only one condition, we no longer need the $and operator.

Running our Aggregate:

Current month: April

In seventh heaven, it worked too! With the use of only a few operators, we could list all the birthdays of April, simple, right?

Now our last case, the birthdays of the current week.

Aggregate birthdays of the week:

That’s what you’re thinking, there’s something new there!

Aggregation Pipeline

In addition to new operators, we also have a new code snippet that we haven't seen before. We can call this new snippet a stage, just as we can also call the next snippet a stage, and this is what an Aggregation Pipeline is formed of, N stages where the returned documents can be manipulated/filtered by steps, until reaching the expected return.

Operator $week

Mongo has the $week operator which returns a number from 0 to 53 from a received date. However, we have a very interesting detail when comparing $week from different years and that's why we need these other operators. The reason for this can be found in the documentation specifically in this part: ‘Weeks begin on Sundays, and week 1 begins with the first Sunday of the year‘. In other words, dates with the same day/month but different years can return a different value when using the $week operator.

To solve this problem we will need the $project operator and return the birthDate field with a new value that will be generated by the $dateFromParts operator, which allows us to build and return a date from values ​​received individually. Our tricky here is to use the operator $year to return the current year we are in and not the year of the user’s birthday, in the other fields, we can return the values ​​referring to the user’s birthday without a problem.

After changing the birthDate value on the first stage ($project), on the next stage ($match), the $birthDate will have its value updated only for the year, example: initial $birthDate 04/22/1989, after our tricky the result will be: 04–22–2020.

Running our Aggregate:

Current week: 04–19–2020 / 04–25–2020

Done! We return all the birthdays of the current week using a simple Aggregate.

Actually working with dates was never a simple task, however, thanks to Mongo and its operators we were able to solve the proposed problem with little effort.

If you’ve made it this far, I hope this article has been helpful and can help you better understand how date operators work when using Aggregation.

If you know another way to solve it, or have any suggestion or criticism, send in the comments that I’m sure we can evolve this solution. Also, feel free to ping me on twitter @luansantosti.