Skip to content
Browse files

Merge pull request #44 from FlorianLudwig/collection_names

Helper function to get list of all collections #43
  • Loading branch information...
2 parents ad8e8fd + acafc2e commit c10ed09889f8121ecf448660b7e25ae569dd3b43 @jehiah jehiah committed Jan 11, 2013
Showing with 13 additions and 0 deletions.
  1. +13 −0 asyncmongo/client.py
View
13 asyncmongo/client.py
@@ -18,6 +18,7 @@
from pool import ConnectionPools
from cursor import Cursor
from bson.son import SON
+from functools import partial
class Client(object):
"""
@@ -89,6 +90,18 @@ def connection(self, collectionname, dbname=None):
"null character")
return Cursor(dbname or self._pool._dbname, collectionname, self._pool)
+ def collection_names(self, callback):
+ """Get a list of all the collection names in selected database"""
+ callback = partial(self._collection_names_result, callback)
+ self["system.namespaces"].find(_must_use_master=True, callback=callback)
+
+ def _collection_names_result(self, callback, results, error=None):
+ """callback to for collection names query, filters out collection names"""
+ names = [r['name'] for r in results if r['name'].count('.') == 1]
+ assert error == None, repr(error)
+ strip = len(self._pool._dbname) + 1
+ callback([name[strip:] for name in names])
+
def command(self, command, value=1, callback=None,
check=True, allowable_errors=[], **kwargs):
"""Issue a MongoDB command.

0 comments on commit c10ed09

Please sign in to comment.
Something went wrong with that request. Please try again.