Skip to content

Commit 7c3dc37

Browse files
committed
you're doing great
1 parent ae1d6b0 commit 7c3dc37

File tree

3 files changed

+113
-1
lines changed

3 files changed

+113
-1
lines changed

046_mongodb/05_query/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ db.customers.find({$and: [{name:"Bond"}, {age:{$gt:20}}]})
3939
```
4040
db.customers.find({$or: [{name:"Bond"}, {age:67}]})
4141
db.customers.find({$or: [{name:"Bond"}, {age:{$lt:20}}]})
42-
db.customers.find({$or: [{name:"Bond"}, {age:{$gt:20}}]})
42+
db.customers.find({$or: [{name:"Bond"}, {age:{$gt:32}}]})
4343
```
4444

4545
### and or

046_mongodb/12_aggregate/README.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
# aggregate
2+
3+
Aggregations operations process data records and return computed results. Aggregation operations group values from multiple documents together, and can perform a variety of operations on the grouped data to return a single result. MongoDB provides three ways to perform aggregation: the aggregation pipeline, the map-reduce function, and single purpose aggregation methods.
4+
5+
[documenation about aggregation](https://docs.mongodb.com/manual/aggregation/)
6+
7+
### single purpose aggregation
8+
9+
[documenation about single purpose aggregation](https://docs.mongodb.com/manual/aggregation/#single-purpose-agg-operations)
10+
11+
There are two functions you can use:
12+
1. [db.collection.count()](https://docs.mongodb.com/manual/reference/method/db.collection.count/#db.collection.count)
13+
1. db.collection.distinct()
14+
15+
### examples - count()
16+
```
17+
db.oscars.count()
18+
```
19+
20+
The above is equivalent to
21+
22+
```
23+
db.oscars.find().count()
24+
```
25+
26+
```
27+
db.customers.find({role:"citizen"}).count()
28+
```
29+
30+
```
31+
db.customers.find({$or: [{name:"Bond"}, {age:{$gt:32}}]}).count()
32+
```
33+
34+
### examples - distinct() - setup
35+
```
36+
db.inventory.insert([
37+
{ "_id": 1, "dept": "A", "item": { "sku": "111", "color": "red" }, "sizes": [ "S", "M" ] },
38+
{ "_id": 2, "dept": "A", "item": { "sku": "111", "color": "blue" }, "sizes": [ "M", "L" ] },
39+
{ "_id": 3, "dept": "B", "item": { "sku": "222", "color": "blue" }, "sizes": "S" },
40+
{ "_id": 4, "dept": "A", "item": { "sku": "333", "color": "black" }, "sizes": [ "S" ] }
41+
])
42+
```
43+
44+
### examples - distinct()
45+
46+
syntax
47+
```
48+
db.collection.distinct(field, query, options)
49+
```
50+
51+
52+
| Parameter | Description |
53+
| --- | --- |
54+
| field | The field for which to return distinct values.
55+
| query | A query that specifies the documents from which to retrieve the distinct values.
56+
| options | Optional. A document that specifies the options. See Options.
57+
58+
```
59+
db.inventory.distinct( "dept" )
60+
```
61+
62+
embedded field:
63+
64+
```
65+
db.inventory.distinct( "item.sku" )
66+
```
67+
68+
```
69+
db.inventory.distinct( "sizes" )
70+
```
71+
72+
73+
You use different **expressions** (listed below) in aggregate group operations.
74+
75+
![aggregate pipeline](aggregate.png)
76+
77+
```
78+
db.<collection name>.aggregate([{<match, sort, geoNear>},{<group>}])
79+
```
80+
81+
[documenation](https://docs.mongodb.com/manual/aggregation/)
82+
[match, sort, geonear](https://docs.mongodb.com/manual/core/aggregation-pipeline/#aggregation-pipeline-operators-and-performance)
83+
84+
### examples - setup
85+
```
86+
db.orders.insert([
87+
{"cust_id":"A123","amount":500,"status":"A"},
88+
{"cust_id":"A123","amount":250,"status":"A"},
89+
{"cust_id":"B212","amount":200,"status":"A"},
90+
{"cust_id":"A123","amount":300,"status":"D"}
91+
])
92+
```
93+
94+
### examples
95+
```
96+
db.orders.aggregate([
97+
{$match:{status:"A"}},
98+
{$group:{_id: "$cust_id",total: {$sum:"$amount"}}}
99+
])
100+
```
101+
102+
103+
### expressions
104+
105+
| expression | description |
106+
107+
```
108+
db.oscars.createIndex({title:1})
109+
db.oscars.createIndex({releaseYear:1, releaseDay:1})
110+
```
111+
112+
[learn to create a unique index and more](https://docs.mongodb.com/manual/reference/method/db.collection.createIndex/#db.collection.createIndex)
290 KB
Loading

0 commit comments

Comments
 (0)