问题
I went through many cursor iterationn questions, but saw nothing which can solve my problem.
I have a database of the form
[{book:'A', author:'John'}, {book:'B', author:'Tony'}, {book:'C', author:'John'}...]
Multiple books for same author possible.
What I need is 2 arrays
authors = ['John','Tony','John']
books = ['A','B','C']
where corresponding elements falls at same index in both arrays.
Now I am getting it by cursor iteration.
authors =[]
books =[]
cursor = collection.find()
for elem in cursor:
authors.append(elem['author'])
books.append(elem['book'])
But this is very slow. I have thousands of documents. Are there any other ways like queries to achieve the result faster.
回答1:
An aggregation query can be performed to collect all authors and books. e.g.
pipeline = [
{
'$group': {
'_id': None,
'authors': { '$push': '$author' },
'books': { '$push': '$book' }
}
}
]
result = collection.aggregate(pipeline))
In [2]: print(result)
[{'_id': None, 'authors': ['John', 'Tony', 'John'], 'books': ['A', 'B', 'C']}]
来源:https://stackoverflow.com/questions/49626941/alternative-for-pymongo-cursor-iteration