Skip to content

Commit 3a3a794

Browse files
authored
Merge pull request #180 from realpython/python-mongodb
MongoDB tutorial code examples
2 parents 40cfd10 + 9f1614c commit 3a3a794

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

python-mongodb/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Description
2+
3+
The files in this folder provide the code examples discussed in the tutorial [Python and MongoDB: Connecting to NoSQL Databases](https://realpython.com/introduction-to-mongodb-and-python/).
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from mongoengine import Document, ListField, StringField, URLField, connect
2+
3+
# Establish a connection
4+
connect(db="rptutorials", host="localhost", port=27017)
5+
6+
7+
# Define a document schema or model
8+
class Tutorial(Document):
9+
title = StringField(required=True, max_length=70)
10+
author = StringField(required=True, max_length=20)
11+
contributors = ListField(StringField(max_length=20))
12+
url = URLField(required=True)
13+
14+
15+
# Create and save documents
16+
tutorial1 = Tutorial(
17+
title="Beautiful Soup: Build a Web Scraper With Python",
18+
author="Martin",
19+
contributors=["Aldren", "Geir", "Jaya", "Joanna", "Mike"],
20+
url="https://realpython.com/beautiful-soup-web-scraper-python/",
21+
)
22+
tutorial1.save() # Insert the new tutorial
23+
24+
# Retrieving all documents and print their title
25+
for doc in Tutorial.objects:
26+
print(doc.title)
27+
28+
# Filtering documents by author
29+
for doc in Tutorial.objects(author="Alex"):
30+
print(doc.title)

python-mongodb/pymongo_example.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import pprint
2+
3+
from pymongo import MongoClient
4+
5+
# Establish a connection
6+
client = MongoClient("localhost", 27017)
7+
8+
# Access the database
9+
db = client.rptutorials
10+
11+
# Access a collection
12+
tutorial = db.tutorial
13+
14+
# Insert a single document
15+
tutorial1 = {
16+
"title": "Working With JSON Data in Python",
17+
"author": "Lucas",
18+
"contributors": ["Aldren", "Dan", "Joanna"],
19+
"url": "https://realpython.com/python-json/",
20+
}
21+
result = tutorial.insert_one(tutorial1)
22+
print(f"One tutorial: {result.inserted_id}")
23+
24+
# Insert several documents
25+
tutorial2 = {
26+
"title": "Python’s Requests Library (Guide)",
27+
"author": "Alex",
28+
"contributors": ["Aldren", "Brad", "Joanna"],
29+
"url": "https://realpython.com/python-requests/",
30+
}
31+
32+
tutorial3 = {
33+
"title": "Object-Oriented Programming (OOP) in Python 3",
34+
"author": "David",
35+
"contributors": ["Aldren", "Joanna", "Jacob"],
36+
"url": "https://realpython.com/python3-object-oriented-programming/",
37+
}
38+
39+
new_result = tutorial.insert_many([tutorial2, tutorial3])
40+
print(f"Multiple tutorials: {new_result.inserted_ids}")
41+
42+
# Retrieve all documents
43+
for doc in tutorial.find():
44+
pprint.pprint(doc)
45+
46+
# Retrieve a single document
47+
jon_tutorial = tutorial.find_one({"author": "Jon"})
48+
pprint.pprint(jon_tutorial)
49+
50+
# Close a connection
51+
with MongoClient() as client:
52+
db = client.rptutorials
53+
for doc in db.tutorial.find():
54+
pprint.pprint(doc)

0 commit comments

Comments
 (0)