Home > Net >  Pymongo Cursor Iteration Bottleneck
Pymongo Cursor Iteration Bottleneck

Time:09-22

I am trying to convert this MongoDB Cursor() object returned from the "nearest" query to a list datatype. This appears to be a bottleneck in my code. I am looking for this operation to be done in just a few milliseconds. Any help would be greatly appreciated. Thank you.

nearest = self.database_objs[common_models.ObjModel().current_geographic_collection].find({"location": {
            "$geoWithin": {
                "$centerSphere": [start, self.distance_radians(self.feet_meter(radius))]}}})

print(list(nearest)) # problem here

CodePudding user response:

Note that find just set a cursor to the first point the query is satisfied (it searches through the collection and stops on the first document that satisfies the conditions).
list(nearest) evaluate the result and loads all the documents from the mongodb cursor to your RAM. If you have a lot of documents in your result query, it will take some time to load them.
You can limit your result and it will be faster.

  • Related