Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 132 additions & 0 deletions api-reference/commands/aggregation/aggregate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
---
title: Aggregate
description: The aggregate command is used to process data records and return computed results.
type: commands
category: aggregation
---

# aggregate

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.

## Syntax

```javascript
db.collection.aggregate(pipeline, options)
```

- **pipeline**: An array of aggregation stages that process and transform the data.
- **options**: Optional. Specifies more options for the aggregation, such as `explain`, `allowDiskUse`, and `cursor`.

## Examples

### Example 1: Calculate total sales by category

This example demonstrates how to calculate the total sales for each category in the `stores` collection.

```javascript
db.stores.aggregate([
{
$unwind: "$sales.salesByCategory"
},
{
$group: {
_id: "$sales.salesByCategory.categoryName",
totalSales: { $sum: "$sales.salesByCategory.totalSales" }
}
}
])
```

#### Sample output

```json
[
{ "_id": "Christmas Trees", "totalSales": 3147281 },
{ "_id": "Nuts", "totalSales": 3002332 },
{ "_id": "Camping Tables", "totalSales": 4431667 }
]
```

### Example 2: Find stores with full-time staff greater than 10

This example shows how to filter stores where the number of full-time staff is greater than 10.

```javascript
db.stores.aggregate([
{
$match: {
"staff.totalStaff.fullTime": { $gt: 10 }
}
}
])
```

#### Sample output

```json
[
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"name": "Lenore's DJ Equipment Store",
"location": { "lat": -9.9399, "lon": -0.334 },
"staff": { "totalStaff": { "fullTime": 18, "partTime": 7 } },
"sales": {
"totalSales": 35911,
"salesByCategory": [ { "categoryName": "DJ Headphones", "totalSales": 35911 } ]
},
"promotionEvents": [
{
"discounts": [
{ "categoryName": "DJ Turntables", "discountPercentage": 18 },
{ "categoryName": "DJ Mixers", "discountPercentage": 15 }
]
}
],
"tag": [ "#SeasonalSale", "#FreeShipping", "#MembershipDeals" ]
}
]
```

### Example 3: List all promotion events with discounts greater than 15%

This example lists all promotion events where any discount is greater than 15%.

```javascript
db.stores.aggregate([
{
$unwind: "$promotionEvents"
},
{
$unwind: "$promotionEvents.discounts"
},
{
$match: {
"promotionEvents.discounts.discountPercentage": { $gt: 15 }
}
},
{
$group: {
_id: "$promotionEvents.eventName",
discounts: { $push: "$promotionEvents.discounts" }
}
}
])
```

#### Sample output

```json
[
{
"discounts": [
{ "categoryName": "Basketball Gear", "discountPercentage": 23 },
{ "categoryName": "Wool Carpets", "discountPercentage": 22 },
{
"categoryName": "Portable Bluetooth Speakers",
"discountPercentage": 24
}
]
}
]
```
81 changes: 81 additions & 0 deletions api-reference/commands/aggregation/count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
title: Count
description: Count command is used to count the number of documents in a collection that match a specified query.
type: commands
category: aggregation
---

# count

The `count` command is used to count the number of documents in a collection that match a specified query. This command is useful for obtaining quick statistics about the data stored in your collections, such as the number of documents that meet certain criteria.

## Syntax

The syntax for the `count` command is as follows:

```javascript
db.collection.count(query, options)
```

- `query`: A document specifying the selection criteria using query operators.
- `options`: Optional. A document specifying options, such as `limit` and `skip`.

## Examples

Here are some examples to demonstrate the usage of the `count` command:

### Example 1. Counting all documents in a collection

To count all documents in the `stores` collection:

```javascript
db.stores.count({})
```

#### Sample output

```json
60570
```

### Example 2. Counting documents with specific criteria

To count the number of stores with a specific `_id` store ID:

```javascript
db.stores.count({ "_id": "e5767a9f-cd95-439c-9ec4-7ddc13d22926" })
```

#### Sample output

```json
1
```

### Example 3. Counting documents with nested criteria

To count the number of stores that have a specific promotion event:

```javascript
db.stores.count({ "promotionEvents.eventName": "Incredible Discount Days" })
```

#### Sample output

```json
2156
```

### Example 4. Counting documents with multiple criteria

To count the number of stores located at a specific latitude and longitude:

```javascript
db.stores.count({ "location.lat": -2.4111, "location.lon": 72.1041 })
```

#### Sample output

```json
1
```
80 changes: 80 additions & 0 deletions api-reference/commands/aggregation/distinct.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
---
title: Distinct
description: The distinct command is used to find the unique values for a specified field across a single collection.
type: commands
category: aggregation
---

# distinct

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.

## Syntax

The basic syntax of the `distinct` command is as follows:

```javascript
db.collection.distinct(field, query, options)
```

- `field`: The field that receives the returned distinct values.
- `query`: Optional. A query that specifies the documents from which to retrieve the distinct values.
- `options`: Optional. Other options for the command.

## Examples

Here are examples using the provided sample JSON structure.

### Example 1: Find distinct categories in sales

To find the distinct `categoryName` in the `salesByCategory` array:

```javascript
db.stores.distinct("sales.salesByCategory.categoryName")
```

#### Sample output

```json
[
"Music Theory Books",
"Superfoods",
"Harmonicas",
"Garden Tools"
]
```

### Example 2: Find distinct event names in promotion events

To find the distinct `eventName` in the `promotionEvents` array:

```javascript
db.stores.distinct("promotionEvents.eventName")
```

#### Sample output

```json
[
"Super Saver Celebration",
"Incredible Discount Days"
]
```

### Example 3: Find distinct discount percentages for a specific event

To find the distinct `discountPercentage` in the `discounts` array for the "Summer Sale" event:

```javascript
db.stores.distinct("promotionEvents.discounts.discountPercentage", { "promotionEvents.eventName": "Incredible Discount Days" })
```

#### Sample output

```json
[
6, 17, 22, 25, 9, 15, 14,
7, 12, 19, 24, 5, 20, 10,
23, 16, 18, 21, 13, 11, 8
]
```