|
3 | 3 |
|
4 | 4 | .. testsetup:: |
5 | 5 |
|
6 | | - from pymongo import Connection |
7 | | - connection = Connection() |
| 6 | + from pymongo import MongoClient |
| 7 | + connection = MongoClient() |
8 | 8 | connection.drop_database('test-database') |
9 | 9 |
|
10 | 10 | This tutorial is intended as an introduction to working with |
@@ -32,27 +32,27 @@ can start it like so: |
32 | 32 | Making a Connection |
33 | 33 | ------------------- |
34 | 34 | 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** |
36 | 36 | instance. Doing so is easy: |
37 | 37 |
|
38 | 38 | .. doctest:: |
39 | 39 |
|
40 | | - >>> from pymongo import Connection |
41 | | - >>> connection = Connection() |
| 40 | + >>> from pymongo import MongoClient |
| 41 | + >>> connection = MongoClient() |
42 | 42 |
|
43 | 43 | The above code will connect on the default host and port. We can also |
44 | 44 | specify the host and port explicitly, as follows: |
45 | 45 |
|
46 | 46 | .. doctest:: |
47 | 47 |
|
48 | | - >>> connection = Connection('localhost', 27017) |
| 48 | + >>> connection = MongoClient('localhost', 27017) |
49 | 49 |
|
50 | 50 | Getting a Database |
51 | 51 | ------------------ |
52 | 52 | A single instance of MongoDB can support multiple independent |
53 | 53 | `databases <http://www.mongodb.org/display/DOCS/Databases>`_. When |
54 | 54 | 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: |
56 | 56 |
|
57 | 57 | .. doctest:: |
58 | 58 |
|
@@ -143,37 +143,6 @@ of the collections in our database: |
143 | 143 | .. note:: The *system.indexes* collection is a special internal |
144 | 144 | collection that was created automatically. |
145 | 145 |
|
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 | | - |
177 | 146 | Getting a Single Document With :meth:`~pymongo.collection.Collection.find_one` |
178 | 147 | ------------------------------------------------------------------------------ |
179 | 148 | The most basic type of query that can be performed in MongoDB is |
|
0 commit comments