I’m using asyncmongo with Tornado + gen.engine, and just wondering what the syntax is for the equivalent of db.collection.distinct("mykey")?
I assume it’s something like
result, error = yield gen.Task(settings.DB.my_data.find, {}, distinct=[("key","mykey")])
But that won’t work. For whatever reason I cannot find any examples of this online.
Thanks.
The problem is, “distinct” not an option for a query, it is a separate command:
http://www.mongodb.org/display/DOCS/Aggregation#Aggregation-Distinct
So use AsyncMongo’s command() method:
The data you need is in result[‘values’].
More examples of using commands from AsyncMongo are in its test suite:
https://github.com/bitly/asyncmongo/blob/master/test/test_command.py
And information on MongoDB commands in general (the examples are in PHP but it’s easy to understand even for Python coders like us):
http://www.kchodorow.com/blog/2011/01/25/why-command-helpers-suck/