Movatterモバイル変換


[0]ホーム

URL:


How to Use MongoDB Database in Python

Learn how to connect to a MongoDB database, list databases, collections, insert data into collections, fetching data from collections and more in Python using pymongo driver module.
  · 7 min read · Updated may 2022 ·Database

Ready to take Python coding to a new level? Explore ourPython Code Generator. The perfect tool to get your code up and running in no time. Start now!

MongoDB is a cross-platform document-oriented database. It is classified as aNoSQL database, as it stores data in flexible, JSON-like documents, meaning fields can vary from document to another and the data structure can be changed over time.

Unlike relational databases such asMySQL that store data within tables, MongoDB stores data in collections inBSON format, which is a binary serialization ofJSON, stands for "Binary JSON", anything you can do with JSON, you can do it with BSON as well, but with additional extensions that are not part of JSON, such asDate andBinData data types.

Related: How to Work with JSON Files in Python.

There are many reasons to choose MongoDB over SQL databases, including:

  • Making most of the cloud computing and storage
  • Flexibility
  • Speeding development
  • It is a distributed database, so high availability, horizontal scaling and geographic distribution are built in and easy to use.

Here is what we gonna cover in this tutorial:

Getting Started

First, you are going to need to install MongoDB in your machine, I highly suggest you check theofficial MongoDB's installation guide,you need to follow the procedure for your operating system.

Second, you need to install MongoDB Python driver:pymongo:

pip3 install pymongo

If you just installpymongo viapip, then you're good to go, the latest version ofpymongo supports all MongoDB database versions.

Finally, You need to start the MongoDB daemon using the following command:

$ mongod

Connecting to a MongoDB Database

After you've installed MongoDB and started the Mongo daemon usingmongod command, the below code is responsible for connecting to the database:

from pymongo import MongoClientfrom pprint import pprint# connect to the MongoDB serverclient = MongoClient()# or explicitly# client = MongoClient("localhost", 27017)

Passing no parameters is the same as specifyinglocalhost as host and the default port27017 of MongoDB. Note that this will not make any connection yet, it will do only once you do any operation (such as getting server information).

If you want to get some information about the server, you can usingclient.server_info() method.

Listing Databases

Let's get all the databases available in our MongoDB server:

# list all database namesprint("Available databases:", client.list_database_names())

This will only output the names for the databases, you can useclient.list_databases() method if you want to get some information about databases such as size taken on disk, here is my output:

Available databases: ['admin', 'config', 'db', 'local', 'mydb']

Don't worry if you don't have the same output as mine, I had MongoDB for a while and for sure, I made a lot of work there.

Accessing a Database

To access a specific database, we can either by accessing it as an attribute or Python's dictionary-style:

# access the database "python", this will create the actual database# if it doesn't existdatabase = client["python"]# or this:# database = client.python

Note that the database does not exist, it'll be created automatically once we create a collection (like a table in SQL) and insert a document.

Listing All Collections with a Database

Collection is the term for a table in MongoDB. To get all the collections inside this database, we simply usedatabase.list_collection_names() method:

# list all collectionsprint("Available collections:", database.list_collection_names())

Here is my output:

[]

Not very surprising, since this is a new database, we haven't created any collection yet.You can also usedatabase.list_collections() method to get each collection along with some information.

Inserting Documents

A document in MongoDB is like a row in relational databases. For demonstration, let's make books collection and insert books in it:

# get books collection (or create one)books = database["books"]# insert a single bookresult = books.insert_one({    "name": "Invent Your Own Computer Games with Python, 4E",    "author": "Al Sweigart",    "price": 17.99,    "url": "https://amzn.to/2zxN2W2"})

