From c2926bf8d635932b8b6f974e0343b1e91224e94b Mon Sep 17 00:00:00 2001 From: seesharprun Date: Tue, 9 Dec 2025 11:32:51 -0500 Subject: [PATCH] Add reference files --- reference/operators/comparison-query/$cmp.md | 195 ++++++++++++++ reference/operators/comparison-query/$eq.md | 263 +++++++++++++++++++ reference/operators/comparison-query/$gt.md | 199 ++++++++++++++ reference/operators/comparison-query/$gte.md | 235 +++++++++++++++++ reference/operators/comparison-query/$in.md | 252 ++++++++++++++++++ reference/operators/comparison-query/$lt.md | 170 ++++++++++++ reference/operators/comparison-query/$lte.md | 196 ++++++++++++++ reference/operators/comparison-query/$ne.md | 202 ++++++++++++++ reference/operators/comparison-query/$nin.md | 229 ++++++++++++++++ 9 files changed, 1941 insertions(+) create mode 100644 reference/operators/comparison-query/$cmp.md create mode 100644 reference/operators/comparison-query/$eq.md create mode 100644 reference/operators/comparison-query/$gt.md create mode 100644 reference/operators/comparison-query/$gte.md create mode 100644 reference/operators/comparison-query/$in.md create mode 100644 reference/operators/comparison-query/$lt.md create mode 100644 reference/operators/comparison-query/$lte.md create mode 100644 reference/operators/comparison-query/$ne.md create mode 100644 reference/operators/comparison-query/$nin.md diff --git a/reference/operators/comparison-query/$cmp.md b/reference/operators/comparison-query/$cmp.md new file mode 100644 index 0000000..fac650d --- /dev/null +++ b/reference/operators/comparison-query/$cmp.md @@ -0,0 +1,195 @@ +--- +title: $cmp +description: The $cmp operator compares two values +type: operators +category: comparison-query +--- + +# $cmp + +The `$cmp` operator compares two specified values. The $cmp operator returns -1 if the first value is less than the second, 0 if the two values are equal and 1 if the first value is greater than the second. + +## Syntax + +```javascript +{ + $cmp: [, ] +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **``** | The first value which is compared to the second by the $cmp operator| +| **``** | The second value being compared to by the $cmp operator| + +## 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 - Compare the total sales of stores to $25,000 + +To compare the total sales of stores within the Boulder Innovations Company to $25,000, first run a query to filter on the company name of stores. Then, use $cmp to compare the sales.totalSales field to 25000. Lastly, project only the name and total sales for the resulting stores. + +```javascript +db.stores.aggregate([{ + $match: { + company: { + $in: ["Boulder Innovations"] + } + } +}, { + $project: { + name: 1, + "sales.salesByCategory.totalSales": 1, + greaterThan25000: { + $cmp: ["$sales.revenue", 25000] + } + } +}]) +``` + +The first two results returned by this query are: + +```json +[ + { + "_id": "a5040801-d127-4950-a320-e55f6aed4b36", + "name": "Boulder Innovations | DJ Equipment Pantry - West Christopher", + "sales": { + "salesByCategory": [ + { + "totalSales": 21522 + } + ] + }, + "greaterThan25000": -1 + }, + { + "_id": "bb6e097a-e204-4b64-9f13-5ae8426fcc76", + "name": "Boulder Innovations | Kitchen Appliance Outlet - Lake Chazville", + "sales": { + "salesByCategory": [ + { + "totalSales": 24062 + }, + { + "totalSales": 24815 + } + ] + }, + "greaterThan25000": 1 + } +] +``` diff --git a/reference/operators/comparison-query/$eq.md b/reference/operators/comparison-query/$eq.md new file mode 100644 index 0000000..c4ae35c --- /dev/null +++ b/reference/operators/comparison-query/$eq.md @@ -0,0 +1,263 @@ +--- +title: $eq +description: The $eq query operator compares the value of a field to a specified value +type: operators +category: comparison-query +--- + +# $eq + +The `$eq` operator is used to match documents where the value of a field is equal to a specified value. The $eq operator filters documents based on exact matches on query predicates to retrieve documents with specific values, objects and arrays. + +## Syntax + +```javascript +{ + field: { + $eq: + } +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **`field`** | The field to be compared| +| **`value`** | The value to compare against| + +## 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 $eq filter on a root level field + +To find a store with the name "Boulder Innovations | Home Security Place - Ankundingburgh", run a query with the $eq predicate to match on the name field and project only the ID and name fields in the result. + +```javascript +db.stores.find({ + name: { + $eq: "Boulder Innovations | Home Security Place - Ankundingburgh" + } +}, { + name: 1 +}) +``` + +This query returns the following result: + +```json +[ + { + "_id": "bda56164-954d-4f47-a230-ecf64b317b43", + "name": "Boulder Innovations | Home Security Place - Ankundingburgh" + } +] +``` + +### Example 2: Use $eq filter on a nested field + +To find a store with a total sales of exactly $37,015, run a query using the $eq operator using the dot notation on the nested field sales.totalSales field. + +```javascript +db.stores.find({ + "sales.totalSales": { + $eq: 37015 + } +}, { + name: 1, + "sales.totalSales": 1 +}) +``` + +This returns the following result: + +```json +[ + { + "_id": "bda56164-954d-4f47-a230-ecf64b317b43", + "name": "Boulder Innovations | Home Security Place - Ankundingburgh", + "sales": { "totalSales": 37015 } + } +] +``` + +### Example 3: Use $eq for individual items in an array + +The following query retrieves documents using equality predicates on individual items within the nested promotionEvents.discounts array. + +This query searches for an equality match on any one of the objects within the nested discounts array + +```javascript +db.stores.find({ + "promotionEvents.discounts": { + $eq: { + categoryName: "Alarm Systems", + discountPercentage: 5 + } + } +}, { + name: 1 +}, { + limit: 2 +}) +``` + +This query returns the following results: + +```json +[ + { + "_id": "ece5bf6c-3255-477e-bf2c-d577c82d6995", + "name": "Proseware, Inc. | Home Security Boutique - Schambergertown" + }, + { + "_id": "7baa8fd8-113a-4b10-a7b9-2c116e976491", + "name": "Tailwind Traders | Home Security Pantry - Port Casper" + } +] +``` + +### Example 4: Use $eq to match the entire array + +This query searches for documents based on exact match on ALL the values within the promotionEvents.discounts array. + +```javascript +db.stores.find({ + "promotionEvents.discounts": { + $eq: [{ + categoryName: "Alarm Systems", + discountPercentage: 5 + }, { + categoryName: "Door Locks", + discountPercentage: 12 + }] + } +}, { + name: 1 +}) +``` + +This query returns the following result: + +```json +[ + { + "_id": "aa9ad64c-29da-42f8-a1f0-30e03bf04a2d", + "name": "Boulder Innovations | Home Security Market - East Sheridanborough" + } +] +``` + +> [!NOTE] +> For an equality match on an entire array, the order of the specified values in the equality predicates must also be an exact match. diff --git a/reference/operators/comparison-query/$gt.md b/reference/operators/comparison-query/$gt.md new file mode 100644 index 0000000..60d6d82 --- /dev/null +++ b/reference/operators/comparison-query/$gt.md @@ -0,0 +1,199 @@ +--- +title: $gt +description: The $gt query operator retrieves documents where the value of a field is greater than a specified value +type: operators +category: comparison-query +--- + +# $gt + +The `$gt` operator retrieves documents where the value of a field is greater than a specified value. The `$gt` operator queries numerical and date values to filter records that exceed a specified threshold. + +## Syntax + +```javascript +{ + field: { + $gt: value + } +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **`field`** | The field in the document you want to compare| +| **`value`** | The value that the field should be greater than| + +## 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: Retrieve stores with sales exceeding $35,000 + +To retrieve a store with over $35,000 in sales, first run a query with $gt operator on the sales.totalSales field. Then limit the query results to one store. + +```javascript +db.stores.find({ + "sales.totalSales": { + $gt: 35000 + } +}, { + name: 1, + "sales.totalSales": 1 +}, { + limit: 1 +}) +``` + +The first result returned by this query is: + +```json +[ + { + "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f", + "name": "First Up Consultants | Bed and Bath Center - South Amir", + "sales": { "totalSales": 37701 } + } +] +``` + +### Example 2: Find a store with more than 12 full-time staff + +To find a store with more than 12 full time staff, first run a query with the $gt operator on the staff.totalStaff.fullTime field. Then project just the name and totalStaff fields and limit the result set to a single store from the list of matching results. + +```javascript +db.stores.find({ + "staff.totalStaff.fullTime": { + $gt: 12 + } +}, { + name: 1, + "staff.totalStaff": 1 +}, { + limit: 1 +}) +``` + +The first result returned by this query is: + +```json +[ + { + "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f", + "name": "First Up Consultants | Bed and Bath Center - South Amir", + "staff": { "totalStaff": { "fullTime": 18, "partTime": 17 } } + } +] +``` diff --git a/reference/operators/comparison-query/$gte.md b/reference/operators/comparison-query/$gte.md new file mode 100644 index 0000000..9bfa22e --- /dev/null +++ b/reference/operators/comparison-query/$gte.md @@ -0,0 +1,235 @@ +--- +title: $gte +description: The $gte operator retrieves documents where the value of a field is greater than or equal to a specified value +type: operators +category: comparison-query +--- + +# $gte + +The `$gte` operator retrieves documents where the value of a field is greater than or equal to a specified value. The `$gte` operator retrieves documents that meet a minimum threshold for the value of a field. + +## Syntax + +```javascript +{ + field: { + $gte: + } +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **`field`** | The field in the document you want to compare| +| **`value`** | The value that the field should be greater than| + +## 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: Find a store with sales >= $35,000 + +To retrieve a store with at least $35,000 in sales, first run a query using the $gte operator on the sales.totalSales field. Then project only the name of the store and its total sales and limit the result set to one document. + +```javascript +db.stores.find({ + "sales.totalSales": { + $gte: 35000 + } +}, { + name: 1, + "sales.totalSales": 1 +}, { + limit: 1 +}) +``` + +The first result returned by this query is: + +```json +[ + { + "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f", + "name": "First Up Consultants | Bed and Bath Center - South Amir", + "sales": { "totalSales": 37701 } + } +] +``` + +### Example 2: Find a store with 12 or more full-time staff + +To retrieve a store with at least 12 full time staff, first run a query with the $gte operator on the staff.totalStaff.fullTime field. Then, project only the name and full time staff count and limit the results to a single document from the result set. + +```javascript +db.stores.find({ + "staff.totalStaff.fullTime": { + $gte: 12 + } +}, { + name: 1, + "staff.totalStaff.fullTime": 1 +}, { + limit: 1 +}) +``` + +The first result returned by this query is: + +```json +[ + { + "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f", + "name": "First Up Consultants | Bed and Bath Center - South Amir", + "staff": { "totalStaff": { "fullTime": 18 } } + } +] +``` + +### Example 3: Find promotion events with a discount percentage greater than or equal to 15% for Laptops + +To find two stores with promotions with a discount of at least 15% for laptops, first run a query to filter stores with laptop promotions. Then use the $gte operator on the discountPercentage field. Lastly, project only the name of the store and limit the results to two documents from the result set. + +```javascript +db.stores.find({ + "promotionEvents.discounts": { + $elemMatch: { + categoryName: "Laptops", + discountPercentage: { + $gte: 15 + } + } + } +}, { + name: 1 +}, { + limit: 2 +}) +``` + +The first two results returned by this query are: + +```json +[ + { + "_id": "60e43617-8d99-4817-b1d6-614b4a55c71e", + "name": "Wide World Importers | Electronics Emporium - North Ayanashire" + }, + { + "_id": "3c441d5a-c9ad-47f4-9abc-ac269ded44ff", + "name": "Contoso, Ltd. | Electronics Corner - New Kiera" + } +] +``` diff --git a/reference/operators/comparison-query/$in.md b/reference/operators/comparison-query/$in.md new file mode 100644 index 0000000..06fbf5b --- /dev/null +++ b/reference/operators/comparison-query/$in.md @@ -0,0 +1,252 @@ +--- +title: $in +description: The $in operator matches value of a field against an array of specified values +type: operators +category: comparison-query +--- + +# $in + +The `$in` operator matches values of a field against an array of possible values. The `$in` operator filters documents where the value of a field equals any of the specified values. + +## Syntax + +```javascript +{ + field: { + $in: [ listOfValues ] + } +} +``` + +## 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 $in operator as comparison-query to find a store with specific categories of promotions + +This query finds stores that offer discounts in either "Smoked Salmon" or "Anklets" categories via promotion events. + +```javascript +db.stores.find({ + "promotionEvents.discounts.categoryName": { + $in: ["Smoked Salmon", "Anklets"] + } +}, { + name: 1, + "promotionEvents.discounts.categoryName": 1 +}).limit(1) +``` + +The first result returned by this query is: + +```json +[ + { + "_id": "48fcdab8-b961-480e-87a9-19ad880e9a0a", + "name": "Lakeshore Retail | Jewelry Collection - South Nicholas", + "promotionEvents": [ + { + "discounts": [ + {"categoryName": "Anklets"}, + {"categoryName": "Cufflinks"} + ] + }, + { + "discounts": [ + {"categoryName": "Anklets"}, + {"categoryName": "Brooches"} + ] + }, + { + "discounts": [ + {"categoryName": "Rings"}, + {"categoryName": "Bracelets"} + ] + }, + { + "discounts": [ + {"categoryName": "Charms"}, + {"categoryName": "Bracelets"} + ] + }, + { + "discounts": [ + {"categoryName": "Watches"}, + {"categoryName": "Pendants"} + ] + } + ] + } +] +``` + +### Example 2 - Use $in operator as array-expression in an array for a specified value or set of values + +This query searches for the specified store and filters documents where at least one `discountPercentage` within any `promotionEvents.discounts` is either 15 or 20. It uses a dot notation path and the $in operator to match nested discount values across the array hierarchy. + +```javascript +db.stores.find({ + _id: "48fcdab8-b961-480e-87a9-19ad880e9a0a", + "promotionEvents.discounts.discountPercentage": { + $in: [15, 20] + } +}, { + _id: 1, + name: 1, + "promotionEvents.discounts": 1 +}) +``` + +This query returns the following result: + +```json +[ + { + "_id": "48fcdab8-b961-480e-87a9-19ad880e9a0a", + "name": "Lakeshore Retail | Jewelry Collection - South Nicholas", + "promotionEvents": [ + { + "discounts": [ + { "categoryName": "Anklets", "discountPercentage": 12 }, + { "categoryName": "Cufflinks", "discountPercentage": 9 } + ] + }, + { + "discounts": [ + { "categoryName": "Anklets", "discountPercentage": 23 }, + { "categoryName": "Brooches", "discountPercentage": 12 } + ] + }, + { + "discounts": [ + { "categoryName": "Rings", "discountPercentage": 10 }, + { "categoryName": "Bracelets", "discountPercentage": 21 } + ] + }, + { + "discounts": [ + { "categoryName": "Charms", "discountPercentage": 9 }, + { "categoryName": "Bracelets", "discountPercentage": 13 } + ] + }, + { + "discounts": [ + { "categoryName": "Watches", "discountPercentage": 20 }, + { "categoryName": "Pendants", "discountPercentage": 7 } + ] + } + ] + } +] +``` diff --git a/reference/operators/comparison-query/$lt.md b/reference/operators/comparison-query/$lt.md new file mode 100644 index 0000000..514fa4f --- /dev/null +++ b/reference/operators/comparison-query/$lt.md @@ -0,0 +1,170 @@ +--- +title: $lt +description: The $lt operator retrieves documents where the value of field is less than a specified value +type: operators +category: comparison-query +--- + +# $lt + +The `$lt` operator retrieves documents where the value of a field is strictly less than a specified value. The `$lt` operator filters documents based on numeric, date, or string values. + +## Syntax + +```javascript +{ + field: { + $lt: value + } +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **`field`** | The field in the document you want to evaluate| +| **`value`** | The value to compare against the field's value. The operator will match documents where the field's value is less than this specified value| + +## 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: Find a store with sales below $36,000 + +To find a store with less than $36,000 in sales, first run a query using $lt on the sales.totalSales field. Then project only the name and total sales of the resulting stores and limit the number of results to a single document. + +```javascript +db.stores.find({ + "sales.totalSales": { + $lt: 36000 + } +}, { + name: 1, + "sales.totalSales": 1 +}, { + limit: 1 +}) +``` + +The first result returned by this query is: + +```json +[ + { + "_id": "e6895a31-a5cd-4103-8889-3b95a864e5a6", + "name": "VanArsdel, Ltd. | Picture Frame Store - Port Clevelandton", + "sales": { "totalSales": 17676 } + } +] +``` diff --git a/reference/operators/comparison-query/$lte.md b/reference/operators/comparison-query/$lte.md new file mode 100644 index 0000000..c10d03a --- /dev/null +++ b/reference/operators/comparison-query/$lte.md @@ -0,0 +1,196 @@ +--- +title: $lte +description: The $lte operator retrieves documents where the value of a field is less than or equal to a specified value +type: operators +category: comparison-query +--- + +# $lte + +The `$lte` operator retrieves documents where the value of a field is less than or equal to a specified value. The `$lte` operator filters documents based on numerical, date, or other comparable fields. + +## Syntax + +```javascript +{ + field: { + $lte: + } +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **`field`** | The field to be compared| +| **`value`** | The value to compare against| + +## 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: Find a store with sales <= $35,000 + +To find a store with sales <= $35,000, run a query using $lte on the sales.totalSales field and limit the resulting documents to a single store. + +```javascript +db.stores.find({ + "sales.totalSales": { + $lte: 35000 + } +}, { + _id: 1 +}, { + limit: 1 +}) +``` + +The first result returned by this query is: + +```json +[ + { + "_id": "e6895a31-a5cd-4103-8889-3b95a864e5a6" + } +] +``` + +### Example 2: Find a store with 12 or fewer full-time staff + +To find a store with <= 12 full-time staff, run a query using $lte on the nested fullTime field. Then project only the name and full time staff count and limit the results to one store from the result set. + +```javascript +db.stores.find({ + "staff.totalStaff.fullTime": { + $lte: 12 + } +}, { + name: 1, + "staff.totalStaff.fullTime": 1 +}, { + limit: 1 +}) +``` + +The first result returned by this query is: + +```json +[ + { + "_id": "e6895a31-a5cd-4103-8889-3b95a864e5a6", + "name": "VanArsdel, Ltd. | Picture Frame Store - Port Clevelandton", + "staff": { "totalStaff": { "fullTime": 6 } } + } +] +``` diff --git a/reference/operators/comparison-query/$ne.md b/reference/operators/comparison-query/$ne.md new file mode 100644 index 0000000..608e417 --- /dev/null +++ b/reference/operators/comparison-query/$ne.md @@ -0,0 +1,202 @@ +--- +title: $ne +description: The $ne operator retrieves documents where the value of a field doesn't equal a specified value +type: operators +category: comparison-query +--- + +# $ne + +The `$ne` operator retrieves documents where the value of a field doesn't equal a specified value. + +## Syntax + +```javascript +{ + field: { + $ne: value + } +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **`field`** | The field to be compared| +| **`value`** | The value that the field shouldn't be equal 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 - Find a store whose name isn't "Fourth Coffee" + +To find a store with a name that isn't "Fourth Coffee", first run a query using $ne on the name field. Then project only the name of the resulting documents and limit the results to one store from the result set. + +```javascript +db.stores.find({ + name: { + $ne: "Fourth Coffee" + } +}, { + _id: 1, + name: 1 +}, { + limit: 1 +}) +``` + +The first result returned by this query is: + +```json +[ + { + "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f", + "name": "First Up Consultants | Bed and Bath Center - South Amir" + } +] +``` + +### Example 2 - Find a store with promotion events that aren't in 2024 + +To find a store with promotions events that don't start in 2024, first run a query using $ne on the nested startDate field. Then project the name and promotions offered by the stores and limit the results to one document from the result set. + +```javascript +db.stores.find({ + "promotionEvents.promotionalDates.startDate": { + $ne: "2024" + } +}, { + name: 1, + "promotionEvents.promotionalDates.startDate": 1 +}, { + limit: 1 +}) +``` + +The first result returned by this query is: + +```json +[ + { + "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f", + "name": "First Up Consultants | Bed and Bath Center - South Amir", + "promotionEvents": [ + { + "promotionalDates": { "startDate": { "Year": 2024, "Month": 9, "Day": 21 } } + } + ] + } +] +``` diff --git a/reference/operators/comparison-query/$nin.md b/reference/operators/comparison-query/$nin.md new file mode 100644 index 0000000..005735c --- /dev/null +++ b/reference/operators/comparison-query/$nin.md @@ -0,0 +1,229 @@ +--- +title: $nin +description: The $nin operator retrieves documents where the value of a field doesn't match a list of values +type: operators +category: comparison-query +--- + +# $nin + +The `$nin` operator retrieves documents where the value of a specified field doesn't match a list of values. + +## Syntax + +```javascript +{ + field: { + $nin: [ < listOfValues > ] + } +} +``` + +## Parameters + +| Parameter | Description | +| --- | --- | +| **`field`** | The field to compare| +| **`[]`** | An array of values that shouldn't match the value of the field being compared| + +## 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 - Find a store with a discount that isn't 10%, 15%, or 20% + +To find a store with promotions offering discounts that are not 10%, 15%, or 20%, first run a query using $nin on the nested discountPercentage field. Then project only the name and discount offered by the result store and limit the result to a single document from the result set. + +```javascript +db.stores.find({ + "promotionEvents.discounts.discountPercentage": { + $nin: [10, 15, 20] + } +}, { + name: 1, + "promotionEvents.discounts.discountPercentage": 1 +}, { + limit: 1 +}) +``` + +The first result returned by this query is: + +```json +[ + { + "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f", + "name": "First Up Consultants | Bed and Bath Center - South Amir", + "promotionEvents": [ + { + "discounts": [ + { "discountPercentage": 18 }, + { "discountPercentage": 17 }, + { "discountPercentage": 9 }, + { "discountPercentage": 5 }, + { "discountPercentage": 5 }, + { "discountPercentage": 6 }, + { "discountPercentage": 9 }, + { "discountPercentage": 5 }, + { "discountPercentage": 19 }, + { "discountPercentage": 21 } + ] + } + ] + } +] +``` + +### Example 2 - Find a store with no discounts on specific categories of promotions + +To find a store without promotions on Smoked Salmon and Anklets, first run a query using $nin on the nested categoryName field. Then project the name and promotions offered by the store and limit the results to one document from the result set. + +```javascript +db.stores.find({ + "promotionEvents.discounts.categoryName": { + $nin: ["Smoked Salmon", "Anklets"] + } +}, { + name: 1, + "promotionEvents.discounts.categoryName": 1 +}, { + limit: 1 +}) +``` + +The first result returned by this query is: + +```json +[ + { + "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f", + "name": "First Up Consultants | Bed and Bath Center - South Amir", + "promotionEvents": [ + { + "discounts": [ + { "categoryName": "Bath Accessories" }, + { "categoryName": "Pillow Top Mattresses" }, + { "categoryName": "Bathroom Scales" }, + { "categoryName": "Towels" }, + { "categoryName": "Bathrobes" }, + { "categoryName": "Mattress Toppers" }, + { "categoryName": "Hand Towels" }, + { "categoryName": "Shower Heads" }, + { "categoryName": "Bedspreads" }, + { "categoryName": "Bath Mats" } + ] + } + ] + } +] +```