Forums
How to read a MongoDB collection using python in pipeline? - Printable Version

+- Forums (https://bdn.bdb.ai)
+-- Forum: BDB Knowledge Base (https://bdn.bdb.ai/forumdisplay.php?fid=13)
+--- Forum: BDB Data Pipeline (https://bdn.bdb.ai/forumdisplay.php?fid=48)
+---- Forum: BDB Data Pipeline Q & A (https://bdn.bdb.ai/forumdisplay.php?fid=17)
+---- Thread: How to read a MongoDB collection using python in pipeline? (/showthread.php?tid=345)



How to read a MongoDB collection using python in pipeline? - shreekantgosavi - 12-21-2022

Use python script component.
Under meta information paste below script in script section.
Provide required credentials.
import pandas as pd
from pymongo import MongoClient
def myread():
    client = MongoClient("mongodb://localhost:27017/"#connection_string
    mydatabase = client.db_name#cdatabase_name
    mycollection = mydatabase.collection_name #collection_name
    cursor = mycollection.aggregate([ { '$project': { '_id': 0}}]) #aggregate_query
    df = pd.DataFrame(list(cursor))
    client.close()
    return df
myread()