Code for How to Use MongoDB Database in Python Tutorial

View on Github

from pymongo import MongoClient
from pprint import pprint

# connect to the MongoDB server
client = MongoClient()
# or explicitly
# client = MongoClient("localhost", 27017)
# list all database names
print("Available databases:", client.list_database_names())
# access the database "python", this will create the actual database
# if it doesn't exist
database = client["python"]
# or this:
# database = client.python
# list all collections
print("Available collections:", database.list_collection_names())
# get books collection (or create one)
books = database["books"]
# insert a single book
result = books.insert_one({
    "name": "Invent Your Own Computer Games with Python, 4E",
    "author": "Al Sweigart",
    "price": 17.99,
    "url": ""

print("One book inserted:", result.inserted_id)

# insert many books
books_data = [
        "name": "Automate the Boring Stuff with Python: Practical Programming for Total Beginners",
        "author": "Al Sweigart",
        "price": 17.76,
        "url": ""
        "name": "Python Crash Course: A Hands-On, Project-Based Introduction to Programming",
        "author": "Eric Matthes",
        "price": 22.97,
        "url": ""
        "name": "MySQL for Python",
        "author": "Albert Lukaszewski",
        "price": 49.99,

result = books.insert_many(books_data)
print("Many books inserted, Ids:", result.inserted_ids)

# get a single book by a specific author
eric_book = books.find_one({"author": "Eric Matthes"})

# get all books by a specific author
sweigart_books = books.find({"author": "Al Sweigart"})
print("Al Sweigart's books:")

# get all documents in books collection
all_books = books.find({})
print("All books:")

# delete a specific document by a JSON query
result = books.delete_one({"author": "Albert Lukaszewski"})

# delete all books by Al Sweigart
result = books.delete_many({"author": "Al Sweigart"})

# printing all documents
all_books = books.find({})
print("All books:")

# drop this collection
# or this:
# books.drop()
# drop this entire database
# close the connection