This article was originally published on MongoDB. Thank you for supporting the partners who make SitePoint possible.
In this article, we’ll introduce developers to programming MongoDB using the Python programming language. PyMongo is the name of the client library (in MongoDB speak we refer to it as a “driver”) we use to interact with the MongoDB Server.
To get started we need to install the toolchain used by a typical MongoDB Python developer.
Key Takeaways
- PyMongo is the Python client library used to interact with MongoDB Server, and can be installed using the pip Python package manager. It is recommended to use a virtual environment to isolate your PyMongo code.
- The tool ‘m’ is used to manage and use multiple installations of MongoDB Server in parallel, and can be installed using npm, the Node.js package manager. It is particularly useful for trying out beta versions while continuing development on a stable release.
- Once MongoDB and PyMongo are installed, a connection between the client and server can be established using MongoClient. This allows developers to issue commands to the server and interact with the database using Python.
Installing m
First up is m. Hard to find online unless your search for “MongoDB m”, m is a tool to manage and use multiple installations of the MongoDB Server in parallel. It is an invaluable tool if you want to try out the latest and greatest beta version but still continue mainline development on our current stable release.
The easiest way to install m is with npm the Node.js package manager (which it turns out is not just for Node.js).
$ sudo npm install -g m
Password:******
/usr/local/bin/m -> /usr/local/lib/node_modules/m/bin/m
+ m@1.4.1
updated 1 package in 2.361s
$
If you can’t or don’t want to use npm, you can download and install directly from the GitHub repo. See the README there for details.
For today we will use m to install the current stable production version (4.0.2 at the time of writing).
We run the stable command to achieve this.
$ m stable
MongoDB version 4.0.2 is not installed.
Installation may take a while. Would you like to proceed? [y/n] y
... installing binary
######################################################################## 100.0%
/Users/jdrumgoole
... removing source
... installation complete
$
If you need to use the path directly in another program you can get that with m bin.
$ m bin 4.0.0
/usr/local/m/versions/4.0.1/bin
$
To run the corresponding binary do m use stable:
$ m use stable
2018-08-28T11:41:48.157+0100 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2018-08-28T11:41:48.171+0100 I CONTROL [initandlisten] MongoDB starting : pid=38524 port=27017 dbpath=/data/db 64-bit host=JD10Gen.local
2018-08-28T11:41:48.171+0100 I CONTROL [initandlisten] db version v4.0.2
2018-08-28T11:41:48.171+0100 I CONTROL [initandlisten] git version: fc1573ba18aee42f97a3bb13b67af7d837826b47
< other server output >
...
2018-06-13T15:52:43.648+0100 I NETWORK [initandlisten] waiting for connections on port 27017
Now that we have a server running we can confirm that it works by connecting via the mongo shell.
$ mongo
MongoDB shell version v4.0.0
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.0
Server has startup warnings:
2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten]
2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten]
2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten] ** Start the server with --bind_ip < address> to specify which IP
2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
2018-07-06T10:56:50.973+0100 I CONTROL [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service to collect and display
metrics about your deployment (disk utilization, CPU, operation statistics,
etc).
The monitoring data will be available on a MongoDB website with a unique
URL created for you. Anyone you share the URL with will also be able to
view this page. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command:
db.enableFreeMonitoring()
---
>
These warnings are standard. They flag that this database has no access controls setup by default and, that it is only listening to connections coming from the machine it is running on (localhost). We will learn how to setup access control and listen on a broader range of ports in later episodes.
Installing the PyMongo Driver
But this series is not about the MongoDB Shell, which uses JavaScript as its coin of the realm, it’s about Python. How do we connect to the database with Python?
First we need to install the MongoDB Python Driver, PyMongo. In MongoDB parlance a driver is a language-specific client library that allows developers to interact with the server in the idiom of their own programming language.
For Python that means installing the driver with pip
. In node.js the driver is installed using npm
and in Java you can use maven
.
$ pip3 install pymongo
Collecting pymongo
Downloading https://files.pythonhosted.org/packages/a1/e0/51df08036e04c1ddc985a2dceb008f2f21fc1d6de711bb6cee85785c1d78/pymongo-3.7.1-cp27-cp27m-macosx_10_13_intel.whl (333kB)
100% |████████████████████████████████| 337kB 4.1MB/s
Installing collected packages: pymongo
Successfully installed pymongo-3.7.1
$
We recommend you use a virtual environment to isolate your PyMongo Monday code. This is not required but is very convenient for isolating different development streams.
Now we can connect to the database:
$ python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymongo (1)
>>> client = pymongo.MongoClient(host="mongodb://localhost:8000") (2)
>>> result = client.admin.command("isMaster") (3)
>>> import pprint
>>> pprint.pprint(result)
{'ismaster': True,
'localTime': datetime.datetime(2018, 6, 13, 21, 55, 2, 272000),
'logicalSessionTimeoutMinutes': 30,
'maxBsonObjectSize': 16777216,
'maxMessageSizeBytes': 48000000,
'maxWireVersion': 6,
'maxWriteBatchSize': 100000,
'minWireVersion': 0,
'ok': 1.0,
'readOnly': False}
>>>
First we import the PyMongo library (1). Then we create a local client
object (2) that holds the connection pool and other status for this server. We generally don’t want more than one MongoClient
object per program as it provides its own connection pool.
Now we are ready to issue a command to the server. In this case it’s the standard MongoDB server information command which is called rather anachronistically isMaster
(3). This is a hangover from the very early versions of MongoDB. It appears in pre 1.0 versions of MongoDB (which is over ten years old at this stage). The isMaster
command returns a dict
which details a bunch of server information. In order to format this in a more readable way we import the pprint
library.
Conclusion
We have installed MongoDB, installed the Python client library (aka driver), started a mongod
server and established a connection between the client and server.
Next week we will introduce CRUD operations on MongoDB, starting with Create.
For direct feedback please pose your questions on twitter/jdrumgoole. That way everyone can see the answers.
The best way to try out MongoDB is via MongoDB Atlas our fully managed Database as a Service available on AWS, Google Cloud Platform (CGP) and Azure.
Frequently Asked Questions (FAQs) about Setting Up Your PyMongo Environment
What are the prerequisites for setting up a PyMongo environment?
Before setting up a PyMongo environment, you need to have Python and MongoDB installed on your system. Python 2.7 or Python 3.4+ versions are recommended. For MongoDB, any version from 2.6 to 4.4 is compatible with PyMongo. Also, ensure that you have pip installed, which is a package manager for Python. It will help you install PyMongo easily.
How do I install PyMongo?
PyMongo can be installed using pip, the Python package installer. Open your terminal or command prompt and type the command “pip install pymongo”. This command will download and install the latest version of PyMongo. If you want to install a specific version, you can specify it like this: “pip install pymongo==version”.
How do I connect to MongoDB using PyMongo?
To connect to MongoDB using PyMongo, you need to import MongoClient from pymongo. Then, create a MongoClient instance and specify the host and port where your MongoDB server is running. The default host is localhost and the default port is 27017. Here is a sample code:from pymongo import MongoClient
client = MongoClient("localhost", 27017)
How do I create a database in MongoDB using PyMongo?
To create a database in MongoDB using PyMongo, you can use the MongoClient instance you created. Here is a sample code:db = client.mydatabase
This code will create a database named “mydatabase”. If the database does not exist, MongoDB will create it for you.
How do I insert data into MongoDB using PyMongo?
To insert data into MongoDB using PyMongo, you can use the insert_one() or insert_many() methods. Here is a sample code:mycollection = db.mycollection
post = {"author": "John", "text": "My first blog post!"}
post_id = mycollection.insert_one(post).inserted_id
This code will insert a document into the “mycollection” collection.
How do I query data from MongoDB using PyMongo?
To query data from MongoDB using PyMongo, you can use the find_one() or find() methods. Here is a sample code:mycollection = db.mycollection
mycollection.find_one({"author": "John"})
This code will find the first document where the author is “John”.
How do I update data in MongoDB using PyMongo?
To update data in MongoDB using PyMongo, you can use the update_one() or update_many() methods. Here is a sample code:mycollection = db.mycollection
mycollection.update_one({"author": "John"}, {"$set": {"text": "My updated blog post!"}})
This code will update the text of the first document where the author is “John”.
How do I delete data from MongoDB using PyMongo?
To delete data from MongoDB using PyMongo, you can use the delete_one() or delete_many() methods. Here is a sample code:mycollection = db.mycollection
mycollection.delete_one({"author": "John"})
This code will delete the first document where the author is “John”.
How do I handle errors in PyMongo?
PyMongo provides several exceptions that you can catch and handle. For example, the ConnectionFailure exception is raised when the client cannot connect to the server. Here is a sample code:from pymongo import MongoClient, errors
try:
client = MongoClient("localhost", 27017)
except errors.ConnectionFailure:
print("Could not connect to server")
How do I close the connection to MongoDB using PyMongo?
To close the connection to MongoDB using PyMongo, you can use the close() method of the MongoClient instance. Here is a sample code:client = MongoClient("localhost", 27017)
client.close()
This code will close the connection to the server.
Joe is the Director of Developer Advocacy, EMEA at MongoDB.