Skip to content

Commit a059c80

Browse files
committed
Add reference files
1 parent 3a2123c commit a059c80

File tree

2 files changed

+273
-0
lines changed

2 files changed

+273
-0
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
---
2+
title: Aggregate
3+
description: The aggregate command is used to process data records and return computed results.
4+
type: commands
5+
category: aggregation
6+
---
7+
8+
# aggregate
9+
10+
The `aggregate` command is used to process data records and return computed results. It performs operations on the data, such as filtering, grouping, and sorting, and can transform the data in various ways. The `aggregate` command is highly versatile and is commonly used for data analysis and reporting.
11+
12+
## Syntax
13+
14+
```console
15+
db.collection.aggregate(pipeline, options)
16+
```
17+
18+
- **pipeline**: An array of aggregation stages that process and transform the data.
19+
- **options**: Optional. Specifies more options for the aggregation, such as `explain`, `allowDiskUse`, and `cursor`.
20+
21+
## Examples
22+
23+
### Example 1: Calculate total sales by category
24+
25+
This example demonstrates how to calculate the total sales for each category in the `stores` collection.
26+
27+
```javascript
28+
db.stores.aggregate([
29+
{
30+
$unwind: "$sales.salesByCategory"
31+
},
32+
{
33+
$group: {
34+
_id: "$sales.salesByCategory.categoryName",
35+
totalSales: { $sum: "$sales.salesByCategory.totalSales" }
36+
}
37+
}
38+
])
39+
```
40+
41+
#### Sample output
42+
43+
```javascript
44+
[mongos] StoreData> db.stores.aggregate([
45+
... {
46+
... $unwind: "$sales.salesByCategory"
47+
... },
48+
... {
49+
... $group: {
50+
... _id: "$sales.salesByCategory.categoryName",
51+
... totalSales: { $sum: "$sales.salesByCategory.totalSales" }
52+
... }
53+
... }
54+
... ])
55+
56+
[
57+
{ _id: 'Christmas Trees', totalSales: 3147281 },
58+
{ _id: 'Nuts', totalSales: 3002332 },
59+
{ _id: 'Camping Tables', totalSales: 4431667 }
60+
]
61+
62+
```
63+
64+
### Example 2: Find stores with full-time staff greater than 10
65+
66+
This example shows how to filter stores where the number of full-time staff is greater than 10.
67+
68+
```javascript
69+
db.stores.aggregate([
70+
{
71+
$match: {
72+
"staff.totalStaff.fullTime": { $gt: 10 }
73+
}
74+
}
75+
])
76+
```
77+
78+
#### Sample output
79+
80+
```javascript
81+
[mongos] StoreData> db.stores.aggregate([
82+
... {
83+
... $match: {
84+
... "staff.totalStaff.fullTime": { $gt: 10 }
85+
... }
86+
... }
87+
... ])
88+
89+
[
90+
{
91+
_id: '7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5',
92+
name: "Lenore's DJ Equipment Store",
93+
location: { lat: -9.9399, lon: -0.334 },
94+
staff: { totalStaff: { fullTime: 18, partTime: 7 } },
95+
sales: {
96+
totalSales: 35911,
97+
salesByCategory: [ { categoryName: 'DJ Headphones', totalSales: 35911 } ]
98+
},
99+
promotionEvents: [
100+
{
101+
discounts: [
102+
{ categoryName: 'DJ Turntables', discountPercentage: 18 },
103+
{ categoryName: 'DJ Mixers', discountPercentage: 15 }
104+
]
105+
}
106+
],
107+
tag: [ '#SeasonalSale', '#FreeShipping', '#MembershipDeals' ]
108+
}
109+
]
110+
```
111+
112+
### Example 3: List all promotion events with discounts greater than 15%
113+
114+
This example lists all promotion events where any discount is greater than 15%.
115+
116+
```javascript
117+
db.stores.aggregate([
118+
{
119+
$unwind: "$promotionEvents"
120+
},
121+
{
122+
$unwind: "$promotionEvents.discounts"
123+
},
124+
{
125+
$match: {
126+
"promotionEvents.discounts.discountPercentage": { $gt: 15 }
127+
}
128+
},
129+
{
130+
$group: {
131+
_id: "$promotionEvents.eventName",
132+
discounts: { $push: "$promotionEvents.discounts" }
133+
}
134+
}
135+
])
136+
```
137+
138+
#### Sample output
139+
140+
```javascript
141+
[mongos] StoreData> db.stores.aggregate([
142+
... {
143+
... $unwind: "$promotionEvents"
144+
... },
145+
... {
146+
... $unwind: "$promotionEvents.discounts"
147+
... },
148+
... {
149+
... $match: {
150+
... "promotionEvents.discounts.discountPercentage": { $gt: 20 }
151+
... }
152+
... },
153+
... {
154+
... $group: {
155+
... _id: "$promotionEvents.eventName",
156+
... discounts: { $push: "$promotionEvents.discounts" }
157+
... }
158+
... }
159+
... ])
160+
[
161+
{
162+
[
163+
{ categoryName: 'Basketball Gear', discountPercentage: 23 },
164+
{ categoryName: 'Wool Carpets', discountPercentage: 22 },
165+
{
166+
categoryName: 'Portable Bluetooth Speakers',
167+
discountPercentage: 24
168+
}
169+
]
170+
}
171+
]
172+
```
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
title: Distinct
3+
description: The distinct command is used to find the unique values for a specified field across a single collection.
4+
type: commands
5+
category: aggregation
6+
---
7+
8+
# distinct
9+
10+
The `distinct` command is used to find the unique values for a specified field across a single collection. This command is useful when you need to identify the set of distinct values for a field without retrieving all the documents or when you need to perform operations like filtering or grouping based on unique values.
11+
12+
## Syntax
13+
14+
The basic syntax of the `distinct` command is as follows:
15+
16+
```javascript
17+
db.collection.distinct(field, query, options)
18+
```
19+
20+
- `field`: The field that receives the returned distinct values.
21+
- `query`: Optional. A query that specifies the documents from which to retrieve the distinct values.
22+
- `options`: Optional. Other options for the command.
23+
24+
## Examples
25+
26+
Here are examples using the provided sample JSON structure.
27+
28+
### Example 1: Find distinct categories in sales
29+
30+
To find the distinct `categoryName` in the `salesByCategory` array:
31+
32+
```javascript
33+
db.stores.distinct("sales.salesByCategory.categoryName")
34+
```
35+
36+
#### Sample output
37+
38+
```javascript
39+
[mongos] StoreData> db.stores.distinct("sales.salesByCategory.categoryName")
40+
[
41+
{
42+
_id: 'Discount Derby',
43+
discounts: [
44+
{ categoryName: 'Bath Sheets', discountPercentage: 25 },
45+
{ categoryName: 'Tablecloths', discountPercentage: 25 },
46+
{ categoryName: 'Drapes', discountPercentage: 25 }
47+
]
48+
}
49+
]
50+
[mongos] StoreData> db.stores.distinct("sales.salesByCategory.categoryName")
51+
[
52+
'Music Theory Books',
53+
'Superfoods',
54+
'Harmonicas',
55+
'Garden Tools',
56+
... 883 more items
57+
]
58+
```
59+
60+
### Example 2: Find distinct event names in promotion events
61+
62+
To find the distinct `eventName` in the `promotionEvents` array:
63+
64+
```javascript
65+
db.stores.distinct("promotionEvents.eventName")
66+
```
67+
68+
#### Sample output
69+
70+
```javascript
71+
[mongos] StoreData> db.stores.distinct("promotionEvents.eventName")
72+
[
73+
{
74+
_id: 'Super Saver Celebration',
75+
discounts: [
76+
{ categoryName: 'Face Towels', discountPercentage: 25 },
77+
{ categoryName: 'Printer Ribbons', discountPercentage: 25 },
78+
{ categoryName: 'Chromebooks', discountPercentage: 25 }
79+
]
80+
}
81+
]
82+
```
83+
84+
### Example 3: Find distinct discount percentages for a specific event
85+
86+
To find the distinct `discountPercentage` in the `discounts` array for the "Summer Sale" event:
87+
88+
```javascript
89+
db.stores.distinct("promotionEvents.discounts.discountPercentage", { "promotionEvents.eventName": "Incredible Discount Days" })
90+
```
91+
92+
#### Sample output
93+
94+
```javascript
95+
[mongos] StoreData> db.stores.distinct("promotionEvents.discounts.discountPercentage", { "promotionEvents.eventName": "Incredible Discount Days" })
96+
[
97+
6, 17, 22, 25, 9, 15, 14,
98+
7, 12, 19, 24, 5, 20, 10,
99+
23, 16, 18, 21, 13, 11, 8
100+
]
101+
```

0 commit comments

Comments
 (0)