Skip to content

Commit 67ebc52

Browse files
committed
Add reference files
1 parent 3a2123c commit 67ebc52

File tree

2 files changed

+345
-0
lines changed

2 files changed

+345
-0
lines changed
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
---
2+
title: $tsIncrement
3+
description: The $tsIncrement operator extracts the increment portion from a timestamp value.
4+
type: operators
5+
category: timestamp-expression
6+
---
7+
8+
# $tsIncrement
9+
10+
The `$tsIncrement` operator returns the increment value from a timestamp. Timestamps in MongoDB consist of two parts: a time value (in seconds since epoch) and an increment value. This operator extracts the increment portion.
11+
12+
## Syntax
13+
14+
```javascript
15+
{
16+
$tsIncrement: <expression>
17+
}
18+
```
19+
20+
## Parameters
21+
22+
| Parameter | Description |
23+
| --- | --- |
24+
| **`expression`** | An expression that evaluates to a timestamp. If the expression does not evaluate to a timestamp, `$tsIncrement` returns an error. |
25+
26+
## Examples
27+
28+
Consider this sample document from the stores collection.
29+
30+
```json
31+
{
32+
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
33+
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
34+
"location": {
35+
"lat": -89.2384,
36+
"lon": -46.4012
37+
},
38+
"staff": {
39+
"totalStaff": {
40+
"fullTime": 8,
41+
"partTime": 20
42+
}
43+
},
44+
"sales": {
45+
"totalSales": 75670,
46+
"salesByCategory": [
47+
{
48+
"categoryName": "Wine Accessories",
49+
"totalSales": 34440
50+
},
51+
{
52+
"categoryName": "Bitters",
53+
"totalSales": 39496
54+
},
55+
{
56+
"categoryName": "Rum",
57+
"totalSales": 1734
58+
}
59+
]
60+
},
61+
"promotionEvents": [
62+
{
63+
"eventName": "Unbeatable Bargain Bash",
64+
"promotionalDates": {
65+
"startDate": {
66+
"Year": 2024,
67+
"Month": 6,
68+
"Day": 23
69+
},
70+
"endDate": {
71+
"Year": 2024,
72+
"Month": 7,
73+
"Day": 2
74+
}
75+
},
76+
"discounts": [
77+
{
78+
"categoryName": "Whiskey",
79+
"discountPercentage": 7
80+
},
81+
{
82+
"categoryName": "Bitters",
83+
"discountPercentage": 15
84+
},
85+
{
86+
"categoryName": "Brandy",
87+
"discountPercentage": 8
88+
},
89+
{
90+
"categoryName": "Sports Drinks",
91+
"discountPercentage": 22
92+
},
93+
{
94+
"categoryName": "Vodka",
95+
"discountPercentage": 19
96+
}
97+
]
98+
},
99+
{
100+
"eventName": "Steal of a Deal Days",
101+
"promotionalDates": {
102+
"startDate": {
103+
"Year": 2024,
104+
"Month": 9,
105+
"Day": 21
106+
},
107+
"endDate": {
108+
"Year": 2024,
109+
"Month": 9,
110+
"Day": 29
111+
}
112+
},
113+
"discounts": [
114+
{
115+
"categoryName": "Organic Wine",
116+
"discountPercentage": 19
117+
},
118+
{
119+
"categoryName": "White Wine",
120+
"discountPercentage": 20
121+
},
122+
{
123+
"categoryName": "Sparkling Wine",
124+
"discountPercentage": 19
125+
},
126+
{
127+
"categoryName": "Whiskey",
128+
"discountPercentage": 17
129+
},
130+
{
131+
"categoryName": "Vodka",
132+
"discountPercentage": 23
133+
}
134+
]
135+
}
136+
]
137+
}
138+
```
139+
140+
### Example 1: Extract increment from audit timestamp
141+
142+
Extract the increment value from the last updated timestamp in the audit log.
143+
144+
```javascript
145+
db.stores.aggregate([
146+
{ $match: {"_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f"} },
147+
{
148+
$project: {
149+
name: 1,
150+
lastUpdatedIncrement: {
151+
$tsIncrement: "$auditLog.lastUpdated"
152+
}
153+
}
154+
}
155+
])
156+
```
157+
158+
This query returns the following result.
159+
160+
```json
161+
[
162+
{
163+
"_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f",
164+
"name": "First Up Consultants | Bed and Bath Center - South Amir",
165+
"lastUpdatedIncrement": Long("5")
166+
}
167+
]
168+
```
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
---
2+
title: $tsSecond
3+
description: The $tsSecond operator extracts the seconds portion from a timestamp value.
4+
type: operators
5+
category: timestamp-expression
6+
---
7+
8+
# $tsSecond
9+
10+
The `$tsSecond` operator returns the seconds value from a timestamp. Timestamps consist of two parts: a time value (in seconds since epoch) and an increment value. This operator extracts the seconds portion, which represents the time since the Unix epoch (January 1, 1970, 00:00:00 UTC).
11+
12+
## Syntax
13+
14+
```javascript
15+
{
16+
$tsSecond: <expression>
17+
}
18+
```
19+
20+
## Parameters
21+
22+
| Parameter | Description |
23+
| --- | --- |
24+
| **`expression`** | An expression that evaluates to a timestamp. If the expression does not evaluate to a timestamp, `$tsSecond` returns an error. |
25+
26+
## Examples
27+
28+
Consider this sample document from the stores collection.
29+
30+
```json
31+
{
32+
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
33+
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
34+
"location": {
35+
"lat": -89.2384,
36+
"lon": -46.4012
37+
},
38+
"staff": {
39+
"totalStaff": {
40+
"fullTime": 8,
41+
"partTime": 20
42+
}
43+
},
44+
"sales": {
45+
"totalSales": 75670,
46+
"salesByCategory": [
47+
{
48+
"categoryName": "Wine Accessories",
49+
"totalSales": 34440
50+
},
51+
{
52+
"categoryName": "Bitters",
53+
"totalSales": 39496
54+
},
55+
{
56+
"categoryName": "Rum",
57+
"totalSales": 1734
58+
}
59+
]
60+
},
61+
"promotionEvents": [
62+
{
63+
"eventName": "Unbeatable Bargain Bash",
64+
"promotionalDates": {
65+
"startDate": {
66+
"Year": 2024,
67+
"Month": 6,
68+
"Day": 23
69+
},
70+
"endDate": {
71+
"Year": 2024,
72+
"Month": 7,
73+
"Day": 2
74+
}
75+
},
76+
"discounts": [
77+
{
78+
"categoryName": "Whiskey",
79+
"discountPercentage": 7
80+
},
81+
{
82+
"categoryName": "Bitters",
83+
"discountPercentage": 15
84+
},
85+
{
86+
"categoryName": "Brandy",
87+
"discountPercentage": 8
88+
},
89+
{
90+
"categoryName": "Sports Drinks",
91+
"discountPercentage": 22
92+
},
93+
{
94+
"categoryName": "Vodka",
95+
"discountPercentage": 19
96+
}
97+
]
98+
},
99+
{
100+
"eventName": "Steal of a Deal Days",
101+
"promotionalDates": {
102+
"startDate": {
103+
"Year": 2024,
104+
"Month": 9,
105+
"Day": 21
106+
},
107+
"endDate": {
108+
"Year": 2024,
109+
"Month": 9,
110+
"Day": 29
111+
}
112+
},
113+
"discounts": [
114+
{
115+
"categoryName": "Organic Wine",
116+
"discountPercentage": 19
117+
},
118+
{
119+
"categoryName": "White Wine",
120+
"discountPercentage": 20
121+
},
122+
{
123+
"categoryName": "Sparkling Wine",
124+
"discountPercentage": 19
125+
},
126+
{
127+
"categoryName": "Whiskey",
128+
"discountPercentage": 17
129+
},
130+
{
131+
"categoryName": "Vodka",
132+
"discountPercentage": 23
133+
}
134+
]
135+
}
136+
]
137+
}
138+
```
139+
140+
### Example 1: Extract seconds from audit timestamp
141+
142+
This query extracts the seconds value from the last updated timestamp in the audit log.
143+
144+
```javascript
145+
db.stores.aggregate([
146+
{ $match: {"_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f"} },
147+
{
148+
$project: {
149+
name: 1,
150+
lastUpdatedSeconds: {
151+
$tsSecond: "$lastUpdated"
152+
},
153+
lastUpdatedDate: {
154+
$toDate: {
155+
$multiply: [
156+
{ $tsSecond: "$lastUpdated" },
157+
1000
158+
]
159+
}
160+
}
161+
}
162+
}
163+
])
164+
```
165+
166+
This query returns the following result.
167+
168+
```json
169+
[
170+
{
171+
"_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f",
172+
"name": "First Up Consultants | Bed and Bath Center - South Amir",
173+
"lastUpdatedSeconds": Long("1640995200"),
174+
"lastUpdatedDate": ISODate("2022-01-01T00:00:00.000Z")
175+
}
176+
]
177+
```

0 commit comments

Comments
 (0)