diff --git a/reference/operators/array-expression/$arrayelemat.md b/reference/operators/array-expression/$arrayelemat.md new file mode 100644 index 0000000..201aabe --- /dev/null +++ b/reference/operators/array-expression/$arrayelemat.md @@ -0,0 +1,179 @@ +--- +title: $arrayElemAt +description: The $arrayElemAt returns the element at the specified array index. +type: operators +category: array-expression +--- + +# $arrayElemAt + +The `$arrayElemAt` operator is used to return the element at the specified array index. This operator is helpful when you need to extract a specific element from an array within your documents. + +## Syntax + +```javascript +{ + $arrayElemAt: ["", ] +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **``**| The array reference from which the element is retrieved.| +| **``**| The index of the element to return. The index is zero-based. A negative index counts from the end of the array.| + +## Examples + +Consider this sample document from the stores collection. + +```json +{ + "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5", + "name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile", + "location": { + "lat": 60.1441, + "lon": -141.5012 + }, + "staff": { + "totalStaff": { + "fullTime": 2, + "partTime": 0 + } + }, + "sales": { + "salesByCategory": [ + { + "categoryName": "DJ Cables", + "totalSales": 1000 + }, + { + "categoryName": "DJ Headphones", + "totalSales": 35921 + } + ], + "fullSales": 3700 + }, + "promotionEvents": [ + { + "eventName": "Cyber Monday Event", + "promotionalDates": { + "startDate": { + "Year": 2024, + "Month": 8, + "Day": 1 + }, + "endDate": { + "Year": 2024, + "Month": 8, + "Day": 7 + } + }, + "discounts": [ + { + "categoryName": "DJ Speakers", + "discountPercentage": 25 + } + ] + }, + { + "eventName": "Black Friday Event", + "promotionalDates": { + "startDate": { + "Year": 2024, + "Month": 8, + "Day": 1 + }, + "endDate": { + "Year": 2024, + "Month": 8, + "Day": 7 + } + }, + "discounts": [ + { + "categoryName": "DJ Speakers", + "discountPercentage": 25 + } + ] + }, + { + "eventName": "Mega Discount Days", + "promotionalDates": { + "startDate": { + "Year": 2024, + "Month": 5, + "Day": 11 + }, + "endDate": { + "Year": 2024, + "Month": 5, + "Day": 18 + } + }, + "discounts": [ + { + "categoryName": "DJ Lights", + "discountPercentage": 20 + } + ] + } + ], + "tag": [ + "#ShopLocal", + "#NewArrival", + "#FashionStore", + "#SeasonalSale", + "#FreeShipping", + "#MembershipDeals" + ] +} + +``` + +### Example 1: Return the first element from an array field + +This query retrieves the first event details from `promotionEvents` array for the searched store. + +```javascript +db.stores.aggregate([ + { $match: { name: "Lakeshore Retail | DJ Equipment Stop - Port Cecile" } }, + { + $project: { + firstPromotionEvent: { $arrayElemAt: ["$promotionEvents", 0] } + } + } +]) +``` + +This query returns the following result. + +```json +[ + { + "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5", + "firstPromotionEvent": { + "eventName": "Cyber Monday Event", + "promotionalDates": { + "startDate": { + "Year": 2024, + "Month": 8, + "Day": 1 + }, + "endDate": { + "Year": 2024, + "Month": 8, + "Day": 7 + } + }, + "discounts": [ + { + "categoryName": "DJ Speakers", + "discountPercentage": 25 + } + ] + } + } +] +``` diff --git a/reference/operators/array-expression/$arraytoobject.md b/reference/operators/array-expression/$arraytoobject.md new file mode 100644 index 0000000..602bedb --- /dev/null +++ b/reference/operators/array-expression/$arraytoobject.md @@ -0,0 +1,152 @@ +--- +title: $arrayToObject +description: The $arrayToObject allows converting an array into a single document. +type: operators +category: array-expression +--- + +# $arrayToObject + +The `$arrayToObject` operator is used to convert an array into a single document. This operator is useful when you need to transform arrays of key-value pairs into a more structured document format. + +## Syntax + +```javascript +{ + $arrayToObject: "" +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **``**| The array to be converted into a document. Each element in the array must be either: a) A two-element array where the first element is the field name and the second element is the field value. b) A document with exactly two fields, "k" and "v", where "k" is the field name and "v" is the field value.| + +## Examples + +Consider this sample document from the stores collection. + +```json +{ + "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5", + "name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile", + "location": { + "lat": 60.1441, + "lon": -141.5012 + }, + "staff": { + "totalStaff": { + "fullTime": 2, + "partTime": 0 + } + }, + "sales": { + "salesByCategory": [ + { + "categoryName": "DJ Headphones", + "totalSales": 35921 + }, + { + "categoryName": "DJ Cables", + "totalSales": 1000 + } + ], + "fullSales": 3700 + }, + "promotionEvents": [ + { + "eventName": "Bargain Blitz Days", + "promotionalDates": { + "startDate": { + "Year": 2024, + "Month": 3, + "Day": 11 + }, + "endDate": { + "Year": 2024, + "Month": 2, + "Day": 18 + } + }, + "discounts": [ + { + "categoryName": "DJ Turntables", + "discountPercentage": 18 + }, + { + "categoryName": "DJ Mixers", + "discountPercentage": 15 + } + ] + }, + { + "eventName": "Discount Delight Days", + "promotionalDates": { + "startDate": { + "Year": 2024, + "Month": 5, + "Day": 11 + }, + "endDate": { + "Year": 2024, + "Month": 5, + "Day": 18 + } + } + } + ], + "tag": [ + "#ShopLocal", + "#FashionStore", + "#SeasonalSale", + "#FreeShipping", + "#MembershipDeals" + ] +} +``` + +### Example 1: Convert the array to a key: value document + +This query converts the `salesByCategory` array into an object where each `categoryName` is a key and `totalSales` is the corresponding value. This transformation simplifies access to sales data by category directly from an object structure. + +```javascript +db.stores.aggregate([{ + $match: { + _id: "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5" + } + }, + { + $project: { + "sales.salesByCategory": { + $arrayToObject: { + $map: { + input: "$sales.salesByCategory", + as: "item", + in: { + k: "$$item.categoryName", + v: "$$item.totalSales" + } + } + } + } + } + } +]) +``` + +The query returns the following result. + +```json +[ + { + "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5", + "sales": { + "salesByCategory": { + "DJ Headphones": 35921, + "DJ Cables": 1000 + } + } + } +] +``` diff --git a/reference/operators/array-expression/$concatarrays.md b/reference/operators/array-expression/$concatarrays.md new file mode 100644 index 0000000..c37408b --- /dev/null +++ b/reference/operators/array-expression/$concatarrays.md @@ -0,0 +1,136 @@ +--- +title: $concatArrays +description: The $concatArrays is used to combine multiple arrays into a single array. +type: operators +category: array-expression +--- + +# $concatArrays + +The `$concatArrays` operator is used to combine multiple arrays into a single array. This operator is useful when you need to merge arrays from different documents or fields in a document. + +## Syntax + +```javascript +{ + $concatArrays: ["", ""] +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **`, `**| The array fields targeted for concatenation.| + +## Examples + +Consider this sample document from the stores collection. + +```json +{ + "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5", + "name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile", + "location": { + "lat": 60.1441, + "lon": -141.5012 + }, + "staff": { + "totalStaff": { + "fullTime": 2, + "partTime": 0 + } + }, + "sales": { + "salesByCategory": [ + { + "categoryName": "DJ Headphones", + "totalSales": 35921 + }, + { + "categoryName": "DJ Cables", + "totalSales": 1000 + } + ], + "fullSales": 3700 + }, + "promotionEvents": [ + { + "eventName": "Bargain Blitz Days", + "promotionalDates": { + "startDate": { + "Year": 2024, + "Month": 3, + "Day": 11 + }, + "endDate": { + "Year": 2024, + "Month": 2, + "Day": 18 + } + }, + "discounts": [ + { + "categoryName": "DJ Turntables", + "discountPercentage": 18 + }, + { + "categoryName": "DJ Mixers", + "discountPercentage": 15 + } + ] + }, + { + "eventName": "Discount Delight Days", + "promotionalDates": { + "startDate": { + "Year": 2024, + "Month": 5, + "Day": 11 + }, + "endDate": { + "Year": 2024, + "Month": 5, + "Day": 18 + } + } + } + ], + "tag": [ + "#ShopLocal", + "#FashionStore", + "#SeasonalSale", + "#FreeShipping", + "#MembershipDeals" + ] +} +``` + +### Example 1: Concatenating Arrays in a document + +This query merges the `categoryName` field from the `promotionEvents.discounts` array with the `tag` array into a single combinedTags array. + +```javascript +db.stores.aggregate([{ + $match: { + _id: "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5" + } +}, { + $project: { + combinedTags: { + $concatArrays: ["$promotionEvents.discounts.categoryName", "$tag"] + } + } +}]) +``` + +This query returns the following result. + +```json +[ + { + "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5", + "combinedTags": [ '#ShopLocal', '#NewArrival', '#NewArrival', '#FreeShipping' ] + } +] +``` diff --git a/reference/operators/array-expression/$filter.md b/reference/operators/array-expression/$filter.md new file mode 100644 index 0000000..c7cab69 --- /dev/null +++ b/reference/operators/array-expression/$filter.md @@ -0,0 +1,155 @@ +--- +title: $filter +description: The $filter operator filters for elements from an array based on a specified condition. +type: operators +category: array-expression +--- + +# $filter + +The `$filter` operator is used to filter elements from an array based on a specified condition. This operator is useful when you need to manipulate or retrieve specific array elements within documents. + +## Syntax + +```javascript +{ + $filter: { + input: "", + as: "", + cond: "" + } +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **`input`**| An expression that resolves to an array.| +| **`as`**| A string that specifies the variable name for each element in the input array.| +| **`cond`**| An expression that determines whether to include the element in the output array.| + +## Examples + +Consider this sample document from the stores collection. + +```json +{ + "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5", + "name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile", + "location": { + "lat": 60.1441, + "lon": -141.5012 + }, + "staff": { + "totalStaff": { + "fullTime": 2, + "partTime": 0 + } + }, + "sales": { + "salesByCategory": [ + { + "categoryName": "DJ Headphones", + "totalSales": 35921 + }, + { + "categoryName": "DJ Cables", + "totalSales": 1000 + } + ], + "fullSales": 3700 + }, + "promotionEvents": [ + { + "eventName": "Bargain Blitz Days", + "promotionalDates": { + "startDate": { + "Year": 2024, + "Month": 3, + "Day": 11 + }, + "endDate": { + "Year": 2024, + "Month": 2, + "Day": 18 + } + }, + "discounts": [ + { + "categoryName": "DJ Turntables", + "discountPercentage": 18 + }, + { + "categoryName": "DJ Mixers", + "discountPercentage": 15 + } + ] + }, + { + "eventName": "Discount Delight Days", + "promotionalDates": { + "startDate": { + "Year": 2024, + "Month": 5, + "Day": 11 + }, + "endDate": { + "Year": 2024, + "Month": 5, + "Day": 18 + } + } + } + ], + "tag": [ + "#ShopLocal", + "#FashionStore", + "#SeasonalSale", + "#FreeShipping", + "#MembershipDeals" + ] +} +``` + +### Example 1: Retrieve an element filtered on condition + +This query demonstrates how to filter sales category based on `totalSales`. + +```javascript +db.stores.aggregate([{ + $match: { + _id: "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5" + } + }, + { + $project: { + filteredSalesByCategory: { + $filter: { + input: "$sales.salesByCategory", + as: "item", + cond: { + $gt: ["$$item.totalSales", 10000] + } + } + } + } + } +]) +``` + +This query returns the following result. + +```json +[ + { + "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5", + "filteredSalesByCategory": [ + { + "categoryName": "DJ Headphones", + "totalSales": 35921 + } + ] + } +] +``` diff --git a/reference/operators/array-expression/$indexofarray.md b/reference/operators/array-expression/$indexofarray.md new file mode 100644 index 0000000..39ad134 --- /dev/null +++ b/reference/operators/array-expression/$indexofarray.md @@ -0,0 +1,179 @@ +--- +title: $indexOfArray +description: The $indexOfArray operator is used to search for an element in an array and return the index of the first occurrence of the element. +type: operators +category: array-expression +--- + +# $indexOfArray + +The `$indexOfArray` operator is used to search for an element in an array and return the index of the first occurrence of the element. If the element isn't found, it returns `-1`. This operator is useful for queries where you need to determine the position of an element within an array. For example, finding the index of a specific value or object in a list. + +## Syntax + +```javascript +{ + $indexOfArray: [ < array > , < searchElement > , < start > , < end > ] +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **``**| The array in which you want to search for the element.| +| **``**| The element you're searching for in the array.| +| **``**| (Optional) The index from which to start the search. If omitted, the search starts from the beginning of the array.| +| **``**| (Optional) The index at which to end the search. If omitted, the search goes until the end of the array.| + +## Examples + +Consider this sample document from the stores collection. + +```json +{ + "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5", + "name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile", + "location": { + "lat": 60.1441, + "lon": -141.5012 + }, + "staff": { + "totalStaff": { + "fullTime": 2, + "partTime": 0 + } + }, + "sales": { + "salesByCategory": [ + { + "categoryName": "DJ Headphones", + "totalSales": 35921 + } + ], + "fullSales": 3700 + }, + "promotionEvents": [ + { + "eventName": "Bargain Blitz Days", + "promotionalDates": { + "startDate": { + "Year": 2024, + "Month": 3, + "Day": 11 + }, + "endDate": { + "Year": 2024, + "Month": 2, + "Day": 18 + } + }, + "discounts": [ + { + "categoryName": "DJ Turntables", + "discountPercentage": 18 + }, + { + "categoryName": "DJ Mixers", + "discountPercentage": 15 + } + ] + } + ], + "tag": [ + "#ShopLocal", + "#SeasonalSale", + "#FreeShipping", + "#MembershipDeals" + ] +} +``` + +### Example 1: Finding the index of the first occurrence + +This query finds the position (index) of a specific category name ("DJ Headphones") inside the `salesByCategory` array across the collection. + +```javascript +db.stores.aggregate([ + { + $project: { + index: { + $indexOfArray: [ + "$sales.salesByCategory.categoryName", + "DJ Headphones" + ] + } + } + }, + // Limit the result to the first 3 documents + { $limit: 3 } +]) +``` + +This query returns the following results: + +```json +[ + { + "_id": "649626c9-eda1-46c0-a27f-dcee19d97f41", + "index": -1 + }, + { + "_id": "8345de34-73ec-4a99-9cb6-a81f7b145c34", + "index": -1 + }, + { + "_id": "57cc4095-77d9-4345-af20-f8ead9ef0197", + "index": -1 + } +] +``` + +### Example 2: Finding the index in a range + +This query finds the position of the "Bargain Blitz Days" promotion event inside the `promotionEvents` array within a specific range of indexes (3 to 5) and filters results along with returning the first three matching documents + +```javascript +db.stores.aggregate([ + // Step 1: Project the index of the "Bargain Blitz Days" event name within the specified range + { + $project: { + index: { + $indexOfArray: [ + "$promotionEvents.eventName", + "Bargain Blitz Days", + 3, + 5 + ] + } + } + }, + // Step 2: Match documents where index > 0 + { + $match: { + index: { $gt: 0 } + } + }, + // Limit the result to the first 3 documents + { $limit: 3 } +]) +``` + +This query returns the following results: + +```json + [ + { + "_id": "ced8caf0-051a-48ce-88d3-2935815261c3", + "index": 3 + }, + { + "_id": "509be7ce-539a-41b5-8fde-b85fb3ef3faa", + "index": 3 + }, + { + "_id": "d06e8136-9a7f-4b08-92c8-dc8eac73bad3", + "index": 3 + } +] +``` diff --git a/reference/operators/array-expression/$isarray.md b/reference/operators/array-expression/$isarray.md new file mode 100644 index 0000000..5253b8f --- /dev/null +++ b/reference/operators/array-expression/$isarray.md @@ -0,0 +1,160 @@ +--- +title: $isArray +description: The $isArray operator is used to determine if a specified value is an array. +type: operators +category: array-expression +--- + +# $isArray + +The `$isArray` operator is used to determine if a specified value is an array. It returns `true` if the value is an array and `false` otherwise. This operator is often used in aggregation pipelines to filter or transform documents based on whether a field contains an array. + +## Syntax + +```javascript +{ + $isArray: +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **``**| Any valid expression that resolves to a value you want to check.| + +## Examples + +Consider this sample document from the stores collection. + +```json +{ + "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5", + "name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile", + "location": { + "lat": 60.1441, + "lon": -141.5012 + }, + "staff": { + "totalStaff": { + "fullTime": 2, + "partTime": 0 + } + }, + "sales": { + "salesByCategory": [ + { + "categoryName": "DJ Headphones", + "totalSales": 35921 + } + ], + "fullSales": 3700 + }, + "promotionEvents": [ + { + "eventName": "Bargain Blitz Days", + "promotionalDates": { + "startDate": { + "Year": 2024, + "Month": 3, + "Day": 11 + }, + "endDate": { + "Year": 2024, + "Month": 2, + "Day": 18 + } + }, + "discounts": [ + { + "categoryName": "DJ Turntables", + "discountPercentage": 18 + }, + { + "categoryName": "DJ Mixers", + "discountPercentage": 15 + } + ] + } + ], + "tag": [ + "#ShopLocal", + "#SeasonalSale", + "#FreeShipping", + "#MembershipDeals" + ] +} +``` + +### Example 1: Checking if a field is an array + +This query checks whether the `sales.salesByCategory` field in each store document is an array and returns that information for the first three documents. + +```javascript +db.stores.aggregate([ + { + $project: { + _id: 1, + isSalesByCategoryArray: { $isArray: "$sales.salesByCategory" } + } + }, + // Limit the result to the first 3 documents + { $limit: 3 } +]) +``` + +The first three results returned by this query are: + +```json +[ + { + "_id": "649626c9-eda1-46c0-a27f-dcee19d97f41", + "isSalesByCategoryArray": true + }, + { + "_id": "8345de34-73ec-4a99-9cb6-a81f7b145c34", + "isSalesByCategoryArray": true + }, + { + "_id": "57cc4095-77d9-4345-af20-f8ead9ef0197", + "isSalesByCategoryArray": true + } +] +``` + +### Example 2: Filtering documents based on array fields + +This query demonstrates use of `$isArray` to filter documents where the `promotionEvents` field is an array. + +```javascript +db.stores.aggregate([ + { + $match: { + $expr: { $isArray: "$promotionEvents" } + } + }, + // Limit the result to the first 3 documents + { $limit: 3 }, + // Include only _id and name fields in the output + { $project: { _id: 1, name: 1 } } +]) +``` + +The first three results returned by this query are: + +```json +[ + { + "_id": "649626c9-eda1-46c0-a27f-dcee19d97f41", + "name": "VanArsdel, Ltd. | Musical Instrument Outlet - East Cassie" + }, + { + "_id": "8345de34-73ec-4a99-9cb6-a81f7b145c34", + "name": "Northwind Traders | Bed and Bath Place - West Oraland" + }, + { + "_id": "57cc4095-77d9-4345-af20-f8ead9ef0197", + "name": "Wide World Importers | Bed and Bath Store - West Vitafort" + } +] +``` diff --git a/reference/operators/array-expression/$map.md b/reference/operators/array-expression/$map.md new file mode 100644 index 0000000..8f3ca40 --- /dev/null +++ b/reference/operators/array-expression/$map.md @@ -0,0 +1,136 @@ +--- +title: $map +description: The $map operator allows applying an expression to each element in an array. +type: operators +category: array-expression +--- + +# $map + +The `$map` operator is used to apply an expression to each element in an array and return an array with the applied results. This operator is useful for transforming arrays within documents, such as modifying each element or extracting specific fields. + +## Syntax + +```javascript +{ + $map: { + input: , + as: , + in: + } +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **`input`** | The array processed by the expression. | +| **`as`** | The variable name for each element in the array. | +| **`in`** | The expression to apply to each element. | + +## Examples + +Consider this sample document from the stores collection. + +```json +{ + "_id": "988d2dd1-2faa-4072-b420-b91b95cbfd60", + "name": "Lakeshore Retail", + "location": { + "lat": -51.3041, + "lon": -166.0838 + }, + "staff": { + "totalStaff": { + "fullTime": 5, + "partTime": 20 + } + }, + "sales": { + "totalSales": 266491, + "salesByCategory": [ + { + "categoryName": "Towel Racks", + "totalSales": 13237 + }, + { + "categoryName": "Washcloths", + "totalSales": 44315 + }, + { + "categoryName": "Face Towels", + "totalSales": 42095 + }, + { + "categoryName": "Toothbrush Holders", + "totalSales": 47912 + }, + { + "categoryName": "Hybrid Mattresses", + "totalSales": 48660 + }, + { + "categoryName": "Napkins", + "totalSales": 31439 + }, + { + "categoryName": "Pillow Cases", + "totalSales": 38833 + } + ] + }, + "tag": [ + "#ShopLocal", + "#FashionStore", + "#SeasonalSale", + "#FreeShipping", + "#MembershipDeals" + ] +} +``` + +### Example 1: Extracting category names + +This query filters the `stores` collection for `_id`, then projects a new field `categoryNames` where each element in the salesByCategory array has its totalSales increased by 500 using the $map operator. + +```javascript +db.stores.aggregate([{ + $match: { + _id: "988d2dd1-2faa-4072-b420-b91b95cbfd60" + } + }, + { + $project: { + categoryNames: { + $map: { + input: "$sales.salesByCategory.totalSales", + as: "category", + in: { + $add: ["$$category", 500] + } + } + } + } + } +]) +``` + +This query returns the following result. + +```json +[ + { + "_id": "988d2dd1-2faa-4072-b420-b91b95cbfd60", + "categoryNames": [ + 13737, + 44815, + 42595, + 48412, + 49160, + 31939, + 39333 + ] + } +] +``` diff --git a/reference/operators/array-expression/$range.md b/reference/operators/array-expression/$range.md new file mode 100644 index 0000000..b74f34c --- /dev/null +++ b/reference/operators/array-expression/$range.md @@ -0,0 +1,209 @@ +--- +title: $range +description: The $range operator allows generating an array of sequential integers. +type: operators +category: array-expression +--- + +# $range + +The `$range` operator is used to generate an array of sequential integers. The operator helps create number arrays in a range, useful for pagination, indexing, or test data. + +## Syntax + +```javascript +{ + $range: [ , , ] +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **`start`** | The starting value of the range (inclusive). | +| **`end`** | The ending value of the range (exclusive). | +| **`step`** | The increment value between each number in the range (optional, defaults to 1). | + +## 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: Generate a range of numbers + +This query demonstrates usage of operator to generate an array of integers from 0 to 5, wherein it includes the left boundary while excludes the right. + +```javascript +db.stores.aggregate([{ + $match: { + _id: "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6" + } +}, { + $project: { + rangeArray: { + $range: [0, 5] + } + } +}]) +``` + +This query returns the following result. + +```json +[ + { + "_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6", + "rangeArray": [ + 0, + 1, + 2, + 3, + 4 + ] + } +] +``` + +### Example 2: Generate a range of numbers with a step value + +This query demonstrates usage of operator to generate an array of even numbers from 0 to 18. + +```javascript +db.stores.aggregate([{ + $match: { + _id: "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6" + } +}, { + $project: { + evenNumbers: { + $range: [0, 8, 2] + } + } +}]) +``` + +This query results the following result. + +```json +[ + { + "_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6", + "rangeArray": [ + 0, + 2, + 4, + 6 + ] + } +] +``` diff --git a/reference/operators/array-expression/$reduce.md b/reference/operators/array-expression/$reduce.md new file mode 100644 index 0000000..cb16629 --- /dev/null +++ b/reference/operators/array-expression/$reduce.md @@ -0,0 +1,177 @@ +--- +title: $reduce +description: The $reduce operator applies an expression to each element in an array & accumulate result as single value. +type: operators +category: array-expression +--- + +# $reduce + +The `$reduce` operator is used to apply an expression to each element in an array and accumulate the results into a single value. This operator is useful for performing operations that require iterating over array elements and aggregating their values. + +## Syntax + +```javascript +$reduce: { + input: , + initialValue: , + in: +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **`input`** | The array to iterate over. | +| **`initialValue`** | The initial cumulative value set before the array iteration begins. | +| **`in`** | A valid expression that evaluates to the accumulated value for each element in the array. | + +## 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: Aggregates the array values + +This query demonstrates how to use `$reduce` to sum the total sales across different categories in the `salesByCategory` array. + +```javascript +db.stores.aggregate([{ + $match: { + _id: "988d2dd1-2faa-4072-b420-b91b95cbfd60" + } +}, { + $project: { + totalSalesByCategory: { + $reduce: { + input: "$sales.salesByCategory.totalSales", + initialValue: 0, + in: { + $add: ["$$value", "$$this"] + } + } + } + } +}]) +``` + +The query returns the following result. + +```json +[ + { + "_id": "988d2dd1-2faa-4072-b420-b91b95cbfd60", + "totalSalesByCategory": 149849 + } +] +``` diff --git a/reference/operators/array-expression/$reversearray.md b/reference/operators/array-expression/$reversearray.md new file mode 100644 index 0000000..14fdea6 --- /dev/null +++ b/reference/operators/array-expression/$reversearray.md @@ -0,0 +1,179 @@ +--- +title: $reverseArray +description: The $reverseArray operator is used to reverse the order of elements in an array. +type: operators +category: array-expression +--- + +# $reverseArray + +The `$reverseArray` operator is used to reverse the order of elements in an array. This operator can be useful when you need to process or display array elements in the opposite order. It's an array expression operator and can be used within aggregation pipelines. + +## Syntax + +```javascript +{ + $reverseArray: +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **``**| The array that you want to reverse.| + +## Examples + +Consider this sample document from the stores collection. + +```json +{ + "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5", + "name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile", + "location": { + "lat": 60.1441, + "lon": -141.5012 + }, + "staff": { + "totalStaff": { + "fullTime": 2, + "partTime": 0 + } + }, + "sales": { + "salesByCategory": [ + { + "categoryName": "DJ Headphones", + "totalSales": 35921 + } + ], + "fullSales": 3700 + }, + "promotionEvents": [ + { + "eventName": "Bargain Blitz Days", + "promotionalDates": { + "startDate": { + "Year": 2024, + "Month": 3, + "Day": 11 + }, + "endDate": { + "Year": 2024, + "Month": 2, + "Day": 18 + } + }, + "discounts": [ + { + "categoryName": "DJ Turntables", + "discountPercentage": 18 + }, + { + "categoryName": "DJ Mixers", + "discountPercentage": 15 + } + ] + }, + { + "eventName": "Discount Delight Days", + "promotionalDates": { + "startDate": { + "Year": 2024, + "Month": 5, + "Day": 11 + }, + "endDate": { + "Year": 2024, + "Month": 5, + "Day": 18 + } + } + } + ], + "tag": [ + "#ShopLocal", + "#FashionStore", + "#SeasonalSale", + "#FreeShipping", + "#MembershipDeals" + ] +} +``` + +### Example 1: Reversing the order of an array + +This query demonstrates the usage of operator for performing reversal on the order of the `promotionEvents` array. + +```javascript +db.stores.aggregate([ + //filtering to one document + { + $match: { + _id: "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5" + } + }, + { + $project: { + _id: 1, + name: 1, + promotionEventsReversed: { + $reverseArray: "$promotionEvents" + } + } + }, + // Include only _id, name, promotionalDates and eventName fields in the output + { + $project: { + _id: 1, + name: 1, + "promotionEventsReversed.promotionalDates": 1, + "promotionEventsReversed.eventName": 1 + } + } +]) +``` + +This query returns the following result. + +```json +[ + { + "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5", + "name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile", + "promotionEventsReversed": [ + { + "eventName": "Discount Delight Days", + "promotionalDates": { + "startDate": { + "Year": 2024, + "Month": 5, + "Day": 11 + }, + "endDate": { + "Year": 2024, + "Month": 5, + "Day": 18 + } + } + }, + { + "eventName": "Bargain Blitz Days", + "promotionalDates": { + "startDate": { + "Year": 2024, + "Month": 3, + "Day": 11 + }, + "endDate": { + "Year": 2024, + "Month": 2, + "Day": 18 + } + } + } + ] + } +] +``` diff --git a/reference/operators/array-expression/$slice.md b/reference/operators/array-expression/$slice.md new file mode 100644 index 0000000..b5bb901 --- /dev/null +++ b/reference/operators/array-expression/$slice.md @@ -0,0 +1,466 @@ +--- +title: $slice +description: The $slice operator returns a subset of an array from any element onwards in the array. +type: operators +category: array-expression +--- + +# $slice + +The `$slice` operator is used to return a subset of an array. It can be used to limit the number of elements in an array to a specified number or to return elements from a specified position in the array. The operator is useful when dealing with large arrays where only a portion of the data is needed for processing or display. + +## Syntax + +The syntax for the `$slice` operator is as following. + +- Returns elements from either the start or end of the array + +```javascript +{ + $slice: [ , ] +} +``` + +- Returns elements from the specified position in the array + +```javascript +{ + $slice: [ , , ] +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **`array`** | Any valid array expression. | +| **`position`** | Any valid integer expression. | +| **`n`** | Any valid integer expression. | + +## 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: Return the set of elements from the array + +This query retrieves the first three elements of the `sales.salesByCategory` array for `_id: 988d2dd1-2faa-4072-b420-b91b95cbfd60` within `stores` collection. + +```javascript +db.stores.aggregate([{ + $match: { + _id: "988d2dd1-2faa-4072-b420-b91b95cbfd60" + } +}, { + $project: { + salesByCategory: { + $slice: ["$sales.salesByCategory", 3] + } + } +}]) +``` + +This query returns the following result. + +```json +[ + { + "_id": "988d2dd1-2faa-4072-b420-b91b95cbfd60", + "salesByCategory": [ + { + "categoryName": "Towel Racks", + "totalSales": 13237 + }, + { + "categoryName": "Washcloths", + "totalSales": 44315 + }, + { + "categoryName": "Face Towels", + "totalSales": 42095 + } + ] + } +] +``` + +### Example 2: Slice with $push + +This query uses `$push` with `$each` to add new elements to the `promotionEvents` array and `$slice` to retain only the first N (positive slice) or last N (negative slice) elements. This ensures the array keeps the most recent entries after the update. + +```javascript +db.stores.updateOne({ + _id: "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5" +}, { + $push: { + promotionEvents: { + $each: [{ + eventName: "Black Friday Event", + promotionalDates: { + startDate: { + Year: 2024, + Month: 8, + Day: 1 + }, + endDate: { + Year: 2024, + Month: 8, + Day: 7 + } + }, + discounts: [{ + categoryName: 'DJ Speakers', + discountPercentage: 25 + }] + }, + { + eventName: "Mega Discount Days", + promotionalDates: { + startDate: { + Year: 2024, + Month: 5, + Day: 11 + }, + endDate: { + Year: 2024, + Month: 5, + Day: 18 + } + }, + discounts: [{ + categoryName: "DJ Lights", + discountPercentage: 20 + }] + } + ], + $slice: -3 + } + } +}) +``` + +The query returns the following result. + +```json +[ + { + "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5", + "name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile", + "location": { + "lat": 60.1441, + "lon": -141.5012 + }, + "staff": { + "totalStaff": { + "fullTime": 2, + "partTime": 0 + } + }, + "sales": { + "salesByCategory": [ + { + "categoryName": "DJ Headphones", + "totalSales": 35921 + }, + { + "categoryName": "DJ Cables", + "totalSales": 1000 + } + ], + "fullSales": 3700 + }, + "promotionEvents": [ + { + "eventName": "Cyber Monday Event", + "promotionalDates": { + "startDate": { + "Year": 2024, + "Month": 8, + "Day": 1 + }, + "endDate": { + "Year": 2024, + "Month": 8, + "Day": 7 + } + }, + "discounts": [ + { + "categoryName": "DJ Speakers", + "discountPercentage": 25 + } + ] + }, + { + "eventName": "Black Friday Event", + "promotionalDates": { + "startDate": { + "Year": 2024, + "Month": 8, + "Day": 1 + }, + "endDate": { + "Year": 2024, + "Month": 8, + "Day": 7 + } + }, + "discounts": [ + { + "categoryName": "DJ Speakers", + "discountPercentage": 25 + } + ] + }, + { + "eventName": "Mega Discount Days", + "promotionalDates": { + "startDate": { + "Year": 2024, + "Month": 5, + "Day": 11 + }, + "endDate": { + "Year": 2024, + "Month": 5, + "Day": 18 + } + }, + "discounts": [ + { + "categoryName": "DJ Lights", + "discountPercentage": 20 + } + ] + } + ], + "tag": [ + "#ShopLocal", + "#NewArrival", + "#FashionStore", + "#SeasonalSale", + "#FreeShipping", + "#MembershipDeals" + ] + } +] +``` + +### Example 3: Fetch the first matching element from an array + +This query retrieves the first document from "sales.salesByCategory" array. + +```javascript +db.stores.find({ + name: "Lakeshore Retail" + }, { + _id: 1, + name: 1, + "sales.salesByCategory": { + $slice: 1 + } + } // restricts the fields to be returned +) +``` + +This query returns the following result. + +```json +[ + { + "_id": "988d2dd1-2faa-4072-b420-b91b95cbfd60", + "name": "Lakeshore Retail", + "sales": { + "salesByCategory": [ + { + "categoryName": "Towel Racks", + "totalSales": 13237 + } + ] + } + } +] +``` + +### Example 4: Fetch the last element from an array + +This query retrieves the last document from "sales.salesByCategory" array. + +```javascript +db.stores.find({ + name: "Lakeshore Retail" +}, { + _id: 1, + name: 1, + "sales.salesByCategory": { + $slice: -1 + } +}) +``` + +This query returns the following result. + +```json +[ + { + "_id": "988d2dd1-2faa-4072-b420-b91b95cbfd60", + "name": "Lakeshore Retail", + "sales": { + "salesByCategory": [ + { + "categoryName": "Pillow Cases", + "totalSales": 38833 + } + ] + } + } +] +``` + +### Example 5: Retrieve a range of elements from an array + +This query retrieves a subset range from "sales.salesByCategory" array. + +```javascript +db.stores.find({ + name: "Lakeshore Retail" +}, { + _id: 1, + name: 1, + "sales.salesByCategory": { + $slice: [3, 2] + } +}) +``` + +This query returns the following result. + +```json +[ + { + "_id": "988d2dd1-2faa-4072-b420-b91b95cbfd60", + "name": "Lakeshore Retail", + "sales": { + "salesByCategory": [ + { + "categoryName": "Toothbrush Holders", + "totalSales": 47912 + }, + { + "categoryName": "Hybrid Mattresses", + "totalSales": 48660 + } + ] + } + } +] +``` diff --git a/reference/operators/array-expression/$sortarray.md b/reference/operators/array-expression/$sortarray.md new file mode 100644 index 0000000..15be7c3 --- /dev/null +++ b/reference/operators/array-expression/$sortarray.md @@ -0,0 +1,193 @@ +--- +title: $sortArray +description: The $sortArray operator helps in sorting the elements in an array. +type: operators +category: array-expression +--- + +# $sortArray + +The `$sortArray` operator is used to sort the elements of an array. The operator can be useful when you need to sort arrays within your documents based on specific fields or criteria. It can be applied to arrays of embedded documents or simple arrays of values. + +## Syntax + +```javascript +{ + $sortArray: { + input: , + sortBy: + } +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **`input`** | The array to be sorted. | +| **`sortBy`** | Specifies the sort order. It can be a single field or multiple fields with their corresponding sort order (1 for ascending, -1 for descending). | + +## 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: Sorting an Array of Embedded Documents + +This query sorts the `sales.salesByCategory` array within each document in descending order based on `totalSales`. + +```javascript +db.stores.aggregate([{ + $match: { + _id: "d3c9df51-41bd-4b4e-a26b-b038d9cf8b45" + } +}, { + $project: { + sortedSalesByCategory: { + $sortArray: { + input: "$sales.salesByCategory", + sortBy: { + totalSales: -1 + } + } + } + } +}]) +``` + +This query returns the following result. + +```json +[ + { + "_id": "d3c9df51-41bd-4b4e-a26b-b038d9cf8b45", + "sortedSalesByCategory": [ + { + "categoryName": "DJ Accessories", + "totalSales": 60000 + }, + { + "categoryName": "Music Accessories", + "totalSales": 40000 + }, + { + "categoryName": "DJ Speakers", + "totalSales": 36972 + }, + { + "categoryName": "DJ Headphones", + "totalSales": 12877 + } + ] + } +] +``` diff --git a/reference/operators/array-expression/$zip.md b/reference/operators/array-expression/$zip.md new file mode 100644 index 0000000..19673d2 --- /dev/null +++ b/reference/operators/array-expression/$zip.md @@ -0,0 +1,184 @@ +--- +title: $zip +description: The $zip operator allows merging two or more arrays element-wise into a single array or arrays. +type: operators +category: array-expression +--- + +# $zip + +The `$zip` operator is used to merge two or more arrays element-wise into a single array of arrays. It's useful when you want to combine related elements from multiple arrays into a single array structure. + +## Syntax + +```javascript +{ + $zip: { + inputs: [ , , ... ], + useLongestLength: , // Optional + defaults: // Optional + } +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **`inputs`** | An array of arrays to be merged element-wise. | +| **`useLongestLength`** | A boolean value that, if set to true, uses the longest length of the input arrays. If false or not specified, it uses the shortest length. | +| **`defaults`** | An array of default values to use if `useLongestLength` is true and any input array is shorter than the longest array. | + +## 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: Basic Usage + +Suppose you want to merge the `categoryName` and `totalSales` fields from the `salesByCategory` array. This query returns individual array of arrays under `categoryWithSales` field. `useLongestLength` set to `true` would return the following output, while a value of `false` removes the `Napkins` array from the output. + +```javascript +db.stores.aggregate([{ + $match: { + _id: "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6" + } + }, + { + $project: { + name: 1, + categoryNames: "$sales.salesByCategory.categoryName", + totalSales: "$sales.salesByCategory.totalSales", + categoryWithSales: { + $zip: { + inputs: ["$sales.salesByCategory.categoryName", "$sales.salesByCategory.totalSales"], + useLongestLength: false + } + } + } + } +]) +``` + +This query returns the following result. + +```json +[ + { + "_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6", + "name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort", + "categoryNames": ["Stockings"], + "totalSales": [25731], + "categoryWithSales": [["Stockings", 25731]] + } +] +```