[DOCS-6903] Comment on: "primer/aggregation.txt" Created: 05/Jan/16  Updated: 03/Nov/17  Resolved: 29/Mar/16

Status: Closed
Project: Documentation
Component/s: None
Affects Version/s: None
Fix Version/s: 01112017-cleanup

Type: Bug Priority: Major - P3
Reporter: Docs Collector User (Inactive) Assignee: Kay Kim (Inactive)
Resolution: Cannot Reproduce Votes: 0
Labels: collector-298ba4e7
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

python, mongo, mac osx, linux

Location: https://docs.mongodb.org/getting-started/python/aggregation/
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36
Referrer: https://www.google.be/
Screen Resolution: 1920 x 1080
repo: docs
source: aggregation


Participants:
Days since reply: 7 years, 46 weeks, 2 days ago

 Description   

Hello, (https://docs.mongodb.org/getting-started/python/aggregation/)

Your documentation is not complete.

The "db.collection.aggregate.(....)" will not return a cursor on the different value but a dict with "ok" (with value 0 or 1) and "result" (with the different values).

The actual documentation should be:

----------------------------------------------------------------------------------------

cursor = db.restaurants.aggregate(
[
{"$group": {"_id": "$borough", "count":

{"$sum": 1}

}}
]
)

if cursor['ok'] == 1:
for document in cursor['result']:
print(document)
else:
print "No document found"

----------------------------------------------------------------------------------------

Hope it will be useful.

Thanks you.



 Comments   
Comment by Kay Kim (Inactive) [ 29/Mar/16 ]

Hi – thank you for filing the ticket. I believe the documentation is correct as since PyMongo v3.0 aggregate method returns a CommandCursor (see API)

>>> cursor = db.restaurants.aggregate(
...     [
...         {"$group": {"_id": "$borough", "count": {"$sum": 1}}}
...     ]
... )
>>> 
>>> print cursor
<pymongo.command_cursor.CommandCursor object at 0x100b63f90>
>>> if cursor['ok'] == 1:
...   print cursor
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'CommandCursor' object has no attribute '__getitem__'

To verify the version of PyMongo:

>>> import pymongo
>>> pymongo.__version__
'3.2.2'

Generated at Thu Feb 08 07:53:12 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.