Skip to content

Commit 70dd173

Browse files
behackettkay-kim
authored andcommitted
Update python code samples for PyMongo 3.0.
Signed-off-by: kay <[email protected]>
1 parent ac551f0 commit 70dd173

18 files changed

+187
-141
lines changed

primer/source/includes/examples-aggregation.yaml

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,9 @@ operation:
6161
- language: python
6262
code: |
6363
cursor = db.restaurants.aggregate(
64-
[
65-
{ "$group": { "_id": "$borough", "count": { "$sum": 1 } } }
66-
],
67-
cursor={}
64+
[
65+
{"$group": {"_id": "$borough", "count": {"$sum": 1}}}
66+
]
6867
)
6968
- pre: "Iterate the cursor and print the matching documents."
7069
language: python
@@ -88,11 +87,10 @@ operation:
8887
- language: python
8988
code: |
9089
cursor = db.restaurants.aggregate(
91-
[
92-
{ "$match": { "borough": "Queens", "cuisine": "Brazilian" } },
93-
{ "$group": { "_id": "$address.zipcode" , "count": { "$sum": 1 } } }
94-
],
95-
cursor={}
90+
[
91+
{"$match": {"borough": "Queens", "cuisine": "Brazilian"}},
92+
{"$group": {"_id": "$address.zipcode", "count": {"$sum": 1}}}
93+
]
9694
)
9795
- pre: |
9896
Iterate the cursor and print the matching documents.

primer/source/includes/examples-index.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ operation:
6767
language: python
6868
code: |
6969
import pymongo
70-
db.restaurants.create_index( [ ( "cuisine", pymongo.ASCENDING ) ] )
70+
db.restaurants.create_index([("cuisine", pymongo.ASCENDING)])
7171
results: |
7272
"u'cuisine_1'"
7373
post: |
@@ -82,10 +82,10 @@ operation:
8282
language: python
8383
code: |
8484
import pymongo
85-
db.restaurants.create_index( [
86-
( "cuisine", pymongo.ASCENDING ),
87-
( "address.zipcode", pymongo.DESCENDING )
88-
] )
85+
db.restaurants.create_index([
86+
("cuisine", pymongo.ASCENDING),
87+
("address.zipcode", pymongo.DESCENDING)
88+
])
8989
results: |
9090
"u'cuisine_1_address.zipcode_-1'"
9191
post: |
@@ -203,4 +203,4 @@ operation:
203203
language: java
204204

205205
- post: ".. include:: includes/example-java-create-compound-index-post.txt"
206-
...
206+
...

