Skip to content

Commit

Permalink
Add distinct() method to ming.mim.Collection
Browse files Browse the repository at this point in the history
The new method is implemented as a command on the Database
connection, so that API works, too.

Signed-off-by: Doug Hellmann <doug.hellmann@dreamhost.com>
  • Loading branch information
Doug Hellmann authored and rick446 committed Jul 30, 2012
1 parent 050c73a commit a7c74b1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 9 additions & 0 deletions ming/mim.py
Expand Up @@ -133,6 +133,10 @@ def command(self, command,
elif 'mapreduce' in command:
collection = command.pop('mapreduce')
return self._handle_mapreduce(collection, **command)
elif 'distinct' in command:
collection = self._collections[command['distinct']]
key = command['key']
return list(set(d[key] for d in collection.find()))
else:
raise NotImplementedError, repr(command)

Expand Down Expand Up @@ -409,6 +413,11 @@ def map_reduce(self, map, reduce, out, full_response=False, **kwargs):
cmd_args.update(kwargs)
return self.database.command(cmd_args)

def distinct(self, key):
return self.database.command({'distinct': self.name,
'key': key,
})


class Cursor(object):

Expand Down
6 changes: 5 additions & 1 deletion ming/tests/test_mim.py
Expand Up @@ -182,5 +182,9 @@ def test_upsert_push(self):
upsert=True)
doc = test.find_one()
self.assertEqual(doc, dict(_id=0, a=5, c=[1]))


def test_distinct(self):
for i in range(5):
self.bind.db.coll.insert({'_id':str(i), 'a':'A'})
result = self.bind.db.coll.distinct('a')
self.assertEqual(result, ['A'])

0 comments on commit a7c74b1

Please sign in to comment.