Pravitha V
Pravitha V

Reputation: 3308

Minimizing the execution time in mongoengine

I have a document called login. the document contains the following:

I have another document called history which contains the following:

I the history document the username is referenced to login document. Using the time field I'll get the details from the history document with the following query.

usersByHistory=models.history.objects(time=searchObj.time) 

using the above result I'll get the user's information using the following code:

list=[]    
for user in usersByHistory :
   name = user.user_name.user_name
   usersList = login_info.objects(user_name=name)
   list.append(usersList)

By executing the above code the execution time is very high, I want to avoid this for loop in the code and replace it with mongoengine query so as to minimize the execution time. someone please help

Upvotes: 0

Views: 715

Answers (1)

Ross
Ross

Reputation: 18111

You could do an in query and provide the array of users: http://docs.mongoengine.org/en/latest/guide/querying.html#query-operators

Heres an example using scalar because we only need a list of user_names.

users_by_history = History.objects(time=searchObj.time).scalar('user_name')
LoginInfo.objects(user_name__in=users_by_history)

*I have updated the class names and variable names to conform to pep8

Upvotes: 2

Related Questions