Control MongoDB Using Python

It's possible to create server logic within python directly interecting with MongoDB servers. First, a driver is needed. In python this MongoDB driver package is known as pymongo. Install it using pip like this:

pip3 install pymongo

Creating a Database Using MongoDB

To create a database in MongoDB start by creating a Python file. Then add an import statement for pymongo, instantiate a MongoClient class and select a database using that client.

import pymongo
myclient = pymongo.MongoClient('mongodb://localhost:27017')
mydb = myclient['mydatabase']

Check if Database Exists

print(myclient.list_database_names())

In MongoDB tables are called collections. To create a collection in MongoDB, use the database object created previously and specify the name of the collection to create.

The code below shows how to create a collection named customers in the mydatabase database defined above.

import pymongo
myclient = pymongo.MongoClient('mongodb://localhost:27017')
mydb = myclient['mydatabase']
mycol = mydb["customers"]

You can check whether a collection exists in a database by listing all collections. To do so add the following line to your python script.

print(mydb.list_collection_names())

In MongoDB, records are called documents. To insert a document into a collection, you can use the insert_one() method.

The first parameter of the insert_one() method is a dictionary containing the names and values of each field in the document that you want to insert. See the example below:

import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
mydict = {"name": "John", "address": "Highway 37"}
x = mycol.insert_one(mydict)

To insert multiple documents, you will need to use the insert_many() method.

import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
mylist = [
{"name": "Amy", "address": "Apple st 652"},
{"name": "Hannah", "address": "Mountain 21"},
{"name": "Michael", "address": "Valley 345"},
]
x = mycol.insert_many(mylist)
print(x.inserted_ids)

References

MongoDB is a powerful tool that you can use to create databases using Python code. For more examples visit the PyMongo Tutorial Page. For the official documentation, visit PyMongo API Reference Page