Skip to content
This repository was archived by the owner on Jul 28, 2026. It is now read-only.

Commit 20b3143

Browse files
committed
Add all Python examples
1 parent 6cb6882 commit 20b3143

13 files changed

Lines changed: 1013 additions & 790 deletions

‎.devcontainer/setup.sh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
sudo apt-get update -y
33
sudo apt-get install jupyter -y
44
sudo python3 ijava-1.3.0/install.py
5-
sudo apt install python3-pymongo
5+
sudo apt install python3-pymongo -y

‎python/00_open_mongodb.ipynb‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Open MongoDB Using the MongoDB Extension\n",
8+
"\n",
9+
"- click on the MongoDB extension on the side panel\n",
10+
"- add a new connection clicking on `Add Connection`\n",
11+
"- use as connection:"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": null,
17+
"metadata": {
18+
"vscode": {
19+
"languageId": "plaintext"
20+
}
21+
},
22+
"outputs": [],
23+
"source": [
24+
"mongodb://admin:mongodb@localhost:27017/"
25+
]
26+
},
27+
{
28+
"cell_type": "markdown",
29+
"metadata": {},
30+
"source": [
31+
"You should see four databases in the MongoDB database server:\n",
32+
"- admin\n",
33+
"- config\n",
34+
"- local\n",
35+
"- library -> we're going to work with this one"
36+
]
37+
},
38+
{
39+
"cell_type": "markdown",
40+
"metadata": {},
41+
"source": [
42+
"## Open the library database\n",
43+
"\n",
44+
"- open the library database\n",
45+
"- open the books collection\n",
46+
"- click on Documents\n",
47+
"- open a document"
48+
]
49+
}
50+
],
51+
"metadata": {
52+
"language_info": {
53+
"name": "python"
54+
}
55+
},
56+
"nbformat": 4,
57+
"nbformat_minor": 2
58+
}

