9

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!

Is it a typical way to make such report? If not, can someone make it clear?

5
  • 1
    Have you tried mongoexport? It will export a collection to CSV and may save you the effort of rolling your own tool. Commented Sep 17, 2012 at 11:29
  • well, I wish "mongoexport" was working in python as well ! or there was some nice IDE for mongodb to be able to play with csv/table format like, easier. Commented Sep 19, 2012 at 10:02
  • There are several different Admin UIs, but "nice" is subjective as everyone has different requirements and preferences. One lateral option you might want to look into is a reporting tool. For example, Jasper/iReport is a visual report designer that supports MongoDB and can export to multiple formats including CSV. Commented Sep 19, 2012 at 13:21
  • There are couple of approaches for this scenario. (1) Using mongoexport command line tool to create the CSV. Create a view on the collection(s) - the view takes an aggregation pipeline for performing all the data transformation - filters, mapping, lookups, reducing, projection, etc. Run the export by specifying the view in the mongoexport's --collection option (just like you use a collection). (2)... Commented Nov 10, 2023 at 5:19
  • (2) Using PyMongo, MongoDB Python driver. Create an aggregation pipeline on the collection(s) within the Python program using PyMongo APIs for all the data transformation. Run the pipeline and the output is used to write to the CSV. Use the Python CSV module's csv.writer function or csv.DictWriter class to write the pipeline output to the csv file. Commented Nov 10, 2023 at 5:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.