Querying in mongoexport through python

4.8k views Asked by At

Can Anyone explain how to export the result of a query with mongoexport inside a program in python ?

os.system('mongoexport -d fintapp -c portfolio --csv --fields userid,budget --query {"budget":{$exists:True}} -o fulbudg.csv')
2

There are 2 answers

2
Sarthak On

Use subprocess

import subprocess


subprocess.call("mongoexport --db db_name --collection collection_name --out output_fileName.csv type csv --fields name_of_columns", shell = True)
0
WGP On
os.system('''mongoexport -d fintapp -c portfolio --csv --fields userid,budget --query '{"budget":{$exists:True}}' -o fulbudg.csv''')

You need quotations around the query, and they have to be different from the other quotations you have used. In python there are 4 different ways of quoting: 'quote', "quote", '''quote''', """quote"""