Skip to content

Commit fb50388

Browse files
committed
added mongodb tutorial
1 parent a2d7c9e commit fb50388

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,6 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
104104
- ### Database
105105
- [How to Use MySQL Database in Python](https://www.thepythoncode.com/article/using-mysql-database-in-python). ([code](database/mysql-connector))
106106
- [How to Connect to a Remote MySQL Database in Python](https://www.thepythoncode.com/article/connect-to-a-remote-mysql-server-in-python). ([code](database/connect-to-remote-mysql-server))
107+
- [How to Use MongoDB Database in Python](https://www.thepythoncode.com/article/introduction-to-mongodb-in-python). ([code](database/mongodb-client))
107108

108109
For any feedback, please consider pulling requests.

database/mongodb-client/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Use MongoDB Database in Python](https://www.thepythoncode.com/article/introduction-to-mongodb-in-python)

database/mongodb-client/mongodb.py

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
from pymongo import MongoClient
2+
from pprint import pprint
3+
4+
# connect to the MongoDB server
5+
client = MongoClient()
6+
# or explicitly
7+
# client = MongoClient("localhost", 27017)
8+
# list all database names
9+
print("Available databases:", client.list_database_names())
10+
# access the database "python", this will create the actual database
11+
# if it doesn't exist
12+
database = client["python"]
13+
# or this:
14+
# database = client.python
15+
# list all collections
16+
print("Available collections:", database.list_collection_names())
17+
# get books collection (or create one)
18+
books = database["books"]
19+
# insert a single book
20+
result = books.insert_one({
21+
"name": "Invent Your Own Computer Games with Python, 4E",
22+
"author": "Al Sweigart",
23+
"price": 17.99,
24+
"url": "https://amzn.to/2zxN2W2"
25+
})
26+
27+
print("One book inserted:", result.inserted_id)
28+
29+
30+
# insert many books
31+
books_data = [
32+
{
33+
"name": "Automate the Boring Stuff with Python: Practical Programming for Total Beginners",
34+
"author": "Al Sweigart",
35+
"price": 17.76,
36+
"url": "https://amzn.to/2YAncdY"
37+
},
38+
{
39+
"name": "Python Crash Course: A Hands-On, Project-Based Introduction to Programming",
40+
"author": "Eric Matthes",
41+
"price": 22.97,
42+
"url": "https://amzn.to/2yQfQZl"
43+
},
44+
{
45+
"name": "MySQL for Python",
46+
"author": "Albert Lukaszewski",
47+
"price": 49.99,
48+
}
49+
]
50+
51+
result = books.insert_many(books_data)
52+
print("Many books inserted, Ids:", result.inserted_ids)
53+
54+
# get a single book by a specific author
55+
eric_book = books.find_one({"author": "Eric Matthes"})
56+
pprint(eric_book)
57+
58+
# get all books by a specific author
59+
sweigart_books = books.find({"author": "Al Sweigart"})
60+
print("Al Sweigart's books:")
61+
pprint(list(sweigart_books))
62+
63+
# get all documents in books collection
64+
all_books = books.find({})
65+
print("All books:")
66+
pprint(list(all_books))
67+
68+
# delete a specific document by a JSON query
69+
result = books.delete_one({"author": "Albert Lukaszewski"})
70+
71+
# delete all books by Al Sweigart
72+
result = books.delete_many({"author": "Al Sweigart"})
73+
74+
# printing all documents
75+
all_books = books.find({})
76+
print("All books:")
77+
pprint(list(all_books))
78+
79+
# drop this collection
80+
database.drop_collection("books")
81+
# or this:
82+
# books.drop()
83+
# drop this entire database
84+
client.drop_database("python")
85+
# close the connection
86+
client.close()
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pymongo

0 commit comments

Comments
 (0)