Skip to content

Update python code samples for PyMongo 3.0. #2234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions primer/source/includes/examples-aggregation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@ operation:
- language: python
code: |
cursor = db.restaurants.aggregate(
[
{ "$group": { "_id": "$borough", "count": { "$sum": 1 } } }
],
cursor={}
[
{"$group": {"_id": "$borough", "count": {"$sum": 1}}}
]
)
- pre: "Iterate the cursor and print the matching documents."
language: python
Expand All @@ -87,11 +86,10 @@ operation:
- language: python
code: |
cursor = db.restaurants.aggregate(
[
{ "$match": { "borough": "Queens", "cuisine": "Brazilian" } },
{ "$group": { "_id": "$address.zipcode" , "count": { "$sum": 1 } } }
],
cursor={}
[
{"$match": {"borough": "Queens", "cuisine": "Brazilian"}},
{"$group": {"_id": "$address.zipcode", "count": {"$sum": 1}}}
]
)
- pre: |
Iterate the cursor and print the matching documents.
Expand Down
12 changes: 6 additions & 6 deletions primer/source/includes/examples-index.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ operation:
language: python
code: |
import pymongo
db.restaurants.create_index( [ ( "cuisine", pymongo.ASCENDING ) ] )
db.restaurants.create_index([("cuisine", pymongo.ASCENDING)])
results: |
"u'cuisine_1'"
post: |
Expand All @@ -81,10 +81,10 @@ operation:
language: python
code: |
import pymongo
db.restaurants.create_index( [
( "cuisine", pymongo.ASCENDING ),
( "address.zipcode", pymongo.DESCENDING )
] )
db.restaurants.create_index([
("cuisine", pymongo.ASCENDING),
("address.zipcode", pymongo.DESCENDING)
])
results: |
"u'cuisine_1_address.zipcode_-1'"
post: |
Expand Down Expand Up @@ -173,4 +173,4 @@ edition: cpp
operation:
literalinclude: includes/example-cpp-create-compound-index.cpp
language: cpp
...
...
61 changes: 31 additions & 30 deletions primer/source/includes/examples-insert.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,41 +60,42 @@ edition: python
operation:
language: python
code: |
from datetime import *;
db.restaurants.insert(
{
"address" : {
"street" : "2 Avenue",
"zipcode" : "10075",
"building" : "1480",
"coord" : [ -73.9557413, 40.7720266 ],
},
"borough" : "Manhattan",
"cuisine" : "Italian",
"grades" : [
{
"date" : datetime.strptime("2014-10-01", "%Y-%m-%d"),
"grade" : "A",
"score" : 11
},
{
"date" : datetime.strptime("2014-01-16", "%Y-%m-%d"),
"grade" : "B",
"score" : 17
}
],
"name" : "Vella",
"restaurant_id" : "41704620"
}
from datetime import datetime
result = db.restaurants.insert_one(
{
"address": {
"street": "2 Avenue",
"zipcode": "10075",
"building": "1480",
"coord": [-73.9557413, 40.7720266]
},
"borough": "Manhattan",
"cuisine": "Italian",
"grades": [
{
"date": datetime.strptime("2014-10-01", "%Y-%m-%d"),
"grade": "A",
"score": 11
},
{
"date": datetime.strptime("2014-01-16", "%Y-%m-%d"),
"grade": "B",
"score": 17
}
],
"name": "Vella",
"restaurant_id": "41704620"
}
)
post: |
The operation returns the ``_id`` field value for the inserted
document.
The operation returns an :py:class:`~pymongo.results.InsertOneResult` object,
which includes the ``_id`` of the inserted document.
results: |
result.inserted_id
ObjectId("54c1478ec2341ddf130f62b7")
replacement:
insertMethod: :py:meth:`~pymongo.collection.Collection.insert()`
clientName: :py:mod:`pymongo`
insertMethod: :py:meth:`~pymongo.collection.Collection.insert_one`
clientName: :py:class:`~pymongo.mongo_client.MongoClient`
---
source:
file: examples-insert-base.yaml
Expand Down
5 changes: 3 additions & 2 deletions primer/source/includes/examples-query-combination.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ edition: python
operation:
- language: python
code: |
cursor = db.restaurants.find( { "cuisine": "Italian", "address.zipcode": "10075" } )
cursor = db.restaurants.find({"cuisine": "Italian", "address.zipcode": "10075"})
- pre: |
Iterate the cursor and print the matching documents.
language: python
Expand All @@ -55,7 +55,8 @@ edition: python
operation:
- language: python
code: |
cursor = db.restaurants.find( { "$or": [ { "cuisine": "Italian" }, { "address.zipcode": "10075" } ] } )
cursor = db.restaurants.find(
{"$or": [{"cuisine": "Italian"}, {"address.zipcode": "10075"}]})
- pre: |
Iterate the cursor and print the matching documents.
language: python
Expand Down
6 changes: 3 additions & 3 deletions primer/source/includes/examples-query-equality.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ edition: python
operation:
- language: python
code: |
cursor = db.restaurants.find( { "borough": "Manhattan" } )
cursor = db.restaurants.find({"borough": "Manhattan"})
- pre: |
Iterate the cursor and print the matching documents.
language: python
Expand All @@ -63,7 +63,7 @@ edition: python
operation:
- language: python
code: |
cursor = db.restaurants.find( { "address.zipcode": "10075" } )
cursor = db.restaurants.find({"address.zipcode": "10075"})
- pre: |
Iterate the cursor and print the matching documents.
language: python
Expand All @@ -79,7 +79,7 @@ edition: python
operation:
- language: python
code: |
cursor = db.restaurants.find( { "grades.grade": "B" } )
cursor = db.restaurants.find({"grades.grade": "B"})
- pre: |
Iterate the cursor and print the matching documents.
language: python
Expand Down
4 changes: 2 additions & 2 deletions primer/source/includes/examples-query-operators.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ edition: python
operation:
- language: python
code: |
cursor = db.restaurants.find( { "grades.score": { "$gt": 30 } } )
cursor = db.restaurants.find({"grades.score": {"$gt": 30}})
- pre: |
Iterate the cursor and print the matching documents.
language: python
Expand All @@ -53,7 +53,7 @@ edition: python
operation:
- language: python
code: |
cursor = db.restaurants.find( { "grades.score": { "$lt": 10 } } )
cursor = db.restaurants.find({"grades.score": {"$lt": 10}})
- pre: |
Iterate the cursor and print the matching documents.
language: python
Expand Down
8 changes: 4 additions & 4 deletions primer/source/includes/examples-query-sort.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ operation:
language: python
code: |
import pymongo
cursor = db.restaurants.find().sort( [
( "borough", pymongo.ASCENDING ),
( "address.zipcode", pymongo.DESCENDING )
] )
cursor = db.restaurants.find().sort([
("borough", pymongo.ASCENDING),
("address.zipcode", pymongo.DESCENDING)
])
- pre: |
Iterate the cursor and print the matching documents.
language: python
Expand Down
26 changes: 15 additions & 11 deletions primer/source/includes/examples-remove.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,15 @@ edition: python
operation:
language: python
code: |
db.restaurants.remove( { "borough": "Manhattan" } )
result = db.restaurants.delete_many({"borough": "Manhattan"})
post: |
The remove operation returns a ``dict`` object which contains the
status of the operation. Successful operation should return ``u'ok':
1.0``. ``u'n'`` field indicates the number of documents removed.
The operation returns a :py:class:`~pymongo.results.DeleteResult` which
reports the number of documents removed.
results: |
result.deleted_count
9826
replacement:
removeMethod: :py:meth:`~pymongo.collection.Collection.remove()`
removeMethod: :py:meth:`~pymongo.collection.Collection.delete_many`
---
source:
file: examples-remove-base.yaml
Expand All @@ -103,13 +105,15 @@ edition: python
operation:
language: python
code: |
db.restaurants.remove()
result = db.restaurants.delete_many({})
post: |
The remove operation returns a ``dict`` object which contains the
status of the operation. Successful operation should return ``u'ok':
1.0``. ``u'n'`` field indicates the number of documents removed.
The operation returns a :py:class:`~pymongo.results.DeleteResult` which
reports the number of documents removed.
results: |
result.deleted_count
15533
replacement:
removeMethod: :py:meth:`~pymongo.collection.Collection.remove()`
removeMethod: :py:meth:`~pymongo.collection.Collection.delete_many`
---
source:
file: examples-remove-base.yaml
Expand All @@ -121,7 +125,7 @@ operation:
code: |
db.restaurants.drop()
replacement:
dropMethod: :py:meth:`~pymongo.collection.Collection.drop()`
dropMethod: :py:meth:`~pymongo.collection.Collection.drop`
---
source:
file: examples-remove-base.yaml
Expand Down
83 changes: 53 additions & 30 deletions primer/source/includes/examples-update-fields.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,60 +74,83 @@ edition: python
operation:
language: python
code: |
db.restaurants.update(
{ "name" : "Juni" },
result = db.restaurants.update_one(
{"name": "Juni"},
{
"$set": {
"cuisine": "American (New)"
},
"$currentDate": { "lastModified": True }
"$set": {
"cuisine": "American (New)"
},
"$currentDate": {"lastModified": True}
}
)

post: |
The operation returns a ``dict`` object which contains the status of
the operation. Successful operation should return ``u'ok': 1.0``. The
``u'n'`` field indicates the number of documents modified.
The operation returns a :py:class:`~pymongo.results.UpdateResult` object that
reports the count of documents matched and modified.
results: |
result.matched_count
1
result.modified_count
1
replacement:
updateMethod: :py:meth:`~pymongo.collection.Collection.update_one`
---
source:
file: examples-update-fields-base.yaml
ref: _update-embedded-field
ref: update-embedded-field
edition: python
operation:
language: javascript
language: python
code: |
db.restaurants.update(
{ "restaurant_id" : "41156888" },
{ "$set": { "address.street": "East 31st Street" } }
result = db.restaurants.update_one(
{"restaurant_id": "41156888"},
{"$set": {"address.street": "East 31st Street"}}
)
post: |
The operation returns a ``dict`` object which contains the status of
the operation. Successful operation should return ``u'ok': 1.0``. The
``u'n'`` field indicates the number of documents modified.
The operation returns a :py:class:`~pymongo.results.UpdateResult` object that
reports the count of documents matched and modified.
results: |
result.matched_count
1
result.modified_count
1
replacement:
updateMethod: :py:meth:`~pymongo.collection.Collection.update_one`
---
source:
file: examples-update-fields-base.yaml
ref: _update-multiple-documents
ref: update-multiple-documents
pre: |
The {{update}} method updates a single document. To update multiple
documents, use the {{updateMany}} method.

The following operation updates *all* documents that have
``address.zipcode`` field equal to ``"10016"``, setting the ``borough``
field to ``"Midtown"`` and the ``lastModified`` field to the current
date.
edition: python
operation:
language: javascript
language: python
code: |
db.restaurants.update(
{ "address.zipcode": "10016" },
{
"$set": { "borough": "Manhattan" },
"$currentDate": { "lastModified": True }
},
multi=True
result = db.restaurants.update_many(
{"address.zipcode": "10016"},
{
"$set": {"borough": "Midtown"},
"$currentDate": {"lastModified": True}
}
)
post: |
The {{updateMethod}} returns a ``dict`` object which contains the status of
the operation. Successful operation should return ``u'ok': 1.0``. The
``u'n'`` field indicates the number of documents modified.
The operation returns a :py:class:`~pymongo.results.UpdateResult` object that
reports the count of documents matched and modified.
results: |
result.matched_count
433
result.modified_count
433
replacement:
updateMethod: :py:meth:`~pymongo.collection.Collection.update()`
update: :py:meth:`~pymongo.collection.Collection.update_one`
updateMany: :py:meth:`~pymongo.collection.Collection.update_many`
---
source:
file: examples-update-fields-base.yaml
Expand Down Expand Up @@ -282,4 +305,4 @@ pre: |
date.
replacement:
updateMethod: "update_many_"
...
...
Loading