We have retrieved books collection (or create it if it doesn't exist) and usedbooks.insert_one() method to insert a single document, we simply passed a Python dictionary.

Let's get the ID of the inserted element:

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

Output:

One book inserted: 5ee79b10c6bd9552e204a625

A great feature about MongoDB, it automatically generates a unique ID for each element inserted, we'll see it again later on.

Now what if we want to insert multiple documents in a time ? We simply useinsert_many() method instead ofinsert_one():

# insert many booksbooks_data = [    {        "name": "Automate the Boring Stuff with Python: Practical Programming for Total Beginners",        "author": "Al Sweigart",        "price": 17.76,        "url": "https://amzn.to/2YAncdY"    },    {        "name": "Python Crash Course: A Hands-On, Project-Based Introduction to Programming",        "author": "Eric Matthes",        "price": 22.97,        "url": "https://amzn.to/2yQfQZl"    },    {        "name": "MySQL for Python",        "author": "Albert Lukaszewski",        "price": 49.99,    }]result = books.insert_many(books_data)print("Many books inserted, Ids:", result.inserted_ids)

We have inserted 3 documents in one go, notice the last book doesn't have aurl field and MongoDB doesn't complain about it, because it's allowed!

Here is my output:

Many books inserted, Ids: [ObjectId('5ee79c4fc6bd9552e204a62c'), ObjectId('5ee79c4fc6bd9552e204a62d'), ObjectId('5ee79c4fc6bd9552e204a62e')]

Fetching Documents

The amazing thing about MongoDB is that we can filter documents using a Python dictionary, let's get a single book by a specific author:

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

Output:

{'_id': ObjectId('5ee79c10c6bd9552e204a627'), 'author': 'Eric Matthes', 'name': 'Python Crash Course: A Hands-On, Project-Based Introduction to '         'Programming', 'price': 22.97, 'url': 'https://amzn.to/2yQfQZl'}

The output is a Python dictionary as well, let's get all books of Al Sweigart:

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

Notice I wrapped the result in alist() function to retrieve it as a list of dictionaries, that's becausefind() method returns apymongo.cursor.Cursor() instance, in which you can iterate over it using a for loop, or wrap it usinglist() function. Here is the expected output:

[{'_id': ObjectId('5ee79b10c6bd9552e204a625'),  'author': 'Al Sweigart',  'name': 'Invent Your Own Computer Games with Python, 4E',  'price': 17.99,  'url': 'https://amzn.to/2zxN2W2'}, {'_id': ObjectId('5ee79c10c6bd9552e204a626'),  'author': 'Al Sweigart',  'name': 'Automate the Boring Stuff with Python: Practical Programming for '          'Total Beginners',  'price': 17.76,  'url': 'https://amzn.to/2YAncdY'}, {'_id': ObjectId('5ee79c34c6bd9552e204a629'),  'author': 'Al Sweigart',  'name': 'Automate the Boring Stuff with Python: Practical Programming for '          'Total Beginners',  'price': 17.76,  'url': 'https://amzn.to/2YAncdY'}, {'_id': ObjectId('5ee79c4fc6bd9552e204a62c'),  'author': 'Al Sweigart',  'name': 'Automate the Boring Stuff with Python: Practical Programming for '          'Total Beginners',  'price': 17.76,  'url': 'https://amzn.to/2YAncdY'}]

Finally, if you want to get all the documents without any filter, you can simply pass an empty dictionary tofind() method:

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

Deleting a Document

To delete a specific document, you simply usedelete_one() method, here is an example:

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

Even if there is more than one document using that filter, it will still delete a single document. If you want to delete all the documents using that filter, you usedelete_many() method:

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

Deleting a Collection

To delete a collection in MongoDB, you have two options:

# drop this collectiondatabase.drop_collection("books")# or this:# books.drop()

This will deletebooks collection.

Deleting a Database

# drop this entire databaseclient.drop_database("python")# close the connectionclient.close()

drop_database() does what its name suggests, it deletes the database given the name in the arguments.

Finally, we close the connection usingclose() method.

Conclusion

Now you know the basic functionalities of MongoDB and its Python driver,pymongo. I encourage you to play around with it to get familiar with the database, it's as flexible as Python language!

Related: How to Use MySQL Database in Python.

Happy Coding ♥

Why juggle between languages when you can convert? Check out ourCode Converter. Try it out today!

View Full Code Understand My Code
Sharing is caring!



Read Also


How to Use MySQL Database in Python
How to Work with JSON Files in Python
How to Connect to a Remote MySQL Database in Python

Comment panel

    Got a coding query or need some guidance before you comment? Check out thisPython Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!





    Ethical Hacking with Python EBook - Topic - Top


    Join 50,000+ Python Programmers & Enthusiasts like you!



    Tags


    New Tutorials

    Popular Tutorials


    Ethical Hacking with Python EBook - Topic - Bottom

    CodingFleet - Topic - Bottom






    Claim your Free Chapter!

    Download a Completely Free Ethical hacking with Python from Scratch Chapter.

    See how the book can help you build awesome hacking tools with Python!



    [8]ページ先頭

    ©2009-2025 Movatter.jp