-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
base: master
Are you sure you want to change the base?
Add Date aggregation #1585
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Rule workout:insights failed for files:
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
mongodb/intro/date-aggregation/aggregation-with-date-expression-operators.md
Outdated
Show resolved
Hide resolved
mongodb/intro/date-aggregation/aggregation-with-date-expression-operators.md
Outdated
Show resolved
Hide resolved
mongodb/intro/date-aggregation/aggregation-with-date-expression-operators.md
Outdated
Show resolved
Hide resolved
mongodb/intro/date-aggregation/aggregation-with-date-expression-operators-ii.md
Outdated
Show resolved
Hide resolved
mongodb/intro/date-aggregation/aggregation-with-date-expression-operators-v.md
Outdated
Show resolved
Hide resolved
mongodb/intro/date-aggregation/aggregation-with-date-expression-operators-v.md
Outdated
Show resolved
Hide resolved
mongodb/intro/date-aggregation/aggregation-with-date-expression-operators-v.md
Outdated
Show resolved
Hide resolved
mongodb/intro/date-aggregation/aggregation-with-date-expression-operators-v.md
Outdated
Show resolved
Hide resolved
mongodb/intro/date-aggregation/aggregation-with-date-expression-operators-v.md
Outdated
Show resolved
Hide resolved
Co-Authored-By: Andrei Calabangiu <andrei@enki.com>
…n-operators.md Co-Authored-By: Andrei Calabangiu <andrei@enki.com>
|
This comment has been minimized.
This comment has been minimized.
1 similar comment
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
1 similar comment
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
2 similar comments
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
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])** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
**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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
**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. |
There was a problem hiding this comment.
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?
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how does this sound?
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
**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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
**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. |
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 | ||
} | ||
``` |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
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 was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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` |
No description provided.