Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
Mongodb cursor iteration slow

I am querying data from mongodb in chunks having 100M + records in total. And then want to process those records within a function.

My query

cursor = collection.find({'facial_task':False}).sort("_id", -1).skip(1000000).limit(10000) 

When I iterate over the cursor, then it is taking too much time and gets stuck a lot even I limit the number of results up to 10. I even retrieved the results using batch size but when processing the loop over it; it takes too much time.

My loop is like this

for dd in cursor:
            ab = threading.Thread(target=insert_func, args=(dd,)) 
            ab.start()
            main_threads.append(ab)
            if len(main_threads) >= 5000:
                print("****Joining Main Thread***")
                for ii in main_threads:
                    ii.join()
                main_threads = []
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

If the slowdown occurs from the very first iteration, then it must have to do with the cursor itself.

Mongodb cursor is not like a 'chached' list of the entire collection; it queries the db one chunk at a time as you iterate over it. You can try to cast that cursor into a list and see if the iteration shows a significant speed-up.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!