Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Date aggregation #1585

Open
wants to merge 62 commits into
base: master
Choose a base branch
from
Open

Conversation

Stefan-Stojanovic
Copy link
Collaborator

No description provided.

@enki-hq

This comment has been minimized.

@enki-hq

This comment has been minimized.

@enki-hq

This comment has been minimized.

@enki-hq
Copy link
Collaborator

enki-hq commented Nov 1, 2019

Rule workout:insights failed for files:

  • mongodb/intro/date-aggregation/README.md

@enki-hq

This comment has been minimized.

@enki-hq

This comment has been minimized.

@enki-hq

This comment has been minimized.

@enki-hq

This comment has been minimized.

@enki-hq

This comment has been minimized.

@enki-hq

This comment has been minimized.

@enki-hq

This comment has been minimized.

@enki-hq

This comment has been minimized.

@Stefan-Stojanovic Stefan-Stojanovic changed the title Add Date aggregation {wip} Add Date aggregation Nov 1, 2019
Stefan-Stojanovic and others added 2 commits November 8, 2019 15:42
Co-Authored-By: Andrei Calabangiu <andrei@enki.com>
…n-operators.md

Co-Authored-By: Andrei Calabangiu <andrei@enki.com>
@kapnobatai137
Copy link
Contributor

  • test in spider
  • merge

@enki-hq

This comment has been minimized.

1 similar comment
@enki-hq

This comment has been minimized.

@enki-hq

This comment has been minimized.

1 similar comment
@enki-hq

This comment has been minimized.

@enki-hq

This comment has been minimized.

@enki-hq

This comment has been minimized.

2 similar comments
@enki-hq

This comment has been minimized.

@enki-hq

This comment has been minimized.

@enki-hq

This comment has been minimized.

@enki-hq

This comment has been minimized.


Now that we know how to extract the date into an ISODate format, we can use some aggregation operators to find the exact time that the document was made as precise as in milliseconds.

**Note:** For easier readability and comprehension of date operators, we will be using the date from the `name:"Bulbasaur"` document shown in the previous insight. **(The document will also be in the footnotes of every insight of this workout under Previous Document[1])**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
**Note:** For easier readability and comprehension of date operators, we will be using the date from the `name:"Bulbasaur"` document shown in the previous insight. **(The document will also be in the footnotes of every insight of this workout under Previous Document[1])**
> 💡 For easier readability and comprehension of date operators, we will be using the date from the `name: "Bulbasaur"` document shown in the previous insight. **(The document will also be in the footnotes of every insight of this workout under Previous Document[1])**


**Note:** For easier readability and comprehension of date operators, we will be using the date from the `name:"Bulbasaur"` document shown in the previous insight. **(The document will also be in the footnotes of every insight of this workout under Previous Document[1])**

**Note:** All 10 date operators have the same syntax:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
**Note:** All 10 date operators have the same syntax:
All date operators have the same syntax:


The `$year` operator is used to find the year the document was created.

Since we are only looking for the date of a specific document, we will use the `$match` stage to only match the document we want. Also, we will use the `$project` stage to only aggregate the parts of the date we want.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure "aggregate" is the right word here. how does this sound?