‎python/01_connect_database.ipynb‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## Connect to MongoDB"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": null,
13+
"metadata": {},
14+
"outputs": [],
15+
"source": [
16+
"from pymongo import MongoClient\n",
17+
"\n",
18+
"\n",
19+
"uri = \"mongodb://admin:mongodb@localhost:27017/\"\n",
20+
"client = MongoClient(uri)\n",
21+
"library_database = client[\"library\"]\n",
22+
"\n",
23+
"books = library_database[\"books\"]"
24+
]
25+
},
26+
{
27+
"cell_type": "markdown",
28+
"metadata": {},
29+
"source": [
30+
"## Send a ping command"
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": null,
36+
"metadata": {},
37+
"outputs": [],
38+
"source": [
39+
"library_database.command(\"ping\")"
40+
]
41+
},
42+
{
43+
"cell_type": "markdown",
44+
"metadata": {},
45+
"source": [
46+
"## Get a list of collections in the database"
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": null,
52+
"metadata": {},
53+
"outputs": [],
54+
"source": [
55+
"collection_list = library_database.list_collections()\n",
56+
"\n",
57+
"for c in collection_list:\n",
58+
" print(c)"
59+
]
60+
},
61+
{
62+
"cell_type": "markdown",
63+
"metadata": {},
64+
"source": [
65+
"## Count documents"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": null,
71+
"metadata": {},
72+
"outputs": [],
73+
"source": [
74+
"books.count_documents({})\n"
75+
]
76+
}
77+
],
78+
"metadata": {
79+
"kernelspec": {
80+
"display_name": "Python 3",
81+
"language": "python",
82+
"name": "python3"
83+
},
84+
"language_info": {
85+
"codemirror_mode": {
86+
"name": "ipython",
87+
"version": 3
88+
},
89+
"file_extension": ".py",
90+
"mimetype": "text/x-python",
91+
"name": "python",
92+
"nbconvert_exporter": "python",
93+
"pygments_lexer": "ipython3",
94+
"version": "3.11.2"
95+
}
96+
},
97+
"nbformat": 4,
98+
"nbformat_minor": 2
99+
}

‎python/02_find.ipynb‎

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# CRUD: find"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": null,
13+
"metadata": {},
14+
"outputs": [],
15+
"source": [
16+
"from pymongo import MongoClient\n",
17+
"\n",
18+
"\n",
19+
"uri = \"mongodb://admin:mongodb@localhost:27017/\"\n",
20+
"client = MongoClient(uri)\n",
21+
"library_database = client[\"library\"]\n",
22+
"\n",
23+
"books = library_database[\"books\"]"
24+
]
25+
},
26+
{
27+
"cell_type": "markdown",
28+
"metadata": {},
29+
"source": [
30+
"## Find one book"
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": null,
36+
"metadata": {},
37+
"outputs": [],
38+
"source": [
39+
"books.find_one()"
40+
]
41+
},
42+
{
43+
"cell_type": "markdown",
44+
"metadata": {},
45+
"source": [
46+
"## Find books written after 2008"
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": null,
52+
"metadata": {},
53+
"outputs": [],
54+
"source": [
55+
"results = books.find({ \"year\": { \"$gt\" : 2008 }})\n",
56+
"\n",
57+
"for f in results:\n",
58+
" print(f) "
59+
]
60+
},
61+
{
62+
"cell_type": "markdown",
63+
"metadata": {},
64+
"source": [
65+
"## Exercise: find all the books that have less than 50 pages and print only the title and pages"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": null,
71+
"metadata": {},
72+
"outputs": [],
73+
"source": [
74+
"filter = { \"pages\": { \"$lt\" : 50 }}\n",
75+
"results = books.find(filter)\n",
76+
"\n",
77+
"for f in results:\n",
78+
" print(f\"{f['title']} - {f['pages']} - {f['year']}.\") "
79+
]
80+
},
81+
{
82+
"cell_type": "markdown",
83+
"metadata": {},
84+
"source": [
85+
"## Exercise: find all the books that have less than 50 pages and were published after 2008. Print only title, pages and year"
86+
]
87+
},
88+
{
89+
"cell_type": "code",
90+
"execution_count": null,
91+
"metadata": {},
92+
"outputs": [],
93+
"source": [
94+
"filter = { \n",
95+
" \"$and\": [\n",
96+
" { \"pages\": { \"$lt\": 50 }},\n",
97+
" { \"year\": { \"$gt\" : 2008 }}\n",
98+
" ]\n",
99+
"}\n",
100+
"\n",
101+
"results = books.find(filter)\n",
102+
"\n",
103+
"for f in results:\n",
104+
" print(f\"{f['title']} - {f['pages']} - {f['year']}.\") "
105+
]
106+
},
107+
{
108+
"cell_type": "code",
109+
"execution_count": null,
110+
"metadata": {},
111+
"outputs": [],
112+
"source": [
113+
"filter = { \n",
114+
" \"$or\": [\n",
115+
" { \"pages\": { \"$lt\": 50 }},\n",
116+
" { \"year\": { \"$gt\" : 2008 }}\n",
117+
" ]\n",
118+
"}\n",
119+
"\n",
120+
"results = books.find(filter)\n",
121+
"\n",
122+
"for f in results:\n",
123+
" print(f\"{f['title']} - {f['pages']} - {f['year']}.\") "
124+
]
125+
},
126+
{
127+
"cell_type": "markdown",
128+
"metadata": {},
129+
"source": [
130+
"## Projections"
131+
]
132+
},
133+
{
134+
"cell_type": "code",
135+
"execution_count": null,
136+
"metadata": {},
137+
"outputs": [],
138+
"source": [
139+
"filter = { \n",
140+
" \"$and\": [\n",
141+
" { \"pages\": { \"$lt\": 50 }},\n",
142+
" { \"year\": { \"$gt\" : 2008 }}\n",
143+
" ]\n",
144+
"}\n",
145+
"\n",
146+
"projection = {\n",
147+
" \"title\": 1,\n",
148+
" \"pages\": 1,\n",
149+
" \"year\": 1,\n",
150+
"}\n",
151+
"\n",
152+
"results = books.find(filter, projection)\n",
153+
"\n",
154+
"for f in results:\n",
155+
" print(f) "
156+
]
157+
},
158+
{
159+
"cell_type": "markdown",
160+
"metadata": {},
161+
"source": [
162+
"Exclude `_id` field"
163+
]
164+
},
165+
{
166+
"cell_type": "code",
167+
"execution_count": null,
168+
"metadata": {},
169+
"outputs": [],
170+
"source": [
171+
"filter = { \n",
172+
" \"$and\": [\n",
173+
" { \"pages\": { \"$lt\": 50 }},\n",
174+
" { \"year\": { \"$gt\" : 2008 }}\n",
175+
" ]\n",
176+
"}\n",
177+
"\n",
178+
"projection = {\n",
179+
" \"_id\": 0,\n",
180+
" \"title\": 1,\n",
181+
" \"pages\": 1,\n",
182+
" \"year\": 1,\n",
183+
"}\n",
184+
"\n",
185+
"results = books.find(filter, projection)\n",
186+
"\n",
187+
"for f in results:\n",
188+
" print(f) "
189+
]
190+
}
191+
],
192+
"metadata": {
193+
"kernelspec": {
194+
"display_name": "Python 3",
195+
"language": "python",
196+
"name": "python3"
197+
},
198+
"language_info": {
199+
"codemirror_mode": {
200+
"name": "ipython",
201+
"version": 3
202+
},
203+
"file_extension": ".py",
204+
"mimetype": "text/x-python",
205+
"name": "python",
206+
"nbconvert_exporter": "python",
207+
"pygments_lexer": "ipython3",
208+
"version": "3.11.2"
209+
}
210+
},
211+
"nbformat": 4,
212+
"nbformat_minor": 2
213+
}

0 commit comments

Comments
 (0)