diff --git a/pymongo/database.py b/pymongo/database.py index 0abcf1e7a..14681b940 100644 --- a/pymongo/database.py +++ b/pymongo/database.py @@ -397,14 +397,19 @@ class Database(common.BaseObject): return result - def collection_names(self): + def collection_names(self, include_system_collections=True): """Get a list of all the collection names in this database. + + :Parameters: + - `include_system_collections` (optional): if ``False`` list + will not include system collections (e.g ``system.indexes``) """ results = self["system.namespaces"].find(_must_use_master=True) names = [r["name"] for r in results] names = [n[len(self.__name) + 1:] for n in names - if n.startswith(self.__name + ".")] - names = [n for n in names if "$" not in n] + if n.startswith(self.__name + ".") and "$" not in n] + if not include_system_collections: + names = [n for n in names if not n.startswith("system.")] return names def drop_collection(self, name_or_collection): diff --git a/test/test_database.py b/test/test_database.py index 60b6b2953..1637a8f16 100644 --- a/test/test_database.py +++ b/test/test_database.py @@ -114,6 +114,10 @@ class TestDatabase(unittest.TestCase): for coll in colls: self.assertTrue("$" not in coll) + colls_without_systems = db.collection_names(False) + for coll in colls_without_systems: + self.assertTrue(not coll.startswith("system.")) + def test_drop_collection(self): db = Database(self.client, "pymongo_test")