Skip to content

Commit bef997c

Browse files
mongodb
Docs, poc/mvp, updated deps
1 parent 2bcba81 commit bef997c

File tree

6 files changed

+156
-4
lines changed

6 files changed

+156
-4
lines changed

README.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ Be the change et al if Windows is your main and you wanna raise a PR with broad
2929
* [Django](#django)
3030
* [Flask](#flask)
3131
* [FastAPI](#fastapi)
32+
* [NoSQL](#nosql)
33+
* [MongoDB](#mongodb)
3234
* [Kubernetes (k8s)](#kubernetes-k8s)
3335
* [Terraform](#terraform)
3436
* [GitHub Actions](#github-actions)
@@ -319,6 +321,44 @@ deactivate
319321
320322
### FastAPI
321323
324+
### NoSQL
325+
#### MongoDB
326+
* See [Docker](#docker) section above
327+
* Setup empty DB as a detached container with port 27017 mapped
328+
```bash
329+
# start mongodb docker container
330+
docker run --name mongodb -d -p 27017:27017 mongo
331+
332+
# stop container
333+
docker stop mongodb
334+
```
335+
* Access DB
336+
* GUI: Install [MongoDB Compass](https://www.mongodb.com/try/download/compass)
337+
![compass](img/compass.png)
338+
* Shell
339+
```bash
340+
# install mongo client
341+
asdf install mongodb latest
342+
343+
# open client with defaults (mongodb://localhost:27017)
344+
mongo
345+
346+
# list databases
347+
show dbs
348+
```
349+
* Python (ayyyy)
350+
```python
351+
# ./nosql/mongo/mongo_mvp.py
352+
import pymongo
353+
354+
client = pymongo.MongoClient('mongodb://localhost:27017/')
355+
db = client["music"]
356+
collection = db["albums"]
357+
data = { "artist": "Grimes", "album": "Miss Anthropocene" }
358+
collection.insert_one(data)
359+
client.close()
360+
```
361+
322362
### Kubernetes (k8s)
323363
* Easiest way to set up a local single node cluster is via one of the following:
324364
* [Rancher Desktop](https://docs.rancherdesktop.io/getting-started/installation)
@@ -527,6 +567,9 @@ deactivate
527567
* Flask
528568
* Bonus points for [Svelte](https://svelte.dev/blog/the-easiest-way-to-get-started) front-end ❤️
529569
* FastAPI
570+
* MongoDB
571+
* Switch to `docker-compose`
572+
* Fix unique index deleting too many keys
530573
* k8s
531574
* `~/.kubeconfig`
532575
* ansible
@@ -566,3 +609,9 @@ deactivate
566609
[Advanced settings configuration in WSL | Microsoft Docs](https://docs.microsoft.com/en-us/windows/wsl/wsl-config)
567610
568611
[Understanding The Python Path Environment Variable in Python](https://www.simplilearn.com/tutorials/python-tutorial/python-path)
612+
613+
[Docker & MongoDB | Containers & Compatibility | MongoDB](https://www.mongodb.com/compatibility/docker)
614+
615+
[MongoDB Python Connection | MongoDB](https://www.mongodb.com/languages/python)
616+
617+
[Python MongoDB](https://www.w3schools.com/python/python_mongodb_getstarted.asp)

img/compass.png

717 KB
Loading

nosql/mongo/mongo_mvp.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env python3
2+
3+
# import dnspython
4+
import pymongo
5+
from icecream import ic
6+
from pandas import DataFrame
7+
8+
# verbose icecream
9+
ic.configureOutput(includeContext=True)
10+
11+
# TODO: ORM for mongo; last.fm API to generate data
12+
13+
# connect to mongodb
14+
client = pymongo.MongoClient('mongodb://localhost:27017/')
15+
16+
# create database
17+
db = client["music"]
18+
19+
# create collection (table in relational database)
20+
collection = db["albums"]
21+
22+
# insert data
23+
data = { "artist": "Grimes", "album": "Miss Anthropocene" }
24+
collection.insert_one(data)
25+
26+
# find data
27+
for x in collection.find():
28+
ic(x)
29+
30+
# query data
31+
for x in collection.find({"artist": "Grimes"}):
32+
ic(x)
33+
34+
# delete album
35+
collection.delete_one({"album": "Miss Anthropocene"})
36+
37+
# update album as 'Art Angels'
38+
data = { "artist": "Grimes", "album": "Art Angels" }
39+
collection.update_one({"album": "Miss Anthropocene"}, {"$set": data})
40+
41+
# insert data
42+
data = { "artist": "Super Furry Animals", "album": "Phantom Power" }
43+
collection.insert_one(data)
44+
45+
# sort data
46+
for x in collection.find().sort("artist", pymongo.ASCENDING):
47+
ic(x)
48+
49+
# TODO: debug -- prefers SFA over Grimes
50+
# remove duplicate artists and albums
51+
collection.create_index([("artist", pymongo.ASCENDING), ("album", pymongo.ASCENDING)], unique=True)
52+
53+
# dataframe
54+
df = DataFrame(list(collection.find()))
55+
ic(df)
56+
57+
# drop collection
58+
collection.drop()
59+
60+
# remove database
61+
client.drop_database("music")
62+
63+
# close connection
64+
client.close()

poetry.lock

Lines changed: 38 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ GitPython = "^3.1.26"
1515
pyee = "^9.0.4"
1616
websockets = "^10.3"
1717
Django = "^4.0.6"
18+
pymongo = "^4.2.0"
19+
dnspython = "^2.2.1"
1820

1921
[tool.poetry.dev-dependencies]
2022
icecream = "^2.1.1"

requirements.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ bs4==0.0.1
66
cattrs==22.1.0; python_version >= "3.7" and python_version < "4.0"
77
certifi==2022.6.15; python_version >= "3.7" and python_version < "4"
88
charset-normalizer==2.1.0; python_version >= "3.7" and python_version < "4" and python_full_version >= "3.6.0"
9-
django==4.0.6; python_version >= "3.8"
9+
django==4.1; python_version >= "3.8"
10+
dnspython==2.2.1; python_version >= "3.6" and python_version < "4.0"
1011
exceptiongroup==1.0.0rc8; python_full_version >= "3.10.0" and python_full_version < "4.0.0" and python_version >= "3.7" and python_version <= "3.10"
1112
gitdb==4.0.9; python_version >= "3.7"
1213
gitpython==3.1.27; python_version >= "3.7"
@@ -16,6 +17,7 @@ numpy==1.23.1; python_version >= "3.10"
1617
pandas==1.4.3; python_version >= "3.8"
1718
playwright==1.18.1; python_version >= "3.7"
1819
pyee==9.0.4
20+
pymongo==4.2.0; python_version >= "3.7"
1921
python-dateutil==2.8.2; python_version >= "3.8" and python_full_version < "3.0.0" or python_full_version >= "3.3.0" and python_version >= "3.8"
2022
pytz==2022.1; python_version >= "3.8"
2123
requests-cache==0.9.5; python_version >= "3.7" and python_version < "4.0"

0 commit comments

Comments
 (0)