primer/source/includes/examples-insert.yaml

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -61,41 +61,42 @@ edition: python
6161
operation:
6262
language: python
6363
code: |
64-
from datetime import *;
65-
db.restaurants.insert(
66-
{
67-
"address" : {
68-
"street" : "2 Avenue",
69-
"zipcode" : "10075",
70-
"building" : "1480",
71-
"coord" : [ -73.9557413, 40.7720266 ],
72-
},
73-
"borough" : "Manhattan",
74-
"cuisine" : "Italian",
75-
"grades" : [
76-
{
77-
"date" : datetime.strptime("2014-10-01", "%Y-%m-%d"),
78-
"grade" : "A",
79-
"score" : 11
80-
},
81-
{
82-
"date" : datetime.strptime("2014-01-16", "%Y-%m-%d"),
83-
"grade" : "B",
84-
"score" : 17
85-
}
86-
],
87-
"name" : "Vella",
88-
"restaurant_id" : "41704620"
89-
}
64+
from datetime import datetime
65+
result = db.restaurants.insert_one(
66+
{
67+
"address": {
68+
"street": "2 Avenue",
69+
"zipcode": "10075",
70+
"building": "1480",
71+
"coord": [-73.9557413, 40.7720266]
72+
},
73+
"borough": "Manhattan",
74+
"cuisine": "Italian",
75+
"grades": [
76+
{
77+
"date": datetime.strptime("2014-10-01", "%Y-%m-%d"),
78+
"grade": "A",
79+
"score": 11
80+
},
81+
{
82+
"date": datetime.strptime("2014-01-16", "%Y-%m-%d"),
83+
"grade": "B",
84+
"score": 17
85+
}
86+
],
87+
"name": "Vella",
88+
"restaurant_id": "41704620"
89+
}
9090
)
9191
post: |
92-
The operation returns the ``_id`` field value for the inserted
93-
document.
92+
The operation returns an :py:class:`~pymongo.results.InsertOneResult` object,
93+
which includes the ``_id`` of the inserted document.
9494
results: |
95+
result.inserted_id
9596
ObjectId("54c1478ec2341ddf130f62b7")
9697
replacement:
97-
insertMethod: :py:meth:`~pymongo.collection.Collection.insert()`
98-
clientName: :py:mod:`pymongo`
98+
insertMethod: :py:meth:`~pymongo.collection.Collection.insert_one`
99+
clientName: :py:class:`~pymongo.mongo_client.MongoClient`
99100
---
100101
source:
101102
file: examples-insert-base.yaml

primer/source/includes/examples-query-combination.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ edition: python
4040
operation:
4141
- language: python
4242
code: |
43-
cursor = db.restaurants.find( { "cuisine": "Italian", "address.zipcode": "10075" } )
43+
cursor = db.restaurants.find({"cuisine": "Italian", "address.zipcode": "10075"})
4444
- pre: |
4545
Iterate the cursor and print the matching documents.
4646
language: python
@@ -56,7 +56,8 @@ edition: python
5656
operation:
5757
- language: python
5858
code: |
59-
cursor = db.restaurants.find( { "$or": [ { "cuisine": "Italian" }, { "address.zipcode": "10075" } ] } )
59+
cursor = db.restaurants.find(
60+
{"$or": [{"cuisine": "Italian"}, {"address.zipcode": "10075"}]})
6061
- pre: |
6162
Iterate the cursor and print the matching documents.
6263
language: python

primer/source/includes/examples-query-equality.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ edition: python
4848
operation:
4949
- language: python
5050
code: |
51-
cursor = db.restaurants.find( { "borough": "Manhattan" } )
51+
cursor = db.restaurants.find({"borough": "Manhattan"})
5252
- pre: |
5353
Iterate the cursor and print the matching documents.
5454
language: python
@@ -64,7 +64,7 @@ edition: python
6464
operation:
6565
- language: python
6666
code: |
67-
cursor = db.restaurants.find( { "address.zipcode": "10075" } )
67+
cursor = db.restaurants.find({"address.zipcode": "10075"})
6868
- pre: |
6969
Iterate the cursor and print the matching documents.
7070
language: python
@@ -80,7 +80,7 @@ edition: python
8080
operation:
8181
- language: python
8282
code: |
83-
cursor = db.restaurants.find( { "grades.grade": "B" } )
83+
cursor = db.restaurants.find({"grades.grade": "B"})
8484
- pre: |
8585
Iterate the cursor and print the matching documents.
8686
language: python

primer/source/includes/examples-query-operators.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ edition: python
3838
operation:
3939
- language: python
4040
code: |
41-
cursor = db.restaurants.find( { "grades.score": { "$gt": 30 } } )
41+
cursor = db.restaurants.find({"grades.score": {"$gt": 30}})
4242
- pre: |
4343
Iterate the cursor and print the matching documents.
4444
language: python
@@ -54,7 +54,7 @@ edition: python
5454
operation:
5555
- language: python
5656
code: |
57-
cursor = db.restaurants.find( { "grades.score": { "$lt": 10 } } )
57+
cursor = db.restaurants.find({"grades.score": {"$lt": 10}})
5858
- pre: |
5959
Iterate the cursor and print the matching documents.
6060
language: python

primer/source/includes/examples-query-sort.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ operation:
4444
language: python
4545
code: |
4646
import pymongo
47-
cursor = db.restaurants.find().sort( [
48-
( "borough", pymongo.ASCENDING ),
49-
( "address.zipcode", pymongo.DESCENDING )
50-
] )
47+
cursor = db.restaurants.find().sort([
48+
("borough", pymongo.ASCENDING),
49+
("address.zipcode", pymongo.DESCENDING)
50+
])
5151
- pre: |
5252
Iterate the cursor and print the matching documents.
5353
language: python

primer/source/includes/examples-remove.yaml

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,15 @@ edition: python
8888
operation:
8989
language: python
9090
code: |
91-
db.restaurants.remove( { "borough": "Manhattan" } )
91+
result = db.restaurants.delete_many({"borough": "Manhattan"})
9292
post: |
93-
The remove operation returns a ``dict`` object which contains the
94-
status of the operation. Successful operation should return ``u'ok':
95-
1.0``. ``u'n'`` field indicates the number of documents removed.
93+
The operation returns a :py:class:`~pymongo.results.DeleteResult` which
94+
reports the number of documents removed.
95+
results: |
96+
result.deleted_count
97+
9826
9698
replacement:
97-
removeMethod: :py:meth:`~pymongo.collection.Collection.remove()`
99+
removeMethod: :py:meth:`~pymongo.collection.Collection.delete_many`
98100
---
99101
source:
100102
file: examples-remove-base.yaml
@@ -104,13 +106,15 @@ edition: python
104106
operation:
105107
language: python
106108
code: |
107-
db.restaurants.remove()
109+
result = db.restaurants.delete_many({})
108110
post: |
109-
The remove operation returns a ``dict`` object which contains the
110-
status of the operation. Successful operation should return ``u'ok':
111-
1.0``. ``u'n'`` field indicates the number of documents removed.
111+
The operation returns a :py:class:`~pymongo.results.DeleteResult` which
112+
reports the number of documents removed.
113+
results: |
114+
result.deleted_count
115+
15533
112116
replacement:
113-
removeMethod: :py:meth:`~pymongo.collection.Collection.remove()`
117+
removeMethod: :py:meth:`~pymongo.collection.Collection.delete_many`
114118
---
115119
source:
116120
file: examples-remove-base.yaml
@@ -122,7 +126,7 @@ operation:
122126
code: |
123127
db.restaurants.drop()
124128
replacement:
125-
dropMethod: :py:meth:`~pymongo.collection.Collection.drop()`
129+
dropMethod: :py:meth:`~pymongo.collection.Collection.drop`
126130
---
127131
source:
128132
file: examples-remove-base.yaml

primer/source/includes/examples-update-fields.yaml

Lines changed: 53 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -75,60 +75,83 @@ edition: python
7575
operation:
7676
language: python
7777
code: |
78-
db.restaurants.update(
79-
{ "name" : "Juni" },
78+
result = db.restaurants.update_one(
79+
{"name": "Juni"},
8080
{
81-
"$set": {
82-
"cuisine": "American (New)"
83-
},
84-
"$currentDate": { "lastModified": True }
81+
"$set": {
82+
"cuisine": "American (New)"
83+
},
84+
"$currentDate": {"lastModified": True}
8585
}
8686
)
87-
8887
post: |
89-
The operation returns a ``dict`` object which contains the status of
90-
the operation. Successful operation should return ``u'ok': 1.0``. The
91-
``u'n'`` field indicates the number of documents modified.
88+
The operation returns a :py:class:`~pymongo.results.UpdateResult` object that
89+
reports the count of documents matched and modified.
90+
results: |
91+
result.matched_count
92+
1
93+
result.modified_count
94+
1
95+
replacement:
96+
updateMethod: :py:meth:`~pymongo.collection.Collection.update_one`
9297
---
9398
source:
9499
file: examples-update-fields-base.yaml
95100
ref: _update-embedded-field
96101
ref: update-embedded-field
97102
edition: python
98103
operation:
99-
language: javascript
104+
language: python
100105
code: |
101-
db.restaurants.update(
102-
{ "restaurant_id" : "41156888" },
103-
{ "$set": { "address.street": "East 31st Street" } }
106+
result = db.restaurants.update_one(
107+
{"restaurant_id": "41156888"},
108+
{"$set": {"address.street": "East 31st Street"}}
104109
)
105110
post: |
106-
The operation returns a ``dict`` object which contains the status of
107-
the operation. Successful operation should return ``u'ok': 1.0``. The
108-
``u'n'`` field indicates the number of documents modified.
111+
The operation returns a :py:class:`~pymongo.results.UpdateResult` object that
112+
reports the count of documents matched and modified.
113+
results: |
114+
result.matched_count
115+
1
116+
result.modified_count
117+
1
118+
replacement:
119+
updateMethod: :py:meth:`~pymongo.collection.Collection.update_one`
109120
---
110121
source:
111122
file: examples-update-fields-base.yaml
112123
ref: _update-multiple-documents
113124
ref: update-multiple-documents
125+
pre: |
126+
The {{update}} method updates a single document. To update multiple
127+
documents, use the {{updateMany}} method.
128+
129+
The following operation updates *all* documents that have
130+
``address.zipcode`` field equal to ``"10016"``, setting the ``borough``
131+
field to ``"Midtown"`` and the ``lastModified`` field to the current
132+
date.
114133
edition: python
115134
operation:
116-
language: javascript
135+
language: python
117136
code: |
118-
db.restaurants.update(
119-
{ "address.zipcode": "10016" },
120-
{
121-
"$set": { "borough": "Manhattan" },
122-
"$currentDate": { "lastModified": True }
123-
},
124-
multi=True
137+
result = db.restaurants.update_many(
138+
{"address.zipcode": "10016"},
139+
{
140+
"$set": {"borough": "Midtown"},
141+
"$currentDate": {"lastModified": True}
142+
}
125143
)
126144
post: |
127-
The {{updateMethod}} returns a ``dict`` object which contains the status of
128-
the operation. Successful operation should return ``u'ok': 1.0``. The
129-
``u'n'`` field indicates the number of documents modified.
145+
The operation returns a :py:class:`~pymongo.results.UpdateResult` object that
146+
reports the count of documents matched and modified.
147+
results: |
148+
result.matched_count
149+
433
150+
result.modified_count
151+
433
130152
replacement:
131-
updateMethod: :py:meth:`~pymongo.collection.Collection.update()`
153+
update: :py:meth:`~pymongo.collection.Collection.update_one`
154+
updateMany: :py:meth:`~pymongo.collection.Collection.update_many`
132155
---
133156
source:
134157
file: examples-update-fields-base.yaml
@@ -325,4 +348,4 @@ operation:
325348
.. include:: includes/example-java-update-multiple-documents-post.txt
326349
replacement:
327350
updateMethod: "updateMany_"
328-
...
351+
...

0 commit comments

Comments
 (0)