title: exporting MongoDB to CSV using pymongo
body: <p>I would like write a script to generate a CSV file from my mongoDB database and I would like to know the most convenient version !</p>
  
  <p>first let me begin with the structure of collections.</p>
  
  <pre><code>MyDataBase -&gt; setting
                users
                fruits
  </code></pre>
  
  <p>in setting I have something like </p>
  
  <pre><code>setting -&gt; _id
             data
             _tenant
  </code></pre>
  
  <p>and the thing I am after, is making a CSV file out of profiles in <em>data</em> 
  which they have some fields/properties like "name", "address", "postalcode", "email", age and etc. and not neccessary all of these profile have all files/properties and even some of them look like collection (have sub-branches) which I am not interested in at all !</p>
  
  <p>so, my code is python so far is look like these</p>
  
  <pre><code>myquery = db.settings.find() # I am getting everything !
  output = csv.writer(open('some.csv', 'wt')) # writng in this file
  
  for items in myquery[0:10]: # first 11 entries
      a = list(items['data']['Profile'].values()) # collections are importent as dictionary and I am making them as list
      tt = list()
      for chiz in a:
          if chiz is not None:
              tt.append(chiz.encode('ascii', 'ignore')) #encoding
          else:
              tt.append("none")
      output.writerow(tt)
  </code></pre>
  
  <p>these fields/properties dont have neccessary all fields, and also even some of them are collection(with sub-branch) and will be imported as dictionary ! so, I have to convert them to list and all and all, there are quite few things to take care in such a process and in all doesn't look that straightforward !</p>
  
  <p>My question might be sounds very general but is it a typical way to make such report ?! if not, can you someone make it clear ?!</p>