Suggested change
Since we are only looking for the date of a specific document, we will use the `$match` stage to only match the document we want. Also, we will use the `$project` stage to only aggregate the parts of the date we want.
Since we are only looking for the date of a specific document, we will use the `$match` stage to only match the document we want. We'll use the `$project` stage to display only the parts of the date we want.

}
```

As you can see in the example above, the `$match` stage was used to match the pokémon document named `"Bulbasaur"` and get the year it was created.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how does this sound?

Suggested change
As you can see in the example above, the `$match` stage was used to match the pokémon document named `"Bulbasaur"` and get the year it was created.
We use the `$match` to find the pokémon document named `"Bulbasaur"`. Then, we extract the creation year in the `project` stage.


The "createdAt" in `createdAtYear: { $year: "$createdAt" }` is just how we named our date field after we extracted the date with the `.getTimestamp()` method.

**Note:** Just like with any aggregation, if we don't exclude the `_id` with the project stage, it will be displayed by default.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
**Note:** Just like with any aggregation, if we don't exclude the `_id` with the project stage, it will be displayed by default.
> 💡 Just like with any aggregation, if we don't exclude the `_id` with the project stage, it will be displayed by default.


Now that we've seen all the date operators and know how to use them, let's take a look at an example of all operators being used in the same aggregation.

**Note:** We are using the same document[1] as in the previous insights.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
**Note:** We are using the same document[1] as in the previous insights.
> 💡 We are using the same document[1] as in the previous insights.

Comment on lines +23 to +59
Example with all operators:
```js
db.pokemon.aggregate([
{ $match: { name: "Bulbasaur" } },
{
$project: {
dayOfTheYear: { $dayOfYear: "$createdAt" },
createdAtYear: { $year: "$createdAt" },
createdAtMonth: { $month: "$createdAt" },
dayOfTheMonth: { $dayOfMonth: "$createdAt" },
createdAtHour: { $hour: "$createdAt" },
createdAtMinute: { $minute: "$createdAt" },
createdAtSecond: { $second: "$createdAt" },
createdAtMillisecond: { $millisecond: "$createdAt" },
weekOfTheYear: { $week: "$createdAt" },
dayOfTheWeek: { $dayOfWeek: "$createdAt" },
_id: 0
}
}
]);
```

Output:
```js
{
"dayOfTheYear": 282,
"createdAtYear": 2019,
"createdAtMonth": 10,
"dayOfTheMonth": 9,
"createdAtHour": 7,
"createdAtMinute": 21,
"createdAtSecond": 14,
"createdAtMillisecond": 0,
"weekOfTheYear": 40,
"dayOfTheWeek": 4
}
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use a more creative example? imo there is no benefit in this example because we've already shown this in all the previous insights. all that it does is add all the operators in the project stage

we could do something like finding the remaining time until one's birthday, the number of days remaining untill a contract renewal or something else. also, we don't have to include all the operators as long as we use an example that shows how a user could use these operators in a real-life application

edit: I notice that you give some examples in the first insight. why not make use of those? "These operators can be used to extract the specific time at which a document was made, they can be used to find a group of documents created on a specific date or to find all documents between 2 dates."

```

---
## Practice
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add a revision question for this as well

---
## Content

In MongoDB, it is possible to aggregate a collection using date expression operators. These operators can be used to extract the specific time at which a document was made, they can be used to find a group of documents created on a specific date or to find all documents between 2 dates.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
In MongoDB, it is possible to aggregate a collection using date expression operators. These operators can be used to extract the specific time at which a document was made, they can be used to find a group of documents created on a specific date or to find all documents between 2 dates.
In MongoDB, it is possible to aggregate a collection using date expression operators.
These operators can be used to extract the specific time at which a document was made, find a group of documents created on a specific date, or to find all documents between two dates.

Comment on lines +21 to +33
There are a total of 10 operators that can be used to aggregate a collection by date.

These are:
- `$year`
- `$month`
- `$dayOfMonth`
- `$week`
- `$hour`
- `$minute`
- `$second`
- `$milisecond`
- `$dayOfYear`
- `$dayOfWeek`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
There are a total of 10 operators that can be used to aggregate a collection by date.
These are:
- `$year`
- `$month`
- `$dayOfMonth`
- `$week`
- `$hour`
- `$minute`
- `$second`
- `$milisecond`
- `$dayOfYear`
- `$dayOfWeek`
There are a total of 10 operators that can be used to aggregate a collection by date:
- `$year`
- `$month`
- `$dayOfMonth`
- `$week`
- `$hour`
- `$minute`
- `$second`
- `$milisecond`
- `$dayOfYear`
- `$dayOfWeek`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants