Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-1431

pymongo cursor taking long time to find

    • Type: Icon: Task Task
    • Resolution: Works as Designed
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 3.5
    • Environment:
      MongoDB Server: 3.2.11
      OS: MacOS High Sierra and Oracle Linux 7
      Python : 2.7

      Pymongo cursor taking ~ 2 seconds, please suggest ways to optimize below query.

      *Facts of query*

      Records in MongoDB : 120,000
      Indexed Key: emp_id
      MongoDB Server: 3.2.11
      PyMongo : 3.5
      Python : 2.7

      *Cursor*

      To find a document from db, by using indexed field as filter and projecting only required fields, its consuming ~2 Seconds.

      I am looking for ways to optimize this query so that it can be faster.

          user_data = MongoldbAdaptor(**APP_CONFIG).db().collection_name.find_one(
              {"emp_id": 1234}, {'nameFirst': 1, 'nameLast': 1, 'emp_id': 1, }
          )
      

      Elapsed Time: 1.829226017
      _____________________________________________________________________________________
      For Informational purpose, please refer same query using find()

          def query_cursor():
              cursor = MongoldbAdaptor(**APP_CONFIG).db().collection_name.find(
                  {"emp_id": 1234}, {'nameFirst': 1, 'nameLast': 1, 'emp_id': 1, }
              ).limit(1)
              
              return cursor
          
          def get_user_data():
              cursor = query_user_data()
              user_data = None
              for document in cursor:
                  user_data = document
              return user_data
          
          print(get_user_data())
      

      Elapsed Time for query_cursor: 0.00215888023376

      Elapsed Time for get_user_data: 1.79128599167

            Assignee:
            shane.harvey@mongodb.com Shane Harvey
            Reporter:
            varunrq@gmail.com Varun
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: