|
| 1 | ++++ |
| 2 | +date = "2017-03-13T12:12:50-05:00" |
| 3 | +title = "Connect to MongoDB via the Python Driver" |
| 4 | +tags = ["beginner", "mongodb", "python"] |
| 5 | ++++ |
| 6 | + |
| 7 | +# Connect to MongoDB via the Python Driver |
| 8 | + |
| 9 | +## Introduction |
| 10 | + |
| 11 | +[PyMongo](http://api.mongodb.com/python/current/index.html) is a Python |
| 12 | +distribution containing tools for working with MongoDB. This tutorial |
| 13 | +demonstrates how to connect to MongoDB using the Pymongo driver. |
| 14 | + |
| 15 | +## Prerequisites |
| 16 | +* A running MongoDB instance. The instance can be running locally if |
| 17 | +MongoDB is [installed](https://docs.mongodb.com/manual/installation/#tutorials) |
| 18 | +or running on a remote host where both the **hostname** and **port** are |
| 19 | +known. |
| 20 | +* Pymongo driver is [installed](http://api.mongodb.com/python/current/installation.html). |
| 21 | + |
| 22 | +## Local MongoDB Instance on Default Port |
| 23 | + |
| 24 | +Pymongo uses the [MongoClient](http://api.mongodb.com/python/current/api/pymongo/mongo_client.html#pymongo.mongo_client.MongoClient) |
| 25 | +for connecting to a MongoDB instance, replica set, or |
| 26 | +sharded cluster. |
| 27 | + |
| 28 | +If your MongoDB instance is running on the default host (**localhost**) |
| 29 | +and port (**27017**), you can instantiate a `MongoClient` without |
| 30 | +specifying any parameters: |
| 31 | + |
| 32 | +~~~python |
| 33 | +import pymongo |
| 34 | +from pymongo import MongoClient |
| 35 | + |
| 36 | +client = MongoClient() |
| 37 | + |
| 38 | +# Get the sampleDB database |
| 39 | +db = client.sampleDB |
| 40 | +~~~ |
| 41 | + |
| 42 | +## Connect Using Host and Port Parameters |
| 43 | + |
| 44 | +The **host** and **port** can be specified explicitly by passing their |
| 45 | +respective values as parameters to the `MongoClient`. The following |
| 46 | +operation connects to a MongoDB instance running on a remote host using |
| 47 | +the **host** and **port** parameters: |
| 48 | + |
| 49 | +~~~python |
| 50 | +import pymongo |
| 51 | +from pymongo import MongoClient |
| 52 | + |
| 53 | +client = MongoClient('mongodb0.tutorials.com', 27017) |
| 54 | + |
| 55 | +# Get the sampleDB database |
| 56 | +db = client.sampleDB |
| 57 | +~~~ |
| 58 | + |
| 59 | +The default **host** and **port** can also be specified explicitly: |
| 60 | + |
| 61 | +~~~python |
| 62 | +import pymongo |
| 63 | +from pymongo import MongoClient |
| 64 | + |
| 65 | +client = MongoClient('localhost', 27017) |
| 66 | + |
| 67 | +# Get the sampleDB database |
| 68 | +db = client.sampleDB |
| 69 | +~~~ |
| 70 | + |
| 71 | +## Connect Using a URI |
| 72 | + |
| 73 | +Pymongo also accepts a |
| 74 | +[URI](https://docs.mongodb.com/manual/reference/connection-string/) |
| 75 | +to connect to MongoDB: |
| 76 | + |
| 77 | +~~~python |
| 78 | +import pymongo |
| 79 | +from pymongo import MongoClient |
| 80 | + |
| 81 | +client = MongoClient('mongodb://localhost:27017') |
| 82 | + |
| 83 | +# Get the sampleDB database |
| 84 | +db = client.sampleDB |
| 85 | +~~~ |
| 86 | + |
| 87 | +## Connect with Authentication Credentials |
| 88 | + |
| 89 | +To connect to a MongoDB instance with authentication enabled, specify a |
| 90 | +[URI](https://docs.mongodb.com/manual/reference/connection-string/) |
| 91 | +in the following format: |
| 92 | + |
| 93 | +~~~sh |
| 94 | +mongodb://[username:password@]host1[:port1] |
| 95 | +~~~ |
| 96 | + |
| 97 | +The following operation connects the user **alice** with password |
| 98 | +**abc123** to a MongoDB instance on the default host and port: |
| 99 | + |
| 100 | +~~~python |
| 101 | +import pymongo |
| 102 | +from pymongo import MongoClient |
| 103 | + |
| 104 | +client = MongoClient('mongodb://alice:abc123@localhost:27017') |
| 105 | + |
| 106 | +# Get the sampleDB database |
| 107 | +db = client.sampleDB |
| 108 | +~~~ |
| 109 | + |
| 110 | +## References |
| 111 | +* [PyMongo API Documentation](https://api.mongodb.com/python/current/) |
0 commit comments