Skip to content Skip to sidebar Skip to footer

Alternative For Pymongo Cursor Iteration

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'}, {

Solution 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']}]

Post a Comment for "Alternative For Pymongo Cursor Iteration"