Django + mongodb

Hello there.
I am working on a project /API/ where I have to deal with a big data. For the project I’m using django and for database mongodb. Now I have an issue with API, it works very slowly. When I’m trying to find something with some fields by mongo shell it takes some ms but by django it takes seconds sometimes minutes. I wonder why it goes so. Can anybody help me to understand what’s wrong with my code in django? For db ENGINE I’m using djongo.
‘’’
companies = Companies.objects.all()
if ‘company_status’ in data:
companies = companies.filter(company_status=data[‘company_status’])
if ‘search’ in data and data[‘search’]:
companies = companies.filter(models.Q(company_name__icontains=data[‘search’]) |
models.Q(company_number=data[‘search’]))
if ‘location’ in data and data[‘location’]:
locality = ‘“locality”: “{}”’.format(data[‘location’])
companies = companies.filter(models.Q(registered_office_address__icontains=locality))
if ‘status’ in data:
query = models.Q()
if ‘linkedin’ in data[‘status’]:
if data[‘status’][‘linkedin’][‘needDetectet’]:
query |= models.Q(is_researched=0)
if data[‘status’][‘linkedin’][‘notFound’]:
query |= (models.Q(is_researched=1) & models.Q(has_linkedin=0))
if data[‘status’][‘linkedin’][‘found’]:
query |= models.Q(has_linkedin=1)
if ‘twitter’ in data[‘status’]:
if data[‘status’][‘twitter’][‘needDetectet’]:
query |= models.Q(is_twitter_researched=0)
if data[‘status’][‘twitter’][‘notFound’]:
query |= (models.Q(has_twitter=0) & models.Q(is_twitter_researched=1))
if data[‘status’][‘twitter’][‘found’]:
query |= models.Q(has_twitter=1)
companies = companies.filter(query)
if ‘date’ in data and bool(data[‘date’]):
if ‘start’ in data[‘date’] and ‘end’ in data[‘date’]:
start_date = dateutil.parser.parse(data[‘date’][‘start’])
end_date = dateutil.parser.parse(data[‘date’][‘end’])
companies = companies.filter(date_of_creation__range=[start_date, end_date])
all_companies = companies.count()
companies = companies.order_by(’-date_of_creation’)
companies = companies[data[‘start_index’]:data[‘start_index’] + data[‘per_page’]]
companies_serializer = CompaniesBriefSerializer(companies, many=True)
‘’’.
Field types are string and date.
I would be very grateful for your help.

Please do not post screenshots of code here. Copy/paste the code into the message. (Images are hard to read on many devices, they can’t be quoted when writing replies, and they can’t be searched when looking for old messages on the site.)

When posting code, enclose it between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This ensures the forum software maintains the proper formatting of the code.

Also, you’ll want to provide the entire view. There might be aspects of this being caused by things you think aren’t important, but really are.

Finally, this belongs in the “Using Django” category and not the “Internals” category.

Now, to address the root question, you probably want to find out whether this is being executed as one query, or if this is creating “N” number of queries. I don’t know Djongo, so I’m not sure what facilities it might provide for doing that. You could try using Django Debug Toolbar to see if it is able to capture that information. You could see if MongoDB has a way of showing you queries being executed. You could even set up a network trace to watch how much traffic is going both ways.
(Is this a case where you’re running the query in the Mongo shell on the server, but executing this query remotely across a network? You may simply be dealing with bandwidth issues by needing to move this much data through the network.)

But if it comes down to an actual Djongo issue, you’re probably going to get more direct help from the creators of Djongo on whatever support facility they provide. Djongo is a third-party package and I’m not aware of any of the individuals responsible for it being members here.