def test_covered_index(self): """Ensure that covered indexes can be used """ class Test(Document): a = IntField() b = IntField() meta = {'indexes': ['a'], 'allow_inheritance': False} Test.drop_collection() obj = Test(a=1) obj.save() IS_MONGODB_3 = get_mongodb_version() >= MONGODB_3 # Need to be explicit about covered indexes as mongoDB doesn't know if # the documents returned might have more keys in that here. query_plan = Test.objects(id=obj.id).exclude('a').explain() if not IS_MONGODB_3: self.assertFalse(query_plan['indexOnly']) else: self.assertEqual( query_plan.get('queryPlanner').get('winningPlan').get( 'inputStage').get('stage'), 'IDHACK') query_plan = Test.objects(id=obj.id).only('id').explain() if not IS_MONGODB_3: self.assertTrue(query_plan['indexOnly']) else: self.assertEqual( query_plan.get('queryPlanner').get('winningPlan').get( 'inputStage').get('stage'), 'IDHACK') query_plan = Test.objects(a=1).only('a').exclude('id').explain() if not IS_MONGODB_3: self.assertTrue(query_plan['indexOnly']) else: self.assertEqual( query_plan.get('queryPlanner').get('winningPlan').get( 'inputStage').get('stage'), 'IXSCAN') self.assertEqual( query_plan.get('queryPlanner').get('winningPlan').get('stage'), 'PROJECTION') query_plan = Test.objects(a=1).explain() if not IS_MONGODB_3: self.assertFalse(query_plan['indexOnly']) else: self.assertEqual( query_plan.get('queryPlanner').get('winningPlan').get( 'inputStage').get('stage'), 'IXSCAN') self.assertEqual( query_plan.get('queryPlanner').get('winningPlan').get('stage'), 'FETCH')
def setUp(self): self.connection = connect(db='mongoenginetest') self.db = get_db() self.mongodb_version = get_mongodb_version() class Person(Document): name = StringField() age = IntField() non_field = True meta = {"allow_inheritance": True} self.Person = Person
def test_covered_index(self): """Ensure that covered indexes can be used """ class Test(Document): a = IntField() b = IntField() meta = { 'indexes': ['a'], 'allow_inheritance': False } Test.drop_collection() obj = Test(a=1) obj.save() IS_MONGODB_3 = get_mongodb_version()[0] >= 3 # Need to be explicit about covered indexes as mongoDB doesn't know if # the documents returned might have more keys in that here. query_plan = Test.objects(id=obj.id).exclude('a').explain() if not IS_MONGODB_3: self.assertFalse(query_plan['indexOnly']) else: self.assertEqual(query_plan.get('queryPlanner').get('winningPlan').get('inputStage').get('stage'), 'IDHACK') query_plan = Test.objects(id=obj.id).only('id').explain() if not IS_MONGODB_3: self.assertTrue(query_plan['indexOnly']) else: self.assertEqual(query_plan.get('queryPlanner').get('winningPlan').get('inputStage').get('stage'), 'IDHACK') query_plan = Test.objects(a=1).only('a').exclude('id').explain() if not IS_MONGODB_3: self.assertTrue(query_plan['indexOnly']) else: self.assertEqual(query_plan.get('queryPlanner').get('winningPlan').get('inputStage').get('stage'), 'IXSCAN') self.assertEqual(query_plan.get('queryPlanner').get('winningPlan').get('stage'), 'PROJECTION') query_plan = Test.objects(a=1).explain() if not IS_MONGODB_3: self.assertFalse(query_plan['indexOnly']) else: self.assertEqual(query_plan.get('queryPlanner').get('winningPlan').get('inputStage').get('stage'), 'IXSCAN') self.assertEqual(query_plan.get('queryPlanner').get('winningPlan').get('stage'), 'FETCH')