Skip to content

Commit 24d34ce

Browse files
jdestefano-mongoi80and
authored andcommitted
DOCSP-429 - Tutorials for connecting to MongoDB via shell and python. (#3)
1 parent 5f3fa8f commit 24d34ce

File tree

2 files changed

+196
-0
lines changed

2 files changed

+196
-0
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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/)
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
+++
2+
date = "2017-03-13T12:12:50-05:00"
3+
title = "Connect to MongoDB via Mongo Shell"
4+
tags = ["beginner", "mongodb", "shell"]
5+
+++
6+
7+
# Connect to MongoDB via Mongo Shell
8+
9+
## Introduction
10+
11+
The [mongo](https://docs.mongodb.com/manual/reference/program/mongo/)
12+
shell is an interactive JavaScript interface to MongoDB. This tutorial
13+
demonstrates how to connect to MongoDB via the **mongo** shell. Once
14+
connected to MongoDB, you can use the **mongo** shell to query and
15+
update data as well as perform administrative operations.
16+
17+
## Prerequisites
18+
19+
* MongoDB is [installed](https://docs.mongodb.com/manual/installation/#tutorials).
20+
The **mongo** shell is a component of the
21+
[MongoDB distributions](http://www.mongodb.org/downloads>).
22+
* A running MongoDB instance. The instance can be running locally or
23+
on a remote host where both the **hostname** and **port** are known.
24+
25+
## Local MongoDB Instance on Default Port
26+
27+
To connect to a MongoDB instance running on **localhost** with **default
28+
port 27017**:
29+
30+
1. At a prompt in a terminal window (or a command prompt for Windows),
31+
go to your `<mongodb installation dir>`:
32+
33+
~~~sh
34+
cd <mongodb installation dir>
35+
~~~
36+
37+
2. Type **./bin/mongo** to start **mongo**:
38+
39+
~~~sh
40+
./bin/mongo
41+
~~~
42+
43+
Alternatively, if you added the `<mongodb installation dir>/bin` to
44+
your `PATH` you can run `mongo` instead of `./bin/mongo`:
45+
46+
~~~sh
47+
mongo
48+
~~~
49+
50+
## Local MongoDB Instance on a Non-default Port
51+
52+
Use the `--port <port>` command-line option to connect to a MongoDB instance
53+
running on **localhost** with a **non-default port 28015**:
54+
55+
56+
~~~sh
57+
mongo --port 28015
58+
~~~
59+
60+
## MongoDB Instance on a Remote Host
61+
62+
Use the `--host <host>:<port>` command-line option to connect to a MongoDB instance
63+
running on a remote host machine:
64+
65+
~~~sh
66+
mongo --host mongodb0.tutorials.com:28015
67+
~~~
68+
69+
Alternatively, pass the **host** and **port** parameters separately:
70+
71+
~~~sh
72+
mongo --host mongodb0.tutorials.com --port 28015
73+
~~~
74+
75+
## MongoDB Instance using Authentication
76+
77+
Use the `--username <user>` and `--password <pass>` command-line options to
78+
connect to a MongoDB instance that is using authentication:
79+
80+
~~~sh
81+
mongo --username alice --password abc123 --host mongodb0.tutorials.com --port 28015
82+
~~~
83+
84+
## References
85+
* [Mongo Shell Documentation](https://docs.mongodb.com/manual/reference/program/mongo/)

0 commit comments

Comments
 (0)