Category / Section
Fetch Specified Fields from MongoDb using Bold ETL
Published:
This article provides guidance on retrieving specific data fields from a MongoDB database utilizing Bold ETL
. This process can be accomplished through the execution of a Python script.
Prerequisite:
PyMongo is required. In upcoming releases of Bold BI
, it will be included as default.
Environment | Commands |
---|---|
Windows | C:\BoldServices\Python39\Scripts\pip.exe install pymongo |
Linux | pip install pymongo |
Here is the sample Python code for fetching selected fields from MongoDB
.
import pandas as pd
import pymongo
clientcon = pymongo.MongoClient("mongodb://user:password@host:port")
# Select the database and collection.
databasename = clientcon["yourdatabasename"]
collectionname = databasename["yourcollectionname"]
# Define the required columns.
fields_to_include = {"yourcolumn1": 1, "yourcolumn12": 1}
documents = collectionname.find({}, fields_to_include)
data = list(documents)
for doc in data:
doc['_id'] = str(doc['_id'])
df = pd.DataFrame(data)
pipeline.run(df, table_name="yourtablename")
- mongodb://user:password@host:port - Replace your connection string.
- yourdatabasename - Replace your Database name.
- yourcollectionname - Replace your Collection name.
- yourcolumn1,yourcolumn12 - Replace your column name.
- yourtablename - Replace yourtablename with the desired table name in your destination database.
Please follow the instructions in order to run the Python script within the Bold ETL platform.
Python DataFrame into Bold BI