I went through many questions of cursor iteration, but did not see anything that could solve my problem.
I have a form database
[{book:'A', author:'John'}, {book:'B', author:'Tony'}, {book:'C', author:'John'}...]
Several books are possible for the same author.
I need 2 arrays
authors = ['John','Tony','John']
books = ['A','B','C']
where the corresponding elements fall with the same index in both arrays.
Now I get it using cursor iteration.
authors =[]
books =[]
cursor = collection.find()
for elem in cursor:
authors.append(elem['author'])
books.append(elem['book'])
But it is very slow. I have thousands of documents. Are there other ways like queries to achieve results faster.
source
share