diff --git a/reference/operators/logical-query/$and.md b/reference/operators/logical-query/$and.md new file mode 100644 index 0000000..cc9f590 --- /dev/null +++ b/reference/operators/logical-query/$and.md @@ -0,0 +1,246 @@ +--- +title: $and +description: The $and operator joins multiple query clauses and returns documents that match all specified conditions. +type: operators +category: logical-query +--- + +# $and + +The `$and` operator performs a logical AND operation on an array of expressions and retrieves documents that satisfy all the expressions. + +## Syntax + +```javascript +{ + $and: [{ + < expression1 > + }, { + < expression2 > + }, ..., { + < expressionN > + }] +} +``` + +## Parameters + +| Parameter | Description | +|-----------|-------------| +| `expression` | An array of expressions that must all be true for a document to be included in the results | + +## 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 AND operator as logical-query + +This query filters for stores where the number of full-time employees is greater than 10 and part-time employees is less than 15 using the `$and` operator. It projects only the `name` and `staff` fields and limits the result to three records. + +```javascript +db.stores.find({ + $and: [{ + "staff.employeeCount.fullTime": { + $gt: 10 + } + }, { + "staff.employeeCount.partTime": { + $lt: 15 + } + }] +}, { + "name": 1, + "staff": 1 +}).limit(3) +``` + +The first three results returned by this query are: + +```json +[ + { + "_id": "e60c807b-d31c-4903-befb-5d608f260ba3", + "name": "Wide World Importers | Appliance Emporium - Craigfort", + "staff": { + "totalStaff": { + "fullTime": 11, + "partTime": 8 + } + } + }, + { + "_id": "70032165-fded-47b4-84a3-8d9c18a4d1e7", + "name": "Northwind Traders | Picture Frame Bazaar - Lake Joesph", + "staff": { + "totalStaff": { + "fullTime": 14, + "partTime": 0 + } + } + }, + { + "_id": "dda2a7d2-6984-40cc-bbea-4cbfbc06d8a3", + "name": "Contoso, Ltd. | Home Improvement Closet - Jaskolskiview", + "staff": { + "totalStaff": { + "fullTime": 16, + "partTime": 8 + } + } + } +] +``` + +### Example 2: Use AND operator as boolean-expression to find stores with high sales and sufficient staff + +This query finds stores that have both total sales greater than 100,000 and more than 30 total staff members. + +```javascript +db.stores.aggregate([ + { + $project: { + name: 1, + totalSales: "$sales.totalSales", + totalStaff: { + $add: ["$staff.employeeCount.fullTime", "$staff.employeeCount.partTime"] + }, + meetsHighPerformanceCriteria: { + $and: [ + { $gt: ["$sales.totalSales", 100000] }, + { $gt: [{ $add: ["$staff.employeeCount.fullTime", "$staff.employeeCount.partTime"] }, 30] } + ] + } + } + }, + { $limit: 2 } +]) +``` + +The first two results returned by this query are: + +```json +[ + { + "_id": "905d1939-e03a-413e-a9c4-221f74055aac", + "name": "Trey Research | Home Office Depot - Lake Freeda", + "totalStaff": 31, + "meetsHighPerformanceCriteria": false + }, + { + "_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6", + "name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort", + "totalStaff": 27, + "meetsHighPerformanceCriteria": false + } +] +``` diff --git a/reference/operators/logical-query/$nor.md b/reference/operators/logical-query/$nor.md new file mode 100644 index 0000000..ed99b22 --- /dev/null +++ b/reference/operators/logical-query/$nor.md @@ -0,0 +1,266 @@ +--- +title: $nor +description: The $nor operator performs a logical NOR on an array of expressions and retrieves documents that fail all the conditions. +type: operators +category: logical-query +--- + +# $nor + +The `$nor` operator performs a logical NOR operation on an array of expressions and selects documents that fail all the specified expressions. + +## Syntax + +```javascript +{ + $nor: [{ + < expression1 > + }, { + < expression2 > + }, ..., { + < expressionN > + }] +} +``` + +## Parameters + +| Parameter | Description | +|-----------|-------------| +| `expression` | An array of expressions, all of which must be false for a document to be included | + +## 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 NOR operation + +To find stores that neither have more than 15 full-time staff nor more than 20 part-time staff, run a query with the $nor operator on both the conditions. Then, project only the name and staff fields from the stores in the result set. + +```javascript +db.stores.find({ + $nor: [{ + "staff.totalStaff.fullTime": { + $gt: 15 + } + }, { + "staff.totalStaff.partTime": { + $gt: 20 + } + }] +}, { + "name": 1, + "staff": 1 +}) +``` + +The first two results returned by this query are: + +```json +[ + { + "_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6", + "name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort", + "staff": { + "totalStaff": { + "fullTime": 9, + "partTime": 18 + } + } + }, + { + "_id": "923d2228-6a28-4856-ac9d-77c39eaf1800", + "name": "Lakeshore Retail | Home Decor Hub - Franciscoton", + "staff": { + "totalStaff": { + "fullTime": 7, + "partTime": 6 + } + } + } +] +``` + +### Example 2: Complex NOR operation + +To find stores without sales over $100,000, without sales of "Digital Watches", or without promotions in September 2024, run a query using the $nor operator on all three conditions. Lastly, project only the name, sales and promotion events from the stores in the result set. + +```javascript +db.stores.find({ + $nor: [{ + "sales.totalSales": { + $gt: 100000 + } + }, { + "sales.salesByCategory.categoryName": "Digital Watches" + }, { + "promotionEvents": { + $elemMatch: { + "promotionalDates.startDate.Month": 9, + "promotionalDates.startDate.Year": 2024 + } + } + }] +}, { + "name": 1, + "sales": 1, + "promotionEvents": 1 +}) +``` + +One of the results returned by this query is: + +```json +[ + { + "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5", + "name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile", + "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 + } + ] + } + ] + } +] +``` diff --git a/reference/operators/logical-query/$not.md b/reference/operators/logical-query/$not.md new file mode 100644 index 0000000..861c56e --- /dev/null +++ b/reference/operators/logical-query/$not.md @@ -0,0 +1,233 @@ +--- +title: $not +description: The $not operator performs a logical NOT operation on a specified expression, selecting documents that don't match the expression. +type: operators +category: logical-query +--- + +# $not + +The `$not` operator performs a logical NOT operation on a specified expression and selects documents that don't match the expression. + +## Syntax + +```javascript +{ + field: { + $not: { + < operator - expression > + } + } +} +``` + +## Parameters + +| Parameter | Description | +|-----------|-------------| +| `operator-expression` | The expression to negate | + +## 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 NOT operation as logical-query operator + +This query retrieves stores where the number of full-time staff isn't equal to 5 using the `$not` operator with $eq. It returns only the `name` and `staff` fields for up to two such matching documents. + +```javascript + db.stores.find({ + "staff.totalStaff.fullTime": { + $not: { + $eq: 5 + } + } + }, { + "name": 1, + "staff": 1 + }).limit(2) +``` + +The first two results returned by this query are: + +```json +[ + { + "_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6", + "name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort", + "staff": { + "totalStaff": { + "fullTime": 9, + "partTime": 18 + } + } + }, + { + "_id": "923d2228-6a28-4856-ac9d-77c39eaf1800", + "name": "Lakeshore Retail | Home Decor Hub - Franciscoton", + "staff": { + "totalStaff": { + "fullTime": 7, + "partTime": 6 + } + } + } +] +``` + +### Example 2: Use NOT operator as boolean-expression to identify stores that aren't high-volume + +This query retrieves stores that don't have high sales volume (not greater than 50,000). + +```javascript +db.stores.aggregate([ + { + $project: { + name: 1, + totalSales: "$sales.salesByCategory.totalSales", + isNotHighVolume: { + $not: { $gt: ["$sales.salesByCategory.totalSales", 50000] } + }, + storeCategory: { + $cond: [ + { $not: { $gt: ["$sales.salesByCategory.totalSales", 50000] } }, + "High Volume Store", + "Small/Medium Store" + ] + } + } + }, + { $limit: 2 } +]) +``` + +The first two results returned by this query are: + +```json +[ + { + "_id": "905d1939-e03a-413e-a9c4-221f74055aac", + "name": "Trey Research | Home Office Depot - Lake Freeda", + "totalSales": [ 37978 ], + "isNotHighVolume": false, + "storeCategory": "Small/Medium Store" + }, + { + "_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6", + "name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort", + "totalSales": [ 25731 ], + "isNotHighVolume": false, + "storeCategory": "Small/Medium Store" + } +] +``` diff --git a/reference/operators/logical-query/$or.md b/reference/operators/logical-query/$or.md new file mode 100644 index 0000000..182567e --- /dev/null +++ b/reference/operators/logical-query/$or.md @@ -0,0 +1,255 @@ +--- +title: $or +description: The $or operator joins query clauses with a logical OR and returns documents that match at least one of the specified conditions. +type: operators +category: logical-query +--- + +# $or + +The `$or` operator performs a logical OR operation on an array of expressions and retrieves documents that satisfy at least one of the specified conditions. + +## Syntax + +```javascript +{ + $or: [{ + < expression1 > + }, { + < expression2 > + }, ..., { + < expressionN > + }] +} +``` + +## Parameters + +| Parameter | Description | +|-----------|-------------| +| `expression` | An array of expressions, where at least one must be true for a document to be included | + +## 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 OR operation as logical-query + +This query retrieves stores with more than 15 full-time staff or more than 20 part-time staff, run a query using the $or operator on both the conditions. Then, project only the name and staff fields from the stores in the result set. + +```javascript +db.stores.find( + { + $or: [ + { "staff.employeeCount.fullTime": { $gt: 15 } }, + { "staff.employeeCount.partTime": { $gt: 20 } } + ] + }, + { + "name": 1, + "staff": 1 + } +).limit(2) +``` + +The first two results returned by this query are: + +```json +[ + { + "_id": "dda2a7d2-6984-40cc-bbea-4cbfbc06d8a3", + "name": "Contoso, Ltd. | Home Improvement Closet - Jaskolskiview", + "staff": { + "employeeCount": { + "fullTime": 16, + "partTime": 8 + } + } + }, + { + "_id": "44fdb9b9-df83-4492-8f71-b6ef648aa312", + "name": "Fourth Coffee | Storage Solution Gallery - Port Camilla", + "staff": { + "employeeCount": { + "fullTime": 17, + "partTime": 15 + } + } + } +] +``` + +### Example 2: Use OR operator as boolean-expression to identify stores with either high sales or large staff + +This query retrieves stores that have either total sales greater than 50,000 or more than 25 total staff members. + +```javascript +db.stores.aggregate([ + { + $project: { + name: 1, + totalSales: "$sales.totalSales", + totalStaff: { + $add: ["$staff.employeeCount.fullTime", "$staff.employeeCount.partTime"] + }, + qualifiesForProgram: { + $or: [ + { $gt: ["$sales.totalSales", 50000] }, + { $gt: [{ $add: ["$staff.employeeCount.fullTime", "$staff.employeeCount.partTime"] }, 25] } + ] + } + } + }, + { $limit: 4 } +]) +``` + +The first four results returned by this query are: + +```json +[ + { + "_id": "905d1939-e03a-413e-a9c4-221f74055aac", + "name": "Trey Research | Home Office Depot - Lake Freeda", + "totalStaff": 31, + "qualifiesForProgram": true + }, + { + "_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6", + "name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort", + "totalStaff": 27, + "qualifiesForProgram": true + }, + { + "_id": "923d2228-6a28-4856-ac9d-77c39eaf1800", + "name": "Lakeshore Retail | Home Decor Hub - Franciscoton", + "totalStaff": 13, + "qualifiesForProgram": false + }, + { + "_id": "7e53ca0f-6e24-4177-966c-fe62a11e9af5", + "name": "Contoso, Ltd. | Office Supply Deals - South Shana", + "totalStaff": 2, + "qualifiesForProgram": false + } +] +``` + +## Performance Considerations + +- Review the suggestions for finding better performance. + - Each condition in the `$or` array is evaluated independently + - Use indexes when possible for better performance + - Consider the order of conditions for optimal execution + - Use `$in` instead of `$or` for multiple equality checks on the same field + - Keep the number of `$or` conditions reasonable