Skip to content

Commit

Permalink
Merge branch 'master' of github.com:tryber/mongodb-aggregation-cheats…
Browse files Browse the repository at this point in the history
…heet
  • Loading branch information
phelipe-ohlsen committed Mar 9, 2021
2 parents 5be2b3b + e891e18 commit 77fb502
Showing 1 changed file with 172 additions and 0 deletions.
172 changes: 172 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,178 @@

## Operadores Aggregation

### $match

**Template**

```javascript
db.collection.aggregate([
{ $match: { <query> } },
]);
```

**Exemplo**

```javascript
db.workers.aggregate([
{ $match: { workerName: "Tiago" } },
]);
```

[Documentação](https://docs.mongodb.com/manual/reference/operator/aggregation/match/")

---

### $limit

**Template**

```javascript
db.collection.aggregate([
{ $limit: <inteiro positivo> },
]);
```

**Exemplo**

```javascript
db.products.aggregate([
{ $match: { laptop: 'Dell' } },
{ $limit: 5 },
]);
```

[Documentação](https://docs.mongodb.com/manual/reference/operator/aggregation/limit/")

---

### $group

**Template**

```javascript
db.collection.aggregate([
{
$group: {
_id: <expressão>,
<campo1>: { <acumulador1> : <expressão1> },
...
<campoN>: { <acumuladorN> : <expressãoN> },
},
},
]);
```

**Exemplo**

```javascript
db.products.aggregate([
{
$group : {
_id : "$laptopId",
count: { $sum: 1 },
},
},
]);
```

[Documentação](https://docs.mongodb.com/manual/reference/operator/aggregation/group/")

---

### $project

**Template**

```javascript
db.collection.aggregate([
{
project: {
<especificação(ões)>
},
},
]);
```

**Exemplo**

```javascript
db.products.aggregate([
{
$project: {
_id: 0, // ou false
productName: "$laptop",
quantity: 1, // ou true
profit: {
$subtract: ["$sale_price", "$cost_price"]
},
},
},
]);
```

[Documentação](https://docs.mongodb.com/manual/reference/operator/aggregation/project)

---

### $unwind

**Template**

```javascript
db.collection.aggregate([
{ $unwind: <caminho do campo array> },
]);
```

**Exemplo**

```javascript
db.streamings.aggregate([
{ $unwind: "$netflix_plans" },
]);
```

[Documentação](https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/)

---

### $lookup

**Template**

```javascript
db.collection.aggregate([
{
$lookup: {
from: <coleção para unir>,
localField: <campo dos documentos de entrada>,
foreignField: <campo dos documentos provenientes da coleção conectada,
as: <campo do array de saída>
},
},
]);
```

**Exemplo**

```javascript
db.orders.aggregate([
{
$lookup: {
from: <inventory>,
localField: <item>,
foreignField: <sku>,
as: <inventory_docs>
},
},
]);
```

[Documentação](https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/)

---

### $lookup (let/pipeline)

**Template**
Expand Down

0 comments on commit 77fb502

Please sign in to comment.