62

Pymongo returns a cursor with it I am able to iterate over the results and append their documents to a list. Is there a way to get the result documents in a list directly? Thanks

user971956
  • 3,010
  • 7
  • 27
  • 46

2 Answers2

122

The following code will convert the entire result set (Cursor) into a list:

myresults = list(mydb.mycollection.find())

This is fine for relatively small result sets, as you are pulling everything into memory.

mcont
  • 1,543
  • 1
  • 19
  • 30
Brian Cajes
  • 3,136
  • 3
  • 19
  • 22
-3

Adding to the answer above, a quick way to create a list of all results from one particular field would be:

myResults = list(mydb.mycollection.find({},{"FieldName":1,"-id":False}))
JackX
  • 75
  • 5