-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
week.txt
86 lines (66 loc) · 2.07 KB
/
week.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
===================
$week (aggregation)
===================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. expression:: $week
Returns the week of the year for a date as a number between 0 and 53.
Weeks begin on Sundays, and week 1 begins with the first Sunday
of the year. Days preceding the first Sunday of the year are in
week 0. This behavior is the same as the "``%U``" operator to the
``strftime`` standard library function.
The :expression:`$week` expression has the following syntax:
.. code-block:: javascript
{ $week: <expression> }
The argument can be any valid :ref:`expression
<aggregation-expressions>` as long as it resolves to a date. For
more information on expressions, see :ref:`aggregation-expressions`.
Example
-------
Consider a ``sales`` collection with the following document:
.. code-block:: javascript
{ "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-01-01T08:15:39.736Z") }
The following aggregation uses the :expression:`$week` and other
date operators to break down the ``date`` field:
.. code-block:: javascript
:emphasize-lines: 15
db.sales.aggregate(
[
{
$project:
{
year: { $year: "$date" },
month: { $month: "$date" },
day: { $dayOfMonth: "$date" },
hour: { $hour: "$date" },
minutes: { $minute: "$date" },
seconds: { $second: "$date" },
milliseconds: { $millisecond: "$date" },
dayOfYear: { $dayOfYear: "$date" },
dayOfWeek: { $dayOfWeek: "$date" },
week: { $week: "$date" }
}
}
]
)
The operation returns the following result:
.. code-block:: javascript
{
"_id" : 1,
"year" : 2014,
"month" : 1,
"day" : 1,
"hour" : 8,
"minutes" : 15,
"seconds" : 39,
"milliseconds" : 736,
"dayOfYear" : 1,
"dayOfWeek" : 4,
"week" : 0
}