pymongo: use projection=[] instead of fields={}
With current pymongo==3.13.0, was causing exception:
> podman-compose exec web env PYTHONPATH=. python explainshell/manager.py --log info --verify
...
INFO:explainshell.store:creating store, db = 'explainshell', host = 'mongodb://db'
Traceback (most recent call last):
File "explainshell/manager.py", line 201, in <module>
sys.exit(main(args.files, args.db, args.host, args.overwrite, args.drop, args.verify))
File "explainshell/manager.py", line 165, in main
ok = s.verify()
File "/opt/webapp/explainshell/store.py", line 372, in verify
manpages = set([m['_id'] for m in self.manpage.find(fields={'_id':1})])
File "/usr/local/lib/python2.7/site-packages/pymongo/collection.py", line 1696, in find
return Cursor(self, *args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'fields'
exit code: 1
Now works:
> podman-compose exec web env PYTHONPATH=. python explainshell/manager.py --log info --verify
...
INFO:explainshell.store:creating store, db = 'explainshell', host = 'mongodb://db'
exit code: 0
I tested the other calls too; updatemanpage unreachable from
current CLI but tested same call in another code path.
Not sure when pymongo API changed; oldest doc I found online https://pymongo.readthedocs.io/en/3.5.1/api/pymongo/collection.html#pymongo.collection.Collection.find which doesn't mention fields= only projection=. Probably broken since 8406b3189797a5b14059ce5dc8ffe2e12b4e9f28?
Change from {x: 1} to ['x'] style is cosmetic, reads less magic to me, but projection arg supports both.
Thanks! Shouldn't the dependency be updated as well?
This updates the code to match documented functionality for pymongo==3.13.0 currently in requirements.txt.
The dependency has already been bumped in commit 8406b3189797a5b14059ce5dc8ffe2e12b4e9f28 and I'm pretty convinced these code paths have been broken since :-D
Oh yes here it is in the docs:
Changed in version 3.0: Changed the parameter names
spec,fields,timeout, andpartialtofilter,projection,no_cursor_timeout, andallow_partial_resultsrespectively. ...
Also here in pymongo 2->3 migration guide: https://pymongo.readthedocs.io/en/3.12.1/migrate-to-pymongo3.html#fields-renamed-projection