diff --git a/pymongo/collection.py b/pymongo/collection.py index d285ddc1c..01013936f 100644 --- a/pymongo/collection.py +++ b/pymongo/collection.py @@ -1176,7 +1176,7 @@ class Collection(common.BaseObject): guaranteed to contain at least a single key, ``"key"`` which is a list of (key, direction) pairs specifying the index (as passed to create_index()). It will also contain any other - information in `system.indexes`, except for the ``"ns"`` and + metadata about the indexes, except for the ``"ns"`` and ``"name"`` keys, which are cleaned. Example output might look like this: @@ -1186,8 +1186,14 @@ class Collection(common.BaseObject): {u'_id_': {u'key': [(u'_id', 1)]}, u'x_1': {u'unique': True, u'key': [(u'x', 1)]}} """ - raw = self.__database.system.indexes.find({"ns": self.__full_name}, - {"ns": 0}, as_class=SON) + client = self.__database.connection + if client._writable_max_wire_version() > 2: + raw = self.__database.command( + "listIndexes", self.__name, as_class=SON, + read_preference=ReadPreference.PRIMARY).get("indexes", []) + else: + raw = self.__database.system.indexes.find({"ns": self.__full_name}, + {"ns": 0}, as_class=SON) info = {} for index in raw: index["key"] = index["key"].items() diff --git a/test/test_collection.py b/test/test_collection.py index 60000f17e..c4bf58ff7 100644 --- a/test/test_collection.py +++ b/test/test_collection.py @@ -135,8 +135,7 @@ class TestCollection(IntegrationTest): db.test.drop_indexes() db.test.insert({}) - self.assertEqual(db.system.indexes.find({"ns": u("pymongo_test.test")}) - .count(), 1) + self.assertEqual(len(db.test.index_information()), 1) db.test.create_index("hello") db.test.create_index([("hello", DESCENDING), ("world", ASCENDING)]) @@ -144,10 +143,7 @@ class TestCollection(IntegrationTest): # Tuple instead of list. db.test.create_index((("world", ASCENDING),)) - count = 0 - for _ in db.system.indexes.find({"ns": u("pymongo_test.test")}): - count += 1 - self.assertEqual(count, 4) + self.assertEqual(len(db.test.index_information()), 4) db.test.drop_indexes() ix = db.test.create_index([("hello", DESCENDING), @@ -155,20 +151,14 @@ class TestCollection(IntegrationTest): self.assertEqual(ix, "hello_world") db.test.drop_indexes() - self.assertEqual(db.system.indexes.find({"ns": u("pymongo_test.test")}) - .count(), 1) + self.assertEqual(len(db.test.index_information()), 1) db.test.create_index("hello") - self.assertTrue(u("hello_1") in - [a["name"] for a in db.system.indexes - .find({"ns": u("pymongo_test.test")})]) + self.assertTrue(u"hello_1" in db.test.index_information()) db.test.drop_indexes() - self.assertEqual(db.system.indexes.find({"ns": u("pymongo_test.test")}) - .count(), 1) + self.assertEqual(len(db.test.index_information()), 1) db.test.create_index([("hello", DESCENDING), ("world", ASCENDING)]) - self.assertTrue(u("hello_-1_world_1") in - [a["name"] for a in db.system.indexes - .find({"ns": u("pymongo_test.test")})]) + self.assertTrue(u"hello_-1_world_1" in db.test.index_information()) db.test.drop() db.test.insert({'a': 1}) @@ -285,34 +275,25 @@ class TestCollection(IntegrationTest): db.test.create_index("hello") name = db.test.create_index("goodbye") - self.assertEqual(db.system.indexes.find({"ns": u("pymongo_test.test")}) - .count(), 3) + self.assertEqual(len(db.test.index_information()), 3) self.assertEqual(name, "goodbye_1") db.test.drop_index(name) # Drop it again. with self.assertRaises(OperationFailure): db.test.drop_index(name) - - self.assertEqual(db.system.indexes.find({"ns": u("pymongo_test.test")}) - .count(), 2) - self.assertTrue(u("hello_1") in - [a["name"] for a in db.system.indexes - .find({"ns": u("pymongo_test.test")})]) + self.assertEqual(len(db.test.index_information()), 2) + self.assertTrue(u"hello_1" in db.test.index_information()) db.test.drop_indexes() db.test.create_index("hello") name = db.test.create_index("goodbye") - self.assertEqual(db.system.indexes.find({"ns": u("pymongo_test.test")}) - .count(), 3) + self.assertEqual(len(db.test.index_information()), 3) self.assertEqual(name, "goodbye_1") db.test.drop_index([("goodbye", ASCENDING)]) - self.assertEqual(db.system.indexes.find({"ns": u("pymongo_test.test")}) - .count(), 2) - self.assertTrue(u("hello_1") in - [a["name"] for a in db.system.indexes - .find({"ns": u("pymongo_test.test")})]) + self.assertEqual(len(db.test.index_information()), 2) + self.assertTrue(u"hello_1" in db.test.index_information()) def test_reindex(self): db = self.db diff --git a/test/test_gridfs.py b/test/test_gridfs.py index b3d17acd1..cdf86859c 100644 --- a/test/test_gridfs.py +++ b/test/test_gridfs.py @@ -146,10 +146,11 @@ class TestGridfs(IntegrationTest): self.assertTrue(isinstance(raw["md5"], string_type)) def test_delete_ensures_index(self): - chunks = self.db.fs.chunks - # setUp has dropped collections. - self.assertFalse(chunks.index_information()) + names = self.db.collection_names() + self.assertFalse([name for name in names if name.startswith('fs')]) + + chunks = self.db.fs.chunks self.fs.delete(file_id=1) diff --git a/test/test_read_preferences.py b/test/test_read_preferences.py index d4d04bbab..e56627dfe 100644 --- a/test/test_read_preferences.py +++ b/test/test_read_preferences.py @@ -321,17 +321,15 @@ class TestCommandAndReadPreference(TestReplicaSetClientBase): self._test_fn(True, lambda: self.c.pymongo_test.command(SON([ ('distinct', 'test'), ('key', 'a'), ('query', {'a': 1})]))) - # Geo stuff. Make sure a 2d index is created and replicated - self.c.pymongo_test.system.indexes.insert({ - 'key' : { 'location' : '2d' }, 'ns' : 'pymongo_test.test', - 'name' : 'location_2d' }, w=self.w) + # Geo stuff. + self.c.pymongo_test.test.create_index([('location', '2d')]) - self.c.pymongo_test.system.indexes.insert(SON([ - ('ns', 'pymongo_test.test'), - ('key', SON([('location', 'geoHaystack'), ('key', 1)])), - ('bucketSize', 100), - ('name', 'location_geoHaystack'), - ]), w=self.w) + self.c.pymongo_test.test.create_index([('location', 'geoHaystack'), + ('key', 1)], bucketSize=100) + + # Attempt to await replication of indexes. + self.c.pymongo_test.test2.insert({}, w=self.w) + self.c.pymongo_test.test2.remove({}, w=self.w) self._test_fn(True, lambda: self.c.pymongo_test.command( 'geoNear', 'test', near=[0, 0])) @@ -366,8 +364,9 @@ class TestCommandAndReadPreference(TestReplicaSetClientBase): 'name': 't_text', 'key': {'t': 'text'}} - db.system.indexes.insert( - index, manipulate=False, check_keys=False, w=self.w) + db.test.create_index([('t', 'text')]) + db.test.insert({}, w=self.w) + db.test.remove({}, w=self.w) self._test_fn(True, lambda: self.c.pymongo_test.command(SON([ ('text', 'test'),