Skip to content

Commit 541f518

Browse files
committed
Update tutorial to use MongoClient PYTHON-423
1 parent 034dba7 commit 541f518

File tree

1 file changed

+7
-38
lines changed

1 file changed

+7
-38
lines changed

doc/tutorial.rst

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ Tutorial
33

44
.. testsetup::
55

6-
from pymongo import Connection
7-
connection = Connection()
6+
from pymongo import MongoClient
7+
connection = MongoClient()
88
connection.drop_database('test-database')
99

1010
This tutorial is intended as an introduction to working with
@@ -32,27 +32,27 @@ can start it like so:
3232
Making a Connection
3333
-------------------
3434
The first step when working with **PyMongo** is to create a
35-
:class:`~pymongo.connection.Connection` to the running **mongod**
35+
:class:`~pymongo.mongo_client.MongoClient` to the running **mongod**
3636
instance. Doing so is easy:
3737

3838
.. doctest::
3939

40-
>>> from pymongo import Connection
41-
>>> connection = Connection()
40+
>>> from pymongo import MongoClient
41+
>>> connection = MongoClient()
4242

4343
The above code will connect on the default host and port. We can also
4444
specify the host and port explicitly, as follows:
4545

4646
.. doctest::
4747

48-
>>> connection = Connection('localhost', 27017)
48+
>>> connection = MongoClient('localhost', 27017)
4949

5050
Getting a Database
5151
------------------
5252
A single instance of MongoDB can support multiple independent
5353
`databases <http://www.mongodb.org/display/DOCS/Databases>`_. When
5454
working with PyMongo you access databases using attribute style access
55-
on :class:`~pymongo.connection.Connection` instances:
55+
on :class:`~pymongo.mongo_client.MongoClient` instances:
5656

5757
.. doctest::
5858

@@ -143,37 +143,6 @@ of the collections in our database:
143143
.. note:: The *system.indexes* collection is a special internal
144144
collection that was created automatically.
145145

146-
Ensuring Your Insert Succeeded
147-
------------------------------
148-
149-
PyMongo’s default behavior for :meth:`~pymongo.collection.Collection.insert`,
150-
:meth:`~pymongo.collection.Collection.update`,
151-
:meth:`~pymongo.collection.Collection.save`,
152-
and :meth:`~pymongo.collection.Collection.remove` is to perform unacknowledged
153-
writes: the driver does not request a response or wait for an acknowledgement
154-
that the operation was successful unless the method is passed safe=True or
155-
another `getLastError <http://www.mongodb.org/display/DOCS/getLastError+Command>`_
156-
option. For example, if two documents with the same ``_id`` are inserted:
157-
158-
.. doctest::
159-
160-
>>> db.posts.insert({'_id': 1})
161-
1
162-
>>> db.posts.insert({'_id': 1})
163-
1
164-
165-
Both inserts appear to succeed, but the second failed on the server.
166-
To see why, we need to pass safe=True
167-
to :meth:`~pymongo.collection.Collection.insert`::
168-
169-
>>> db.posts.insert({'_id': 1}, safe=True)
170-
Traceback (most recent call last):
171-
pymongo.errors.DuplicateKeyError: E11000 duplicate key error index: test-database.posts.$_id_ dup key: { : 1 }
172-
173-
Applications should generally set a default of safe=True when they first create a Connection::
174-
175-
>>> connection = Connection('localhost', 27017, safe=True)
176-
177146
Getting a Single Document With :meth:`~pymongo.collection.Collection.find_one`
178147
------------------------------------------------------------------------------
179148
The most basic type of query that can be performed in MongoDB is

0 commit comments

Comments
 (0)