Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exporting MongoDB to CSV using pymongo

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 !

first let me begin with the structure of collections.

MyDataBase -> setting
              users
              fruits

in setting I have something like

setting -> _id
           data
           _tenant

and the thing I am after, is making a CSV file out of profiles in data 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 !

so, my code is python so far is look like these

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)

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 !

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 ?!

like image 853
Areza Avatar asked Sep 17 '12 11:09

Areza


People also ask

How do I export data from MongoDB to CSV?

Export MongoDB to CSV (e.g. Excel) Open the Export Wizard and select your export source. This screen only appears if you haven't chosen an item in the Connection Tree, run a previous query, or selected specific documents. Next, choose CSV as the export format then click Next.

Is PyMongo the same as MongoDB?

PyMongo is MongoDB's official native driver for Python. It's a library that lets you connect to a MongoDB database and query the data stored using the MongoDB Query API. It is the recommended way to interface with the document database.

How do I export an entire MongoDB database?

So, to export data from the MongoDB database, MongoDB provides a command-line tool known as mongoexport. Using this tool you can exports data of a collection in JSON or CSV(comma-separated value) format. Moreover, we can also use features like limit and sort on a collection while exporting the data.


1 Answers

Yes, I am using the same way. It is clear and fast, also it works without of any additional libraries.

like image 153
wowkin2 Avatar answered Nov 06 '22 05:11

wowkin2