From 67ebc52b8c753b9be8757f3a85e63b075f261dc9 Mon Sep 17 00:00:00 2001 From: seesharprun Date: Tue, 9 Dec 2025 11:33:02 -0500 Subject: [PATCH] Add reference files --- .../timestamp-expression/$tsincrement.md | 168 +++++++++++++++++ .../timestamp-expression/$tssecond.md | 177 ++++++++++++++++++ 2 files changed, 345 insertions(+) create mode 100644 reference/operators/timestamp-expression/$tsincrement.md create mode 100644 reference/operators/timestamp-expression/$tssecond.md diff --git a/reference/operators/timestamp-expression/$tsincrement.md b/reference/operators/timestamp-expression/$tsincrement.md new file mode 100644 index 0000000..60efc60 --- /dev/null +++ b/reference/operators/timestamp-expression/$tsincrement.md @@ -0,0 +1,168 @@ +--- +title: $tsIncrement +description: The $tsIncrement operator extracts the increment portion from a timestamp value. +type: operators +category: timestamp-expression +--- + +# $tsIncrement + +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. + +## Syntax + +```javascript +{ + $tsIncrement: +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **`expression`** | An expression that evaluates to a timestamp. If the expression does not evaluate to a timestamp, `$tsIncrement` returns an error. | + +## Examples + +Consider this sample document from the stores collection. + +```json +{ + "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4", + "name": "First Up Consultants | Beverage Shop - Satterfieldmouth", + "location": { + "lat": -89.2384, + "lon": -46.4012 + }, + "staff": { + "totalStaff": { + "fullTime": 8, + "partTime": 20 + } + }, + "sales": { + "totalSales": 75670, + "salesByCategory": [ + { + "categoryName": "Wine Accessories", + "totalSales": 34440 + }, + { + "categoryName": "Bitters", + "totalSales": 39496 + }, + { + "categoryName": "Rum", + "totalSales": 1734 + } + ] + }, + "promotionEvents": [ + { + "eventName": "Unbeatable Bargain Bash", + "promotionalDates": { + "startDate": { + "Year": 2024, + "Month": 6, + "Day": 23 + }, + "endDate": { + "Year": 2024, + "Month": 7, + "Day": 2 + } + }, + "discounts": [ + { + "categoryName": "Whiskey", + "discountPercentage": 7 + }, + { + "categoryName": "Bitters", + "discountPercentage": 15 + }, + { + "categoryName": "Brandy", + "discountPercentage": 8 + }, + { + "categoryName": "Sports Drinks", + "discountPercentage": 22 + }, + { + "categoryName": "Vodka", + "discountPercentage": 19 + } + ] + }, + { + "eventName": "Steal of a Deal Days", + "promotionalDates": { + "startDate": { + "Year": 2024, + "Month": 9, + "Day": 21 + }, + "endDate": { + "Year": 2024, + "Month": 9, + "Day": 29 + } + }, + "discounts": [ + { + "categoryName": "Organic Wine", + "discountPercentage": 19 + }, + { + "categoryName": "White Wine", + "discountPercentage": 20 + }, + { + "categoryName": "Sparkling Wine", + "discountPercentage": 19 + }, + { + "categoryName": "Whiskey", + "discountPercentage": 17 + }, + { + "categoryName": "Vodka", + "discountPercentage": 23 + } + ] + } + ] +} +``` + +### Example 1: Extract increment from audit timestamp + +Extract the increment value from the last updated timestamp in the audit log. + +```javascript +db.stores.aggregate([ + { $match: {"_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f"} }, + { + $project: { + name: 1, + lastUpdatedIncrement: { + $tsIncrement: "$auditLog.lastUpdated" + } + } + } +]) +``` + +This query returns the following result. + +```json +[ + { + "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f", + "name": "First Up Consultants | Bed and Bath Center - South Amir", + "lastUpdatedIncrement": Long("5") + } +] +``` diff --git a/reference/operators/timestamp-expression/$tssecond.md b/reference/operators/timestamp-expression/$tssecond.md new file mode 100644 index 0000000..843ee66 --- /dev/null +++ b/reference/operators/timestamp-expression/$tssecond.md @@ -0,0 +1,177 @@ +--- +title: $tsSecond +description: The $tsSecond operator extracts the seconds portion from a timestamp value. +type: operators +category: timestamp-expression +--- + +# $tsSecond + +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). + +## Syntax + +```javascript +{ + $tsSecond: +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **`expression`** | An expression that evaluates to a timestamp. If the expression does not evaluate to a timestamp, `$tsSecond` returns an error. | + +## Examples + +Consider this sample document from the stores collection. + +```json +{ + "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4", + "name": "First Up Consultants | Beverage Shop - Satterfieldmouth", + "location": { + "lat": -89.2384, + "lon": -46.4012 + }, + "staff": { + "totalStaff": { + "fullTime": 8, + "partTime": 20 + } + }, + "sales": { + "totalSales": 75670, + "salesByCategory": [ + { + "categoryName": "Wine Accessories", + "totalSales": 34440 + }, + { + "categoryName": "Bitters", + "totalSales": 39496 + }, + { + "categoryName": "Rum", + "totalSales": 1734 + } + ] + }, + "promotionEvents": [ + { + "eventName": "Unbeatable Bargain Bash", + "promotionalDates": { + "startDate": { + "Year": 2024, + "Month": 6, + "Day": 23 + }, + "endDate": { + "Year": 2024, + "Month": 7, + "Day": 2 + } + }, + "discounts": [ + { + "categoryName": "Whiskey", + "discountPercentage": 7 + }, + { + "categoryName": "Bitters", + "discountPercentage": 15 + }, + { + "categoryName": "Brandy", + "discountPercentage": 8 + }, + { + "categoryName": "Sports Drinks", + "discountPercentage": 22 + }, + { + "categoryName": "Vodka", + "discountPercentage": 19 + } + ] + }, + { + "eventName": "Steal of a Deal Days", + "promotionalDates": { + "startDate": { + "Year": 2024, + "Month": 9, + "Day": 21 + }, + "endDate": { + "Year": 2024, + "Month": 9, + "Day": 29 + } + }, + "discounts": [ + { + "categoryName": "Organic Wine", + "discountPercentage": 19 + }, + { + "categoryName": "White Wine", + "discountPercentage": 20 + }, + { + "categoryName": "Sparkling Wine", + "discountPercentage": 19 + }, + { + "categoryName": "Whiskey", + "discountPercentage": 17 + }, + { + "categoryName": "Vodka", + "discountPercentage": 23 + } + ] + } + ] +} +``` + +### Example 1: Extract seconds from audit timestamp + +This query extracts the seconds value from the last updated timestamp in the audit log. + +```javascript +db.stores.aggregate([ + { $match: {"_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f"} }, + { + $project: { + name: 1, + lastUpdatedSeconds: { + $tsSecond: "$lastUpdated" + }, + lastUpdatedDate: { + $toDate: { + $multiply: [ + { $tsSecond: "$lastUpdated" }, + 1000 + ] + } + } + } + } +]) +``` + +This query returns the following result. + +```json +[ + { + "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f", + "name": "First Up Consultants | Bed and Bath Center - South Amir", + "lastUpdatedSeconds": Long("1640995200"), + "lastUpdatedDate": ISODate("2022-01-01T00:00:00.000Z") + } +] +```