export data to csv from mongodb by using python

  • Last Update :
  • Techknowledgy :

After that conduct an export MongoDB documents python, the conversion format is automatic for whichever file type you choose based on the call method.,MongoDB compass can export the data from a collection on either JSON or CSV. In compass, we can also apply the filter on the documents while exporting the data and compass only export those data that match the given condition.,This command will help you to install the pymongo driver that is used to make connections between python and MongoDB.,For this python provides the Pandas library that gives you the ability to export MongoDB documents into different formats like CSV, JSON, and HTML.

For export the data in CSV format

mongoexport– db database_name– collection collection_name– type = csv– fields fields_names– out path_or_name_of_the_file
2._
mongoexport– - db Company - –collection EmpInfo - –type = csv - –fields name, age - –out E: \students.csv

In this example, we export only 2 records of the EmpInfo collection using the limit into a CSV file.

mongoexport--db Company--collection EmpInfo--limit 2--type = csv--fields name, age--out E: \employee.csv
  • Open MongoDB shell and run the above command to start MongoDB server
mongo "mongodb+srv://cluster0.mrbxt.mongodb.net/myFirstDatabase"--username test123
  • Now we use the mongoexport tool to export the documents into CSV file
mongoexport--host cluster0 - shard - 00 - 01. mrbxt.mongodb.net: 27017--db starwars--collection test--type csv--fields StudentId, StudentName, age--out E: \test.csv--authenticationDatabase admin--ssl--username test123--password test123

Suggestion : 2

Here is my recommended version of mongo_export_to_file if you need Pandas:

def mongo_export_to_file():
   today = datetime.today()
today = today.strftime("%m-%d-%Y")
_, _, instance_col = set_db()
# make an API call to the MongoDB server
mongo_docs = instance_col.find()

# Convert the mongo docs to a DataFrame
docs = pandas.DataFrame(mongo_docs)
# Discard the Mongo ID
for the documents
docs.pop("_id")

# compute the output file directory and name
output_dir = os.path.join('..', '..', 'output_files', 'aws_instance_list', 'csv', '')
output_file = os.path.join(output_dir, 'aws-instance-master-list-' + today + '.csv')

#
export MongoDB documents to a CSV file, leaving out the row "labels"(row numbers)
docs.to_csv(output_file, ",", index = False) # CSV delimited by commas

Here is the version I would use in a project that did not need Pandas:

def mongo_export_to_file():
   today = datetime.today()
today = today.strftime("%m-%d-%Y")
_, _, instance_col = set_db()
# make an API call to the MongoDB server
mongo_docs = instance_col.find()
if mongo_docs.count() == 0:
   return

fieldnames = list(mongo_docs[0].keys())
fieldnames.remove('_id')

# compute the output file directory and name
output_dir = os.path.join('..', '..', 'output_files', 'aws_instance_list', 'csv', '')
output_file = os.path.join(output_dir, 'aws-instance-master-list-' + today + '.csv')
with open(output_file, 'w', newline = '') as csvfile:
   writer = csv.DictWriter(csvfile, fieldnames = fieldnames, extrasaction = "ignore")
writer.writeheader()
writer.writerows(mongo_docs)
  1. Substituting the doc dict in the first line of the for loop by an OrderedDict:
...
from collections
import OrderedDict
   ...
   ...
   # iterate over the list of MongoDB dict documents
for num, doc in enumerate(mongo_docs):
   doc = OrderedDict(doc)

In the export to csv format, add index=False:

      #
      export MongoDB documents to a CSV file
      docs.to_csv(output_file, sep = ",", index = False) # CSV delimited by commas

What I had to do was create a list of filednames and apply it to the DF:

#
export MongoDB documents to a CSV file
fieldnames = ['AWS Account', 'Account Number', 'Name', 'Instance ID', 'AMI ID', 'Volumes', 'Private IP', 'Public IP', 'Private DNS', 'Availability Zone', 'VPC ID', 'Type', 'Key Pair Name', 'State', 'Launch Date']
docs.to_csv(output_file, columns = fieldnames, sep = ",", index = False) # CSV delimited by commas

Suggestion : 3

2 days ago Apr 15, 2020  · Export MongoDB data to CSV file using fs. For this method, we need json2csv module. The module has Parser class that we can use parse () method to get the CSV formated data as a string. Then fs writeFile () function helps us to write the string to CSV file. Install with the command: npm install json2csv. , 6 days ago Sep 09, 2021  · To export the MongoDB documents to CSV we use to_csv () method. After that conduct an export MongoDB documents python, the conversion format is automatic for whichever file type you choose based on the call method. here, index=False to remove the index column becase sometimes its automatically generate. ,So... The problem is that the csv writer doesn't understand the concept of "subdictionaries" as mongo returns it.,What I understand you should do is "walk" the list of answers and create one dictionary out of each record (each dictionary) in that list. Once you have a list of "flattened" dictionaries you can pass those and write them in your csv file:


"answers": [{
   "order": 0,
   "text": {
      "en": "Yes"
   },
   "answerId": "527d65de7563dd0fb98fa28c"
}, {
   "order": 1,
   "text": {
      "en": "No"
   },
   "answerId": "527d65de7563dd0fb98fa28b"
}]

{
   "_id": "a hex ID that correspond with the record that contains several answers",
   "answers": [...a list with a bunch of dicts in it...]
}
1._
mongoexport– db database_name– collection collection_name– type = csv– fields fields_names– out path_or_name_of_the_file
mongoexport –db database_name –collection collection_name –type=csv –fields fields_names –out path_or_name_of_the_file
mongoexport– - db Company - –collection EmpInfo - –type = csv - –fields name, age - –out E: \students.csv
mongoexport –-db Company -–collection EmpInfo -–type=csv -–fields name,age -–out E:\students.csv
mongoexport--db Company--collection EmpInfo--limit 2--type = csv--fields name, age--out E: \employee.csv
mongoexport --db Company --collection EmpInfo --type csv --noHeaderLine --fields name,age --out E:\header.csv
mongo "mongodb+srv://cluster0.mrbxt.mongodb.net/myFirstDatabase"--username test123
mongo "mongodb+srv://cluster0.mrbxt.mongodb.net/myFirstDatabase"
--username test123
mongoexport--host cluster0 - shard - 00 - 01. mrbxt.mongodb.net: 27017--db starwars--collection test--type csv--fields StudentId, StudentName, age--out E: \test.csv--authenticationDatabase admin--ssl--username test123--password test123