Getting mongoDB links takes time

I am using mongoengine as an Object-Document mapper. Here is a brief description of the collections that are causing the problem. Each document in collection A can have a list of links to documents in collection B.

class A(Document): list_b = ListField(EmbeddedDocumentField(EB)) #other fields are not mentioned. class EB(EmbeddedDocument): b_reference = ReferenceField('B') loc = GeoPointField() class B(Document): name = StringField() #other fields are not mentioned. 

When I try to access list objects of a specific document using

document_of_A.list_b

the execution time of the specified line depends on the number of links present in the list. E.g. it takes 100 ms for 100 links in the list.

Is there a better way to get links ?, so that the execution time of the above line will be reduced.

0
source share
1 answer

The query should use the select_related flag if you want to quickly get all the links. Note that the reference search will cost additional queries, and select_related() designed to reduce the number of rounds to mongodb.

 # Single document lookup document_of_A.select_related(2) # Queryset A.objects.select_related(2) 

Why 2 for select_related searches? Well recursive depth:

  • find any links in the list itself
  • search links in separate embedded documents
+2
source

Source: https://habr.com/ru/post/969750/


All Articles