Skip to content

Commit c87bf4a

Browse files
committed
you're doing great
1 parent 2142a75 commit c87bf4a

File tree

2 files changed

+228
-2
lines changed

2 files changed

+228
-2
lines changed

046_mongodb/14_users/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,9 @@ mongo
102102
should be unauthorized:
103103
```
104104
show collections
105+
```
106+
107+
#### drop user
108+
```
109+
db.dropUser("<user name>")
105110
```

046_mongodb/README.md

Lines changed: 223 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,9 +1363,230 @@ db.oscars.insert([
13631363
db.<collection name>.createIndex({<field to index>:<1 for ascend, -1 descend>})
13641364
```
13651365

1366+
#### create index
13661367
```
13671368
db.oscars.createIndex({title:1})
1368-
db.oscars.createIndex({releaseYear:1, releaseDay:1})
13691369
```
13701370

1371-
[learn to create a unique index and more](https://docs.mongodb.com/manual/reference/method/db.collection.createIndex/#db.collection.createIndex)
1371+
#### see indexes
1372+
```
1373+
db.oscars.getIndexes()
1374+
```
1375+
1376+
[learn to create a unique index and more](https://docs.mongodb.com/manual/reference/method/db.collection.createIndex/#db.collection.createIndex)
1377+
1378+
## 12_aggregate
1379+
1380+
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.
1381+
1382+
[documenation about aggregation](https://docs.mongodb.com/manual/aggregation/)
1383+
1384+
#### single purpose aggregation
1385+
1386+
[documenation about single purpose aggregation](https://docs.mongodb.com/manual/aggregation/#single-purpose-agg-operations)
1387+
1388+
There are two functions you can use:
1389+
1390+
#### [db.collection.count()](https://docs.mongodb.com/manual/reference/method/db.collection.count/#db.collection.count)
1391+
1392+
#### [db.collection.distinct()](https://docs.mongodb.com/manual/reference/method/db.collection.distinct/#db.collection.distinct)
1393+
1394+
```
1395+
db.collection.distinct(field, query, options)
1396+
```
1397+
1398+
| Parameter | Description |
1399+
| --- | --- |
1400+
| field | The field for which to return distinct values.
1401+
| query | A query that specifies the documents from which to retrieve the distinct values.
1402+
| options | Optional. A document that specifies the options. See Options.
1403+
1404+
#### examples - count()
1405+
```
1406+
db.oscars.count()
1407+
```
1408+
1409+
```
1410+
db.oscars.find().count()
1411+
```
1412+
1413+
```
1414+
db.customers.find({role:"citizen"}).count()
1415+
```
1416+
1417+
```
1418+
db.customers.find({$or: [{name:"Bond"}, {age:{$gt:32}}]}).count()
1419+
```
1420+
1421+
#### examples - distinct() - setup
1422+
```
1423+
db.inventory.insert([
1424+
{ "_id": 1, "dept": "A", "item": { "sku": "111", "color": "red" }, "sizes": [ "S", "M" ] },
1425+
{ "_id": 2, "dept": "A", "item": { "sku": "111", "color": "blue" }, "sizes": [ "M", "L" ] },
1426+
{ "_id": 3, "dept": "B", "item": { "sku": "222", "color": "blue" }, "sizes": "S" },
1427+
{ "_id": 4, "dept": "A", "item": { "sku": "333", "color": "black" }, "sizes": [ "S" ] }
1428+
])
1429+
```
1430+
1431+
#### examples - distinct()
1432+
1433+
```
1434+
db.inventory.distinct( "dept" )
1435+
```
1436+
1437+
```
1438+
db.inventory.distinct( "item.sku" )
1439+
```
1440+
1441+
```
1442+
db.inventory.distinct( "sizes" )
1443+
```
1444+
1445+
#### aggregation pipeline
1446+
1447+
![aggregate pipeline](aggregate.png)
1448+
1449+
```
1450+
db.<collection name>.aggregate([{<match, sort, geoNear>},{<group>}])
1451+
```
1452+
1453+
MongoDB’s aggregation framework is modeled on the concept of data processing pipelines. Documents enter a multi-stage pipeline that transforms the documents into an aggregated result.
1454+
1455+
The most basic pipeline stages provide filters that operate like queries and document transformations that modify the form of the output document.
1456+
1457+
Other pipeline operations provide tools for grouping and sorting documents by specific field or fields as well as tools for aggregating the contents of arrays, including arrays of documents. In addition, pipeline stages can use operators for tasks such as calculating the average or concatenating a string.
1458+
1459+
The pipeline provides efficient data aggregation using native operations within MongoDB, and is the preferred method for data aggregation in MongoDB.
1460+
1461+
[source](https://docs.mongodb.com/manual/aggregation/)
1462+
1463+
#### example - setup
1464+
```
1465+
db.orders.insert([
1466+
{"cust_id":"A123","amount":500,"status":"A"},
1467+
{"cust_id":"A123","amount":250,"status":"A"},
1468+
{"cust_id":"B212","amount":200,"status":"A"},
1469+
{"cust_id":"A123","amount":300,"status":"D"}
1470+
])
1471+
```
1472+
1473+
#### example
1474+
```
1475+
db.orders.aggregate([
1476+
{$match:{status:"A"}},
1477+
{$group:{_id: "$cust_id",total: {$sum:"$amount"}}}
1478+
])
1479+
```
1480+
1481+
## 13_additional
1482+
1483+
#### locking down your database
1484+
1485+
#### create admin super user
1486+
1487+
```
1488+
use admin
1489+
db.createUser(
1490+
{
1491+
user: "jamesbond",
1492+
pwd: "moneypennyrocks007sworld",
1493+
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
1494+
}
1495+
)
1496+
```
1497+
1498+
[built in user roles](https://docs.mongodb.com/manual/reference/built-in-roles/)
1499+
1500+
#### exit mongo & then start again
1501+
```
1502+
mongod
1503+
```
1504+
1505+
```
1506+
mongo -u "jamesbond" -p "moneypennyrocks007sworld" --authenticationDatabase "admin"
1507+
```
1508+
1509+
#### see current user
1510+
1511+
```
1512+
db.runCommand({connectionStatus : 1})
1513+
```
1514+
1515+
#### create regular user
1516+
Give this user readwrite permissions on the ```store``` db.
1517+
1518+
```
1519+
db.createUser(
1520+
{
1521+
user: "bond",
1522+
pwd: "moneypenny007",
1523+
roles: [ { role: "readWrite", db: "store" } ]
1524+
}
1525+
)
1526+
```
1527+
1528+
#### exit mongo & then start again
1529+
```
1530+
mongod
1531+
```
1532+
1533+
```
1534+
mongo -u "bond" -p "moneypenny007" --authenticationDatabase "store"
1535+
```
1536+
1537+
#### see current user
1538+
1539+
```
1540+
db.runCommand({connectionStatus : 1})
1541+
```
1542+
1543+
#### lock down the database
1544+
1545+
[enable auth](https://docs.mongodb.com/master/tutorial/enable-authentication/)
1546+
1547+
[getting auth running on mongo](https://docs.mongodb.com/manual/tutorial/enable-authentication/)
1548+
1549+
#### exit mongo & then start again with auth enabled
1550+
```
1551+
mongod --auth
1552+
```
1553+
1554+
```
1555+
mongo -u "bond" -p "moneypenny007" --authenticationDatabase "store"
1556+
```
1557+
1558+
#### test
1559+
1560+
```
1561+
use store
1562+
```
1563+
1564+
```
1565+
show collections
1566+
```
1567+
1568+
```
1569+
db.customers.find()
1570+
```
1571+
1572+
```
1573+
db.customers.insert({"role" : "double-zero", "name" : "Elon Musk", "age" : 47 })
1574+
```
1575+
1576+
#### test
1577+
1578+
launch a new terminal window
1579+
1580+
```
1581+
mongo
1582+
```
1583+
1584+
should be unauthorized:
1585+
```
1586+
show collections
1587+
```
1588+
1589+
#### drop user
1590+
```
1591+
db.dropUser("<user name>")
1592+
```

0 commit comments

Comments
 (0)