Skip to content

Commit 52eb179

Browse files
committed
Add reference files
1 parent 3a2123c commit 52eb179

5 files changed

Lines changed: 1184 additions & 0 deletions

File tree

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
---
2+
title: $currentDate
3+
description: The $currentDate operator sets the value of a field to the current date, either as a Date or a timestamp.
4+
type: operators
5+
category: field-update
6+
---
7+
8+
# $currentDate
9+
10+
The `$currentDate` operator sets the value of a field to the current date, either as a Date or a timestamp. This operator is useful for tracking when documents were last modified or for setting creation timestamps.
11+
12+
## Syntax
13+
14+
```javascript
15+
{
16+
$currentDate: {
17+
<field1>: <typeSpecification1>,
18+
<field2>: <typeSpecification2>,
19+
...
20+
}
21+
}
22+
```
23+
24+
## Parameters
25+
26+
| Parameter | Description |
27+
| --- | --- |
28+
| **`field`** | The name of the field to set to the current date. |
29+
| **`typeSpecification`** | Optional. Specifies the type of the date value. Can be `true` (for Date type) or `{ $type: "timestamp" }` for timestamp type. Default is `true` (Date). |
30+
31+
## Examples
32+
33+
Consider this sample document from the stores collection.
34+
35+
```json
36+
{
37+
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
38+
"name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile",
39+
"location": {
40+
"lat": 60.1441,
41+
"lon": -141.5012
42+
},
43+
"staff": {
44+
"totalStaff": {
45+
"fullTime": 2,
46+
"partTime": 0
47+
}
48+
},
49+
"sales": {
50+
"salesByCategory": [
51+
{
52+
"categoryName": "DJ Headphones",
53+
"totalSales": 35921
54+
}
55+
],
56+
"fullSales": 3700
57+
},
58+
"promotionEvents": [
59+
{
60+
"eventName": "Bargain Blitz Days",
61+
"promotionalDates": {
62+
"startDate": {
63+
"Year": 2024,
64+
"Month": 3,
65+
"Day": 11
66+
},
67+
"endDate": {
68+
"Year": 2024,
69+
"Month": 2,
70+
"Day": 18
71+
}
72+
},
73+
"discounts": [
74+
{
75+
"categoryName": "DJ Turntables",
76+
"discountPercentage": 18
77+
},
78+
{
79+
"categoryName": "DJ Mixers",
80+
"discountPercentage": 15
81+
}
82+
]
83+
}
84+
],
85+
"tag": [
86+
"#ShopLocal",
87+
"#SeasonalSale",
88+
"#FreeShipping",
89+
"#MembershipDeals"
90+
],
91+
"company": "Lakeshore Retail",
92+
"city": "Port Cecile",
93+
"lastUpdated": {
94+
"$date": "2024-12-11T10:21:58.274Z"
95+
}
96+
}
97+
```
98+
99+
### Example 1: Setting current date
100+
101+
To add a `lastUpdated` field with the current date to a store document, use the $currentDate operator to create the field with the current date as a Date object.
102+
103+
```javascript
104+
db.stores.updateOne(
105+
{ "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f" },
106+
{
107+
$currentDate: {
108+
"lastUpdated": true
109+
}
110+
}
111+
)
112+
```
113+
114+
This operation returns the following the result.
115+
116+
```json
117+
{
118+
acknowledged: true,
119+
insertedId: null,
120+
matchedCount: 1,
121+
modifiedCount: 1,
122+
upsertedCount: 0
123+
}
124+
125+
```
126+
127+
### Example 2: Setting current timestamp
128+
129+
To add both a date field and a timestamp field to track modifications, use the $currentDate operator with a value of true and with a typestamp value.
130+
131+
```javascript
132+
db.stores.updateOne(
133+
{ "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f" },
134+
{
135+
$currentDate: {
136+
"lastModified": true,
137+
"lastModifiedTimestamp": { $type: "timestamp" }
138+
}
139+
}
140+
)
141+
```
142+
143+
This operation returns the following result
144+
145+
```json
146+
{
147+
acknowledged: true,
148+
insertedId: null,
149+
matchedCount: Long("1"),
150+
modifiedCount: Long("1"),
151+
upsertedCount: 0
152+
}
153+
```
154+
155+
### Example 3: Updating nested fields
156+
157+
Set current date for nested fields in the document structure.
158+
159+
```javascript
160+
db.stores.updateOne(
161+
{ "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f" },
162+
{
163+
$currentDate: {
164+
"sales.lastSalesUpdate": true,
165+
"staff.lastStaffUpdate": { $type: "timestamp" }
166+
}
167+
}
168+
)
169+
```
170+
171+
This operation returns the following result.
172+
173+
```json
174+
[
175+
{
176+
"_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f",
177+
"name": "First Up Consultants | Bed and Bath Center - South Amir",
178+
"lastUpdated": ISODate("2025-02-12T10:30:45.123Z"),
179+
"lastModified": ISODate("2025-02-12T10:30:45.123Z"),
180+
"lastModifiedTimestamp": Timestamp(1739450445, 1),
181+
"sales": {
182+
"totalSales": 37701,
183+
"lastSalesUpdate": ISODate("2025-02-12T10:30:45.123Z"),
184+
"salesByCategory": [
185+
{
186+
"categoryName": "Mattress Toppers",
187+
"totalSales": 37701
188+
}
189+
]
190+
},
191+
"staff": {
192+
"totalStaff": {
193+
"fullTime": 18,
194+
"partTime": 17
195+
},
196+
"lastStaffUpdate": Timestamp(1739450445, 1)
197+
}
198+
}
199+
]
200+
```
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
---
2+
title: $inc
3+
description: The $inc operator increments the value of a field by a specified amount.
4+
type: operators
5+
category: field-update
6+
---
7+
8+
# $inc
9+
10+
The `$inc` operator increments the value of a field by a specified amount. If the field doesn't exist, `$inc` creates the field and sets it to the specified value. The operator accepts positive and negative values for incrementing and decrementing respectively.
11+
12+
## Syntax
13+
14+
The syntax for the `$inc` operator is as follows:
15+
16+
```javascript
17+
{
18+
$inc: {
19+
<field1>: <amount1>,
20+
<field2>: <amount2>,
21+
...
22+
}
23+
}
24+
```
25+
26+
## Parameters
27+
28+
| Parameter | Description |
29+
| --- | --- |
30+
| **`field`** | The name of the field to increment. |
31+
| **`amount`** | The increment value. Must be a number (positive for increment, negative for decrement). |
32+
33+
## Examples
34+
35+
Consider this sample document from the stores collection.
36+
37+
```json
38+
{
39+
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
40+
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
41+
"staff": {
42+
"totalStaff": {
43+
"fullTime": 19,
44+
"partTime": 20
45+
}
46+
},
47+
"sales": {
48+
"totalSales": 151864,
49+
"salesByCategory": [
50+
{
51+
"categoryName": "Sound Bars",
52+
"totalSales": 2120
53+
},
54+
{
55+
"categoryName": "Home Theater Projectors",
56+
"totalSales": 45004
57+
}
58+
]
59+
}
60+
}
61+
```
62+
63+
### Example 1: Incrementing staff count
64+
65+
To increase the full-time staff count by 3, use the $inc operator on the fullTime staff field with a value of 3.
66+
67+
```javascript
68+
db.stores.updateOne(
69+
{ "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74" },
70+
{
71+
$inc: {
72+
"staff.totalStaff.fullTime": 3
73+
}
74+
}
75+
)
76+
```
77+
78+
### Example 2: Decreasing and increasing values
79+
80+
To decrease the part-time staff by 2 and increase total sales by 5000, use the $inc operator on both fields with values of -2 and 5000 respectively.
81+
82+
```javascript
83+
db.stores.updateOne(
84+
{ "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74" },
85+
{
86+
$inc: {
87+
"staff.totalStaff.partTime": -2,
88+
"sales.totalSales": 5000
89+
}
90+
}
91+
)
92+
```
93+
94+
### Example 3: Creating New Fields
95+
96+
If a field doesn't exist, `$inc` creates it with the specified value.
97+
98+
```javascript
99+
db.stores.updateOne(
100+
{ "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74" },
101+
{
102+
$inc: {
103+
"staff.contractorCount": 5,
104+
"sales.monthlyTarget": 200000
105+
}
106+
}
107+
)
108+
```
109+
110+
### Example 4: Incrementing array element values
111+
112+
Update specific sales figures within the salesByCategory array using positional operators.
113+
114+
```javascript
115+
db.stores.updateOne(
116+
{
117+
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
118+
"sales.salesByCategory.categoryName": "Sound Bars"
119+
},
120+
{
121+
$inc: {
122+
"sales.salesByCategory.$.totalSales": 500
123+
}
124+
}
125+
)
126+
```
127+
128+
After these operations, the document is updated as follows:
129+
130+
```json
131+
{
132+
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
133+
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
134+
"staff": {
135+
"totalStaff": {
136+
"fullTime": 22,
137+
"partTime": 18
138+
},
139+
"contractorCount": 5
140+
},
141+
"sales": {
142+
"totalSales": 156864,
143+
"monthlyTarget": 200000,
144+
"salesByCategory": [
145+
{
146+
"categoryName": "Sound Bars",
147+
"totalSales": 2620
148+
},
149+
{
150+
"categoryName": "Home Theater Projectors",
151+
"totalSales": 45004
152+
}
153+
]
154+
}
155+
}
156+
```

0 commit comments

Comments
 (0)