I am using pymongo and mongoengine, Below is the collection schema.
class Person(Document, BaseMixin):
school_id = StringField(required = True)
first_name = StringField(default="")
last_name = StringField(default="")
email = StringField(default="")
creation_time = DateTimeField(default = datetime.utcnow())
user_status = StringField(default='active') # possible values, 'active', 'blocked', 'deleted'
meta = {
'indexes': [('school_id', 'first_name'), ('school_id', 'user_status', 'first_name'),
('school_id', 'user_status', 'creation_time'), ('school_id', 'creation_time')],
'index_background': True
}
I want to get the list of active and blocked users for the particular date range and search active and blocked users by their first name. I was going through the mongo docs about the indexes and found this document stating its better to have indexes on status filed mongo docs
I have created the above indexes to test which index mongo picks while processing queries. The collection contains data for multiple schools so added index on school_id. First query: Index used = school_id_1_user_status_1_first_name_1
Person.objects(school_id = 'test', user_status__in = ['active', 'blocked'], first_name = 'test_name')
Second query: index used = school_id_1_creation_time_1
Person.objects(school_id = 'test', user_status__in = ['active', 'blocked'], creation_time__gte = datetime(2018, 7, 1))
So I am not sure how indexes are being used in this situation?, Can anyone give more information about it. Also, suggest me if I am doing something wrong with my indexing scheme or collection schema. Thanks.