Skip to content

Instantly share code, notes, and snippets.

@filipeandre
Created June 15, 2024 21:25
Show Gist options
  • Save filipeandre/2b30112974b20b32c4420140f7b854b3 to your computer and use it in GitHub Desktop.
Save filipeandre/2b30112974b20b32c4420140f7b854b3 to your computer and use it in GitHub Desktop.
Example of a script that exports a mongo db collection to an excel file
import os
import pandas as pd
from pymongo import MongoClient
import inquirer
import configparser
# pip install pymongo pandas openpyxl inquirer
config_file = 'to_excel.ini'
config_section = 'mongodb'
def get_user_inputs():
questions = [
inquirer.Text('user', message="Please enter the MongoDB user"),
inquirer.Password('password', message="Please enter the MongoDB password"),
inquirer.Text('host', message="Please enter the MongoDB host"),
inquirer.Text('app_name', message="Please enter the app name"),
inquirer.Text('db', message="Please enter the database name"),
inquirer.Text('collection', message="Please enter the collection name"),
inquirer.Text('file_name', message="Please enter the output Excel file name (without extension)")
]
return inquirer.prompt(questions)
def load_config():
config = configparser.ConfigParser()
if os.path.exists(config_file):
config.read(config_file)
return config
def main():
config = load_config()
if not config.has_section(config_section):
config.add_section(config_section)
config[config_section] = get_user_inputs()
with open(config_file, 'w') as configfile:
config.write(configfile)
config = config[config_section]
client = MongoClient(
f"mongodb+srv://{config['user']}:{config['password']}@{config['host']}/?retryWrites=true&w=majority&appName={config['app_name']}"
)
db = client[config['db']]
collection = db[config['collection']]
data = collection.find()
records = []
for record in data:
records.append({
'id': record['_id'],
'name': record['data']['name'],
'email': record['data']['email'],
'country': record['data']['country'],
'tel': record['data']['tel'],
'attendance': record['data']['attendance'],
'adults': record['data']['adults'],
'children': record['data']['children'],
'meal': record['data']['meal'],
'remarks': record['data']['remarks'],
'ip': record['meta']['ip'],
'userAgent': record['meta']['userAgent'],
'date': record['meta']['date'],
'ref': record['ref'],
'status': record['status']
})
df = pd.DataFrame(records)
df.to_excel(f"{config['file_name']}.xlsx", index=False)
print('Excel file generated successfully.')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment