Forum Discussion

sharath123's avatar
sharath123
Helper I
4 years ago
Solved

MongoDB Data is not showing in powerbi by using python script

import pymongo
from pymongo import MongoClient
connection = pymongo.MongoClient("mongodb+srv://<username>:<password>@cluster0.zev7c.mongodb.net/myFirstDatabase?retryWrites=true&w=majority")


db=connection['study']
collection=db['college']

one = collection.find_one({'studentName':"Gopi"})
print(one)


when I run this code in python showing the result


{'_id': ObjectId('6174eb2d3d927d29a8d0b271'), 'studentid': 2, 'studentName': 'Gopi', 'Branch': 'Electrical', 'joinedyear': 2018, 'Hostel': 'A2'}


but when I run the same code in Powerbi Data is not showing.


  • loch45's avatar
    loch45
    3 years ago

    This seemed to work:
    # Specify the collection name
    collection = db[
    'your_collection_name']

    # Perform the query
    query = {}  
    # Your query conditions here
    result = collection.find(query)

    # Convert the query result to a DataFrame
    df = pd.DataFrame(result)

    # Print the DataFrame
    print(df)

3 Replies

  • Hey sharath123 ,

     

    make sure that the result that you want to use inside PowerBI is a dataframe, I assume you can "convert" your JSON object using Pandas.

     

    Hopefully, this provides what you are looking for.

     

    Regards,

    Tom

    • sharath123's avatar
      sharath123
      Helper I

      TomMartens thanks. but how to convert this result into a data frame?
      please provide the syntax to convert the result into a data frame, so that I run the script in powerbi. 

      • loch45's avatar
        loch45
        New Member

        This seemed to work:
        # Specify the collection name
        collection = db[
        'your_collection_name']

        # Perform the query
        query = {}  
        # Your query conditions here
        result = collection.find(query)

        # Convert the query result to a DataFrame
        df = pd.DataFrame(result)

        # Print the DataFrame
        print(df)