1

This is how a typical document in my collection is structured:

{u'_id': ObjectId('58645996fa36ac0b9f0e738d'),
 u'alias': u'loco_ono',
 u'artist': u'ONO’,
 u'date_time': datetime.datetime(2016, 12, 29, 0, 32, 22, 723000),
 u'followers': [{u'permalink': u'pschedelicsuperfuzz',
   u'plan': u'Free'},
  {u'permalink': u'd-miller',
   u'plan': u'Free'}],
 u'followers_count': 60,
 u'last_modified': u'2016/10/17 18:53:09 +0000',
 u'plan': u'Pro'}

How can I iterate through every document in my collection and write to a CSV file the following information for each iteration using pymongo?

Artist   | Follower               | Count
loco_ono | pschedelicsuperfuzz    | 1
loco_ono | d-miller               | 1

So far I have this:

import pymongo
from pymongo import MongoClient

mongo_client = MongoClient()
db = mongo_client.soundcloud_db
artist_followers = db.artist_followers

I'm not sure how to iterate through the artist_followers collection correctly and retrieve only the 'permalink' field values from the 'followers' array for each artist to output in the format described above.

1
  • What you tried for item in artist_followers: print item yet? Commented Jan 6, 2017 at 1:52

1 Answer 1

1

I think you want to iterate over collection.find() to retrieve the documents from the collections, then use an inner loop to iterate over the followers.

I don't understand your count field, and the exact formatting will take a bit of fiddling, but here's a fragment that should help you:

for doc in artist_followers.find():
    for follower in doc["followers"]:
        print(doc["alias"], follower["permalink"])
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.