This article was originally published on the MongoDB website. Thanks to our partners who support SitePoint!
This article will guide developers how to operate MongoDB databases using the Python programming language. PyMongo is a client library for interacting with MongoDB servers (which we call "drivers" in MongoDB).
First of all, we need to install the toolchain that typical MongoDB Python developers use.
First of all, m. Unless your search is "MongoDB m", it's hard to find it online, a tool for parallel management and installations using multiple MongoDB servers. If you want to try the latest and best beta versions but still want to continue to do mainline development on the current stable version, this is a very valuable tool.
The easiest way to install m is to use npm, i.e. Node.js package manager (it turns out to be more than just 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 it directly from the GitHub repository. Please refer to README there for more details.
Today we will use m to install the currently 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 use m bin
to get it.
$ m bin 4.0.0 /usr/local/m/versions/4.0.1/bin $
To run the corresponding binary file, use 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 ... 2018-06-13T15:52:43.648+0100 I NETWORK [initandlisten] waiting for connections on port 27017
Now we have a running server, we can confirm that it works properly through the mongo shell connection.
$ 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 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 indicate that by default this database has no access control set and that it only listens for connections from the machine it is running (localhost). We will learn how to set up access control and listen for a wider range of ports in future articles.
But this series is not about MongoDB Shell (which uses JavaScript as its main language), but about Python. How do we connect to a database using Python?
First of all, we need to install the MongoDB Python driver PyMongo. In MongoDB terminology, a driver is a language-specific client library that allows developers to interact with the server using idioms of their own programming language.
For Python, this means using pip to install the driver. In node.js, the driver is installed using npm, and maven can be used in Java.
$ 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 $
We recommend that you use a virtual environment to isolate your PyMongo code. This is not required, but is very convenient for isolating different development streams.
Now we can connect to the database:
$ 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 $
First, we import the PyMongo library (1). Then, we create a local client object (2) which saves the connection pool and other states of this server. We usually don't want multiple MongoClient objects per program because it provides its own connection pool.
Now we can issue commands to the server. In this case, it is a standard MongoDB server information command, quite outdatedly called isMaster (3). This is left over from earlier versions of MongoDB. It appears in MongoDB versions before 1.0 (it has been around for more than a decade now). The isMaster command returns a dictionary that details a lot of server information. To format it in a more readable way, we imported the pprint library.
We have installed MongoDB, installed the Python client library (aka driver), started a mongod server, and established a connection between the client and the server.
Next week we will introduce CRUD operations on MongoDB, starting with creation.
For direct feedback, please ask your question on twitter/jdrumgoole. This way everyone can see the answer.
The best way to try MongoDB is through MongoDB Atlas, which is a fully managed database-as-a-service we offer on AWS, Google Cloud Platform (CGP), and Azure.
(The FAQs section is omitted as it's largely unrelated to the main article's content and would significantly increase the output length. It's also a standard set of instructions easily found elsewhere.)
The above is the detailed content of Setting up Your PyMongo Environment. For more information, please follow other related articles on the PHP Chinese website!