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

180
Views
How to convert MotorLatentCommandCursor to list

I use Motor driver of mongoDB in my django project and I have problem to convert my data(who comes from database as cursor) to list, here is my code:

documents = db.factor.aggregate([{"$limit": 3},
     {"$lookup": {"from": "subfactor", "localField": "_id", "foreignField": "factor_id", "as": "subfactor"}}])

for d in await list(documents):
    print(d)

and the Error:

File "/usr/lib/python3.8/asyncio/runners.py", line 43, in run
return loop.run_until_complete(main)
File "/usr/lib/python3.8/asyncio/base_events.py", line 595, in run_until_complete
self.run_forever()
File "/usr/lib/python3.8/asyncio/base_events.py", line 563, in run_forever
self._run_once()
File "/usr/lib/python3.8/asyncio/base_events.py", line 1808, in _run_once
event_list = self._selector.select(timeout)
File "/usr/lib/python3.8/selectors.py", line 468, in select
fd_event_list = self._selector.poll(timeout, max_ev)
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I don't know Motor but a quick check in their docs shows this:

Cursor is async iterable, so you can use async for to iterate:

async for document in db.test_collection.find({'i': {'$lt': 2}}):
    pprint.pprint(document)

You can turn this into a list with a async list comprehention:

items = [x async for x in db.test_collection.find({'i': {'$lt': 2}})]

Also they have helper functions to convert a cursor to an awaitable list:

items = await db.test_collection.find({'i': {'$lt': 2}}).to_list(length=100)
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!