From b32f8484d4488f5350d9a9709bc21490939895ec Mon Sep 17 00:00:00 2001 From: seesharprun Date: Tue, 9 Dec 2025 11:32:46 -0500 Subject: [PATCH] Add reference files --- .../operators/arithmetic-expression/$abs.md | 209 +++++++++++++++ .../operators/arithmetic-expression/$add.md | 173 ++++++++++++ .../operators/arithmetic-expression/$ceil.md | 184 +++++++++++++ .../arithmetic-expression/$divide.md | 188 +++++++++++++ .../operators/arithmetic-expression/$exp.md | 191 ++++++++++++++ .../operators/arithmetic-expression/$floor.md | 212 +++++++++++++++ .../operators/arithmetic-expression/$ln.md | 207 +++++++++++++++ .../operators/arithmetic-expression/$log.md | 216 +++++++++++++++ .../operators/arithmetic-expression/$log10.md | 242 +++++++++++++++++ .../arithmetic-expression/$multiply.md | 196 ++++++++++++++ .../operators/arithmetic-expression/$pow.md | 192 ++++++++++++++ .../operators/arithmetic-expression/$round.md | 249 ++++++++++++++++++ .../operators/arithmetic-expression/$sqrt.md | 220 ++++++++++++++++ .../arithmetic-expression/$subtract.md | 203 ++++++++++++++ .../operators/arithmetic-expression/$trunc.md | 190 +++++++++++++ 15 files changed, 3072 insertions(+) create mode 100644 reference/operators/arithmetic-expression/$abs.md create mode 100644 reference/operators/arithmetic-expression/$add.md create mode 100644 reference/operators/arithmetic-expression/$ceil.md create mode 100644 reference/operators/arithmetic-expression/$divide.md create mode 100644 reference/operators/arithmetic-expression/$exp.md create mode 100644 reference/operators/arithmetic-expression/$floor.md create mode 100644 reference/operators/arithmetic-expression/$ln.md create mode 100644 reference/operators/arithmetic-expression/$log.md create mode 100644 reference/operators/arithmetic-expression/$log10.md create mode 100644 reference/operators/arithmetic-expression/$multiply.md create mode 100644 reference/operators/arithmetic-expression/$pow.md create mode 100644 reference/operators/arithmetic-expression/$round.md create mode 100644 reference/operators/arithmetic-expression/$sqrt.md create mode 100644 reference/operators/arithmetic-expression/$subtract.md create mode 100644 reference/operators/arithmetic-expression/$trunc.md diff --git a/reference/operators/arithmetic-expression/$abs.md b/reference/operators/arithmetic-expression/$abs.md new file mode 100644 index 0000000..60541f2 --- /dev/null +++ b/reference/operators/arithmetic-expression/$abs.md @@ -0,0 +1,209 @@ +--- +title: $abs +description: The $abs operator returns the absolute value of a number. +type: operators +category: arithmetic-expression +--- + +# $abs + +The `$abs` operator returns the absolute value of a number. It removes any negative sign from a number, making it positive. + +## Syntax + +```javascript +{ + $abs: +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **``** | Any expression that resolves to a number. If the expression is null or refers to a missing field, $abs returns null. | + +## 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 - Use the absolute value of total sales + +To calculate the absolute difference in sales volume of each category and the average sales across all categories for a store, first run a query to filter on the specific store. Then, calculate the difference in sales between each category and the average across all categories. Lastly, project the absolute difference using the $abs operator. + +```javascript +db.stores.aggregate([{ + $match: { + _id: "40d6f4d7-50cd-4929-9a07-0a7a133c2e74" + } +}, { + $project: { + name: 1, + salesByCategory: { + $map: { + input: "$sales.salesByCategory", + as: "category", + in: { + categoryName: "$$category.categoryName", + totalSales: "$$category.totalSales", + differenceFromAverage: { + $abs: { + $subtract: ["$$category.totalSales", { + $avg: "$sales.salesByCategory.totalSales" + }] + } + } + } + } + } + } +}]) +``` + +This query returns the following result: + +```json +[ + { + "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74", + "name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury", + "salesByCategory": [ + { + "categoryName": "Sound Bars", + "totalSales": 2120, + "differenceFromAverage": 28252.8 + }, + { + "categoryName": "Home Theater Projectors", + "totalSales": 45004, + "differenceFromAverage": 14631.2 + }, + { + "categoryName": "Game Controllers", + "totalSales": 43522, + "differenceFromAverage": 13149.2 + }, + { + "categoryName": "Remote Controls", + "totalSales": 28946, + "differenceFromAverage": 1426.8 + }, + { + "categoryName": "VR Games", + "totalSales": 32272, + "differenceFromAverage": 1899.2 + } + ] + } +] +``` diff --git a/reference/operators/arithmetic-expression/$add.md b/reference/operators/arithmetic-expression/$add.md new file mode 100644 index 0000000..3c3961f --- /dev/null +++ b/reference/operators/arithmetic-expression/$add.md @@ -0,0 +1,173 @@ +--- +title: $add +description: The $add operator returns the sum of two numbers or the sum of a date and numbers. +type: operators +category: arithmetic-expression +--- + +# $add + +The `$add` operator adds numbers together or adds numbers and dates. When adding numbers and dates, the numbers are interpreted as milliseconds. + +## Syntax + +```javascript +{ + $add: [ ] +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **``** | Any valid expressions that resolve to numbers or dates. The expressions can be any combination of numbers and dates. | + +## 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 - Get the current and project staff count + +To calculate the total staff and project the total staff looking forward, use the $add operator on the nested totalStaff object to return the desired results. + +```javascript +db.stores.aggregate([{ + $match: { + _id: "40d6f4d7-50cd-4929-9a07-0a7a133c2e74" + } +}, { + $project: { + name: 1, + currentTotalStaff: { + $add: ["$staff.totalStaff.fullTime", "$staff.totalStaff.partTime"] + }, + projectedNextYearStaff: { + $add: ["$staff.totalStaff.fullTime", "$staff.totalStaff.partTime", 2] + } + } +}]) +``` + +This query returns the following result: + +```json +[ + { + "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74", + "name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury", + "currentTotalStaff": 39, + "projectedNextYearStaff": 41 + } +] +``` diff --git a/reference/operators/arithmetic-expression/$ceil.md b/reference/operators/arithmetic-expression/$ceil.md new file mode 100644 index 0000000..adde026 --- /dev/null +++ b/reference/operators/arithmetic-expression/$ceil.md @@ -0,0 +1,184 @@ +--- +title: $ceil +description: The $ceil operator returns the smallest integer greater than or equal to the specified number. +type: operators +category: arithmetic-expression +--- + +# $ceil + +The `$ceil` operator computes the ceiling of the input number. This operator returns the smallest integer value that is greater than or equal to the input. + +## Syntax + +```javascript +{ + $ceil: +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **``** | The input number whose ceiling needs to be returned. For a null or missing field, $ceil returns null. | + +## 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 - Calculate the ceiling of average sales volume per employee + +To calculate the ceiling of the sales volume per employee, first run a query to divide the total sales for the store by the number of staff. Then, use the $ceil operator to return the ceiling of the calculated value. + +```javascript +db.stores.aggregate([{ + $match: { + _id: "40d6f4d7-50cd-4929-9a07-0a7a133c2e74" + } + }, + { + $project: { + name: 1, + totalSales: "$sales.totalSales", + totalStaff: { + $add: ["$staff.totalStaff.fullTime", "$staff.totalStaff.partTime"] + }, + ceiledAverageSalesPerStaff: { + $ceil: { + $divide: [ + "$sales.totalSales", + { + $add: ["$staff.totalStaff.fullTime", "$staff.totalStaff.partTime"] + } + ] + } + } + } + } +]) +``` + +This query returns the following result: + +```json +[ + { + "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74", + "name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury", + "totalSales": 151864, + "totalStaff": 39, + "ceiledAverageSalesPerStaff": 3894 + } +] +``` diff --git a/reference/operators/arithmetic-expression/$divide.md b/reference/operators/arithmetic-expression/$divide.md new file mode 100644 index 0000000..7e01e1d --- /dev/null +++ b/reference/operators/arithmetic-expression/$divide.md @@ -0,0 +1,188 @@ +--- +title: $divide +description: The $divide operator divides two numbers and returns the quotient. +type: operators +category: arithmetic-expression +--- + +# $divide + +The `$divide` operator divides two numbers and returns the quotient. The $divide operator returns an error if the divisor is zero. + +## Syntax + +```javascript +{ + $divide: [ , ] +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **``** | Any valid expression that resolves to a number to be divided. | +| **``** | Any valid expression that resolves to a nonzero number to divide by. | + +## 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 - Calculate the average sales volume per employee + +To calculate the average sales volume per employee, first run a query using the $divide operator to divide the total sales by the staff count. To calculate the percentage of full time staff, use the $divide operator to dive the number of full time staff by the total staff count and project the result as a percentage. + +```javascript +db.stores.aggregate([{ + $match: { + _id: "40d6f4d7-50cd-4929-9a07-0a7a133c2e74" + } + }, + { + $project: { + name: 1, + averageSalesPerStaff: { + $divide: [ + "$sales.totalSales", + { + $add: ["$staff.totalStaff.fullTime", "$staff.totalStaff.partTime"] + } + ] + }, + fullTimeStaffPercentage: { + $multiply: [{ + $divide: [ + "$staff.totalStaff.fullTime", + { + $add: ["$staff.totalStaff.fullTime", "$staff.totalStaff.partTime"] + } + ] + }, 100] + } + } + } +]) +``` + +This query returns the following result: + +```json +[ + { + "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74", + "name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury", + "averageSalesPerStaff": 3893.95, + "fullTimeStaffPercentage": 48.72 + } +] +``` diff --git a/reference/operators/arithmetic-expression/$exp.md b/reference/operators/arithmetic-expression/$exp.md new file mode 100644 index 0000000..60dec9c --- /dev/null +++ b/reference/operators/arithmetic-expression/$exp.md @@ -0,0 +1,191 @@ +--- +title: $exp +description: The $exp operator raises e to the specified exponent and returns the result +type: operators +category: arithmetic-expression +--- + +# $exp + +The `$exp` operator returns the value of e raised to the specified exponent. The mathematical constant e is approximately equal to 2.71828. + +## Syntax + +```javascript +{ + $exp: +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **``** | Any valid expression that resolves to a number. | + +## 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 - Calculate exponential growth rate + +To calculate the exponential growth rate of total sales volume of a store by 10% and 20% respectively, use the $exp operator to multiple the value of the totalSales field by e^0.1 and e^0.2. + +```javascript +db.stores.aggregate([{ + $match: { + _id: "40d6f4d7-50cd-4929-9a07-0a7a133c2e74" + } + }, + { + $project: { + name: 1, + currentSales: "$sales.totalSales", + projectedGrowth: { + oneYear: { + $multiply: [ + "$sales.totalSales", + { + $exp: 0.1 + } // 10% growth rate + ] + }, + twoYears: { + $multiply: [ + "$sales.totalSales", + { + $exp: 0.2 + } // 20% growth rate + ] + } + } + } + } +]) +``` + +This query returns the following result: + +```json +[ + { + "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74", + "name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury", + "currentSales": 151864, + "projectedGrowth": { + "oneYear": 167809.93, + "twoYears": 185304.95 + } + } +] +``` diff --git a/reference/operators/arithmetic-expression/$floor.md b/reference/operators/arithmetic-expression/$floor.md new file mode 100644 index 0000000..9c3be01 --- /dev/null +++ b/reference/operators/arithmetic-expression/$floor.md @@ -0,0 +1,212 @@ +--- +title: $floor +description: The $floor operator returns the largest integer less than or equal to the specified number +type: operators +category: arithmetic-expression +--- + +# $floor + +The `$floor` operator returns the largest integer less than or equal to the specified number. + +## Syntax + +```javascript +{ + $floor: +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **``** | Any valid expression that resolves to a number. | + +## 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 - Calculate the floor of total sales and discounts + +To calculate the floor of the average sales volume for a given store and the floor of sales per category, run a query using the $floor operator to return the desired results. + +```javascript +db.stores.aggregate([{ + $match: { + _id: "40d6f4d7-50cd-4929-9a07-0a7a133c2e74" + } + }, + { + $project: { + name: 1, + averageSalesFloor: { + $floor: { + $divide: [ + "$sales.totalSales", + { + $size: "$sales.salesByCategory" + } + ] + } + }, + categoriesWithFloorSales: { + $map: { + input: "$sales.salesByCategory", + as: "category", + in: { + categoryName: "$$category.categoryName", + floorSales: { + $floor: "$$category.totalSales" + } + } + } + } + } + } +]) +``` + +This query returns the following result: + +```json +[ + { + "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74", + "name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury", + "averageSalesFloor": 30372, + "categoriesWithFloorSales": [ + { + "categoryName": "Sound Bars", + "floorSales": 2120 + }, + { + "categoryName": "Home Theater Projectors", + "floorSales": 45004 + }, + { + "categoryName": "Game Controllers", + "floorSales": 43522 + }, + { + "categoryName": "Remote Controls", + "floorSales": 28946 + }, + { + "categoryName": "VR Games", + "floorSales": 32272 + } + ] + } +] +``` diff --git a/reference/operators/arithmetic-expression/$ln.md b/reference/operators/arithmetic-expression/$ln.md new file mode 100644 index 0000000..6e04dfe --- /dev/null +++ b/reference/operators/arithmetic-expression/$ln.md @@ -0,0 +1,207 @@ +--- +title: $ln +description: The $ln operator calculates the natural logarithm of the input +type: operators +category: arithmetic-expression +--- + +# $ln + +The `$ln` operator calculates the natural logarithm (base e) of the input number. + +## Syntax + +```javascript +{ + $ln: +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **``** | Any valid expression that resolves to a positive number. | + +## 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 - Calculate the natural logarithm of total sales by category + +To calculate the natural logarithm of sales volume by category to analyze growth rates, run a query using the $ln operator on the totalSales field to return the desired result. + +```javascript +db.stores.aggregate([{ + $match: { + _id: "40d6f4d7-50cd-4929-9a07-0a7a133c2e74" + } + }, + { + $project: { + name: 1, + salesGrowthMetrics: { + $map: { + input: "$sales.salesByCategory", + as: "category", + in: { + categoryName: "$$category.categoryName", + salesValue: "$$category.totalSales", + naturalLog: { + $ln: "$$category.totalSales" + } + } + } + } + } + } +]) +``` + +This query returns the following result: + +```json +[ + { + "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74", + "name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury", + "salesGrowthMetrics": [ + { + "categoryName": "Sound Bars", + "salesValue": 2120, + "naturalLog": 7.659 + }, + { + "categoryName": "Home Theater Projectors", + "salesValue": 45004, + "naturalLog": 10.714 + }, + { + "categoryName": "Game Controllers", + "salesValue": 43522, + "naturalLog": 10.681 + }, + { + "categoryName": "Remote Controls", + "salesValue": 28946, + "naturalLog": 10.273 + }, + { + "categoryName": "VR Games", + "salesValue": 32272, + "naturalLog": 10.382 + } + ] + } +] +``` diff --git a/reference/operators/arithmetic-expression/$log.md b/reference/operators/arithmetic-expression/$log.md new file mode 100644 index 0000000..cb4fa7e --- /dev/null +++ b/reference/operators/arithmetic-expression/$log.md @@ -0,0 +1,216 @@ +--- +title: $log +description: The $log operator calculates the logarithm of a number in the specified base +type: operators +category: arithmetic-expression +--- + +# $log + +The `$log` operator calculates the logarithm of a number in the specified base. + +## Syntax + +```javascript +{ + $log: [ , ] +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **``** | Any valid expression that resolves to a positive number. | +| **``** | Any valid expression that resolves to a positive number to be used as the logarithm base. | + +## 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 - Calculate the log of total sales + +To calculate the log of sales volumes per category in base 2 and 10, run a query using the $log operator on the totalSales field with bases 2 and 10 respectively to return the desired result. + +```javascript +db.stores.aggregate([{ + $match: { + _id: "40d6f4d7-50cd-4929-9a07-0a7a133c2e74" + } + }, + { + $project: { + name: 1, + categoryAnalysis: { + $map: { + input: "$sales.salesByCategory", + as: "category", + in: { + categoryName: "$$category.categoryName", + sales: "$$category.totalSales", + logBase2: { + $log: ["$$category.totalSales", 2] + }, + logBase10: { + $log: ["$$category.totalSales", 10] + } + } + } + } + } + } +]) +``` + +This query returns the following result: + +```json +[ + { + "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74", + "name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury", + "categoryAnalysis": [ + { + "categoryName": "Sound Bars", + "sales": 2120, + "logBase2": 11.051, + "logBase10": 3.326 + }, + { + "categoryName": "Home Theater Projectors", + "sales": 45004, + "logBase2": 15.458, + "logBase10": 4.653 + }, + { + "categoryName": "Game Controllers", + "sales": 43522, + "logBase2": 15.410, + "logBase10": 4.639 + }, + { + "categoryName": "Remote Controls", + "sales": 28946, + "logBase2": 14.822, + "logBase10": 4.462 + }, + { + "categoryName": "VR Games", + "sales": 32272, + "logBase2": 14.977, + "logBase10": 4.509 + } + ] + } +] +``` diff --git a/reference/operators/arithmetic-expression/$log10.md b/reference/operators/arithmetic-expression/$log10.md new file mode 100644 index 0000000..3923ff5 --- /dev/null +++ b/reference/operators/arithmetic-expression/$log10.md @@ -0,0 +1,242 @@ +--- +title: $log10 +description: The $log10 operator calculates the log of a specified number in base 10 +type: operators +category: arithmetic-expression +--- + +# $log10 + +The `$log10` operator calculates the logarithm of a number in base 10 and returns the result. + +## Syntax + +```javascript +{ + $log10: +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **``** | Any valid expression that resolves to a positive number. | + +## 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 - Analyze sales distribution + +To bucket the distribution of sales per category within a store, run a query using the $log10 operator on the totalSales field. Then, bucket the categories into "Low", "Medium" and "High" based on the result. + +```javascript +db.stores.aggregate([{ + $match: { + _id: "40d6f4d7-50cd-4929-9a07-0a7a133c2e74" + } + }, + { + $project: { + name: 1, + salesAnalysis: { + $map: { + input: "$sales.salesByCategory", + as: "category", + in: { + categoryName: "$$category.categoryName", + originalSales: "$$category.totalSales", + logScale: { + $log10: "$$category.totalSales" + }, + magnitudeClass: { + $switch: { + branches: [{ + case: { + $lt: [{ + $log10: "$$category.totalSales" + }, 3] + }, + then: "Low" + }, + { + case: { + $lt: [{ + $log10: "$$category.totalSales" + }, 4] + }, + then: "Medium" + }, + { + case: { + $lt: [{ + $log10: "$$category.totalSales" + }, 5] + }, + then: "High" + } + ], + default: "Very High" + } + } + } + } + } + } + } +]) +``` + +This query returns the following result: + +```json +[ + { + "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74", + "name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury", + "salesAnalysis": [ + { + "categoryName": "Sound Bars", + "originalSales": 2120, + "logScale": 3.326, + "magnitudeClass": "Medium" + }, + { + "categoryName": "Home Theater Projectors", + "originalSales": 45004, + "logScale": 4.653, + "magnitudeClass": "High" + }, + { + "categoryName": "Game Controllers", + "originalSales": 43522, + "logScale": 4.639, + "magnitudeClass": "High" + }, + { + "categoryName": "Remote Controls", + "originalSales": 28946, + "logScale": 4.462, + "magnitudeClass": "High" + }, + { + "categoryName": "VR Games", + "originalSales": 32272, + "logScale": 4.509, + "magnitudeClass": "High" + } + ] + } +] +``` diff --git a/reference/operators/arithmetic-expression/$multiply.md b/reference/operators/arithmetic-expression/$multiply.md new file mode 100644 index 0000000..9bc9bbb --- /dev/null +++ b/reference/operators/arithmetic-expression/$multiply.md @@ -0,0 +1,196 @@ +--- +title: $multiply +description: The $multiply operator multiplies the input numerical values +type: operators +category: arithmetic-expression +--- + +# $multiply + +The `$multiply` operator calculates the product of the specified input numerical values. + +## Syntax + +```javascript +{ + $multiply: [ ] +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +|**``**| A comma separated list of numerical values. + +## 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: Multiply a field by a constant + +To double the total sales for all stores under the "First Up Consultants" company, first run a query to filter on the name of the company. Then, use the $multiply operator on the totalSales field to return the desired results. + +```javascript +db.stores.aggregate([{ + $match: { + company: { + $in: ["First Up Consultants"] + } + } +}, { + $project: { + company: 1, + "sales.revenue": 1, + salesVolumeDoubled: { + $multiply: ["$sales.revenue", 2] + } + } +}]) +``` + +The first three results returned by this query are: + +```json +[ + { + "_id": "39acb3aa-f350-41cb-9279-9e34c004415a", + "sales": { + "revenue": 279183 + }, + "company": "First Up Consultants", + "salesVolumeDoubled": 558366 + }, + { + "_id": "26afb024-53c7-4e94-988c-5eede72277d5", + "sales": { + "revenue": 50000 + }, + "company": "First Up Consultants", + "salesVolumeDoubled": 100000 + }, + { + "_id": "62438f5f-0c56-4a21-8c6c-6bfa479494ad", + "sales": { + "revenue": 68508 + }, + "company": "First Up Consultants", + "salesVolumeDoubled": 137016 + } +] +``` + +## Limitations + +- The `$multiply` operator works only with numerical expressions. Using it with non-numerical values result in an error. +- Be cautious of overflow or precision issues when working with large numbers or floating-point arithmetic. diff --git a/reference/operators/arithmetic-expression/$pow.md b/reference/operators/arithmetic-expression/$pow.md new file mode 100644 index 0000000..d981e02 --- /dev/null +++ b/reference/operators/arithmetic-expression/$pow.md @@ -0,0 +1,192 @@ +--- +title: $pow +description: The `$pow` operator calculates the value of a numerical value raised to the power of a specified exponent. +type: operators +category: arithmetic-expression +--- + +# $pow + +The `$pow` operator calculates the value of a number raised to a specified exponent. + +## Syntax + +```javascript +{ + $pow: [ , ] +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **``** | The base number to be raised to the exponent. | +| **``** | The exponent to raise the base number to. | + +## 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: Calculate the square of total sales volume + +To calculate the square of the sales volume of all stores under the "First Up Consultants" company, first run a query to filter on the name of the company. Then, use the $power operator on the nested fullSales field to calculate the desired result. + +```javascript +db.stores.aggregate([{ + $match: { + company: { + $in: ["First Up Consultants"] + } + } +}, { + $project: { + company: 1, + "sales.revenue": 1, + fullSalesSquare: { + $pow: ["$sales.revenue", 2] + } + } +}]) +``` + +The first three results returned by this query are: + +```json +[ + { + "_id": "39acb3aa-f350-41cb-9279-9e34c004415a", + "sales": { + "revenue": 279183 + }, + "company": "First Up Consultants", + "salesVolumeDoubled": 558366 + }, + { + "_id": "26afb024-53c7-4e94-988c-5eede72277d5", + "sales": { + "revenue": 50000 + }, + "company": "First Up Consultants", + "salesVolumeDoubled": 100000 + }, + { + "_id": "62438f5f-0c56-4a21-8c6c-6bfa479494ad", + "sales": { + "revenue": 68508 + }, + "company": "First Up Consultants", + "salesVolumeDoubled": 137016 + } +] +``` diff --git a/reference/operators/arithmetic-expression/$round.md b/reference/operators/arithmetic-expression/$round.md new file mode 100644 index 0000000..cdd4421 --- /dev/null +++ b/reference/operators/arithmetic-expression/$round.md @@ -0,0 +1,249 @@ +--- +title: $round +description: The $round operator rounds a number to a specified decimal place. +type: operators +category: arithmetic-expression +--- + +# $round + +The `$round` operator is used to round a number to a specified decimal place. It's useful in aggregations where numerical precision is important, such as financial calculations or statistical analysis. + +## Syntax + +```javascript +{ + $round: [ , ] +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **``** | The number to be rounded. | +| **``** | The decimal place to which the number should be rounded. | + +## 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 - Round the location coordinates of stores + +To round the latitude and longitude of all stores within the "First Up Consultants" company, first run a query to filter on the name of the company. Then, use the $round operator on the lat and lon fields to return the desired result. + +```javascript +db.stores.aggregate([{ + $match: { + company: { + $in: ["First Up Consultants"] + } + } +}, { + $project: { + company: 1, + "location.lat": 1, + "location.lon": 1, + roundedLat: { + $round: ["$location.lat", 1] + }, + roundedLon: { + $round: ["$location.lon", 1] + } + } +}]) +``` + +The first three results returned by this query are: + +```json +[ + { + "_id": "39acb3aa-f350-41cb-9279-9e34c004415a", + "location": { + "lat": 87.2239, + "lon": -129.0506 + }, + "company": "First Up Consultants", + "roundedLat": 87.2, + "roundedLon": -129.1 + }, + { + "_id": "26afb024-53c7-4e94-988c-5eede72277d5", + "location": { + "lat": -29.1866, + "lon": -112.7858 + }, + "company": "First Up Consultants", + "roundedLat": -29.2, + "roundedLon": -112.8 + }, + { + "_id": "62438f5f-0c56-4a21-8c6c-6bfa479494ad", + "location": { + "lat": -0.2136, + "lon": 108.7466 + }, + "company": "First Up Consultants", + "roundedLat": -0.2, + "roundedLon": 108.7 + } +] +``` + +### Example 2 - Round to the nearest thousand + +To round the total sales volume of stores within the "First Up Consultants" company, first run a query to filter stores by the company name. Then use the $round operator on the totalSales field to round the value to the nearest thousand. + +```javascript +db.stores.aggregate([{ + $match: { + company: { + $in: ["First Up Consultants"] + } + } +}, { + $project: { + company: 1, + "sales.totalSales": 1, + roundedSales: { + $round: ["$sales.totalSales", -3] + } + } +}]) +``` + +The first three results returned by this query are: + +```json +[ + { + "_id": "39acb3aa-f350-41cb-9279-9e34c004415a", + "sales": {}, + "company": "First Up Consultants", + "roundedSales": 279000 + }, + { + "_id": "26afb024-53c7-4e94-988c-5eede72277d5", + "sales": {}, + "company": "First Up Consultants", + "roundedSales": 50000 + }, + { + "_id": "62438f5f-0c56-4a21-8c6c-6bfa479494ad", + "sales": {}, + "company": "First Up Consultants", + "roundedSales": 69000 + } +] +``` diff --git a/reference/operators/arithmetic-expression/$sqrt.md b/reference/operators/arithmetic-expression/$sqrt.md new file mode 100644 index 0000000..2357349 --- /dev/null +++ b/reference/operators/arithmetic-expression/$sqrt.md @@ -0,0 +1,220 @@ +--- +title: $sqrt +description: The $sqrt operator calculates and returns the square root of an input number +type: operators +category: arithmetic-expression +--- + +# $sqrt + +The `$sqrt` operator is used to calculate the square root of a specified number. + +## Syntax + +```javascript +{ + $sqrt: +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **``**| Any valid expression that resolves to a number. | + +## 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: Calculate the square root of sales + +To calculate the square root of the sales volumes of each store under the "First Up Consultants" company, first run a query to filter stores by the company name. Then, use the $sqrt operator on the totalSales field to retrieve the desired results. + +```javascript +db.stores.aggregate([{ + $match: { + company: { + $in: ["First Up Consultants"] + } + } +}, { + $project: { + name: 1, + "sales.revenue": 1, + categoryName: "$promotionEvents.discounts.categoryName", + sqrtFullSales: { + $sqrt: "$sales.revenue" + } + } +}]) +``` + +The first two results returned by this query are: + +```json +[ + { + "_id": "c52c9f65-5b1a-4ef5-a7a2-d1af0426cbe4", + "name": "First Up Consultants | Jewelry Pantry - Nicolasberg", + "sales": { + "revenue": 4624 + }, + "categoryName": [ + [ + "Watches", + "Bracelets" + ], + [ + "Brooches", + "Necklaces" + ], + [ + "Charms", + "Brooches" + ], + [ + "Brooches", + "Anklets" + ], + [ + "Earrings", + "Anklets" + ] + ], + "sqrtFullSales": 68 + }, + { + "_id": "176aa484-c21c-44ce-ab6d-5e097bbdc2b4", + "name": "First Up Consultants | Medical Supply Shop - Daughertyville", + "sales": { + "revenue": 67311 + }, + "categoryName": [ + [ + "First Aid Kits", + "OTC Medications" + ], + [ + "Blood Pressure Monitors", + "OTC Medications" + ], + [ + "Face Masks", + "Stethoscopes" + ] + ], + "sqrtFullSales": 259.44363549719236 + } +] +``` diff --git a/reference/operators/arithmetic-expression/$subtract.md b/reference/operators/arithmetic-expression/$subtract.md new file mode 100644 index 0000000..d271085 --- /dev/null +++ b/reference/operators/arithmetic-expression/$subtract.md @@ -0,0 +1,203 @@ +--- +title: $subtract +description: The $subtract operator subtracts two numbers and returns the result. +type: operators +category: arithmetic-expression +--- + +# $subtract + +The `$subtract` operator is used to subtract two numbers and return the result. + +## Syntax + +```javascript +{ + $subtract: [ , ] +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **``** | The minuend (the number from which another number is to be subtracted). | +| **``** | The subtrahend (the number to be subtracted). | + +## 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: Calculating the difference between full time and part time staff + +To calculate the absolute difference in part time and full time staff for stores within the "First Up Consultants" company, first run a query to filter stores by the company name. Then, use the $diff operator along with the $abs operator to calculate the absolute difference between the full time and part time staff for each store. + +```javascript +db.stores.aggregate([{ + $match: { + company: { + $in: ["First Up Consultants"] + } + } +}, { + $project: { + name: 1, + staff: 1, + staffCountDiff: { + $abs: { + $subtract: ["$staff.employeeCount.fullTime", "$staff.employeeCount.partTime"] + } + } + } +}]) +``` + +The first three results returned by this query are: + +```json +[ + { + "_id": "62438f5f-0c56-4a21-8c6c-6bfa479494ad", + "name": "First Up Consultants | Plumbing Supply Shoppe - New Ubaldofort", + "staff": { + "employeeCount": { + "fullTime": 20, + "partTime": 18 + } + }, + "staffCountDiff": 2 + }, + { + "_id": "bfb213fa-8db8-419f-8e5b-e7096120bad2", + "name": "First Up Consultants | Beauty Product Shop - Hansenton", + "staff": { + "employeeCount": { + "fullTime": 18, + "partTime": 10 + } + }, + "staffCountDiff": 8 + }, + { + "_id": "14ab145b-0819-4d22-9e02-9ae0725fcda9", + "name": "First Up Consultants | Flooring Haven - Otisville", + "staff": { + "employeeCount": { + "fullTime": 19, + "partTime": 10 + } + }, + "staffCountDiff": 9 + } +] +``` diff --git a/reference/operators/arithmetic-expression/$trunc.md b/reference/operators/arithmetic-expression/$trunc.md new file mode 100644 index 0000000..9016535 --- /dev/null +++ b/reference/operators/arithmetic-expression/$trunc.md @@ -0,0 +1,190 @@ +--- +title: $trunc +description: The $trunc operator truncates a number to a specified decimal place. +type: operators +category: arithmetic-expression +--- + +# $trunc + +The `$trunc` operator truncates a number to a specified decimal place. + +## Syntax + +```javascript +{ + $trunc: [ , ] +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **``** | The number to truncate. | +| **``** | The decimal place to truncate the specified number to. A positive value truncates to the right of the decimal point, and a negative value truncates to the left of the decimal point. | + +## 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 - Fetch truncated location coordinates + +To retrieve the truncated coordinates of stores within the "First Up Consultants" company, first run a query to filter stores by the company name. Then, use the $trunc operator on the latitude and longitude fields to return the desired result. + +```javascript +db.stores.aggregate([ + { + $project: { + truncatedLat: { $trunc: ["$location.lat", 2] } + } + } +]) +``` + +The first three results returned by this query are: + +```json +[ + { + "_id": "39acb3aa-f350-41cb-9279-9e34c004415a", + "name": "First Up Consultants | Bed and Bath Pantry - Port Antone", + "location": { + "lat": 87.2239, + "lon": -129.0506 + }, + "truncatedLatitute": 87, + "truncatedLongitude": -129 + }, + { + "_id": "26afb024-53c7-4e94-988c-5eede72277d5", + "name": "First Up Consultants | Microphone Bazaar - South Lexusland", + "location": { + "lat": -29.1866, + "lon": -112.7858 + }, + "truncatedLatitute": -29, + "truncatedLongitude": -112 + }, + { + "_id": "62438f5f-0c56-4a21-8c6c-6bfa479494ad", + "name": "First Up Consultants | Plumbing Supply Shoppe - New Ubaldofort", + "location": { + "lat": -0.2136, + "lon": 108.7466 + }, + "truncatedLatitute": 0, + "truncatedLongitude": 108 + } +] +```