Pymongo Cursor to Dataframe taking long time

350 Views Asked by At

I am using Mongodb Database to store data, and while I query data from the DB, it gives the output in 'pymongo cursor' type, which I need to convert to Dataframe. However, I have tried 2 methods, but they are taking 2-4 seconds to do it(even for as less as 1 record). Is there a way of faster conversion to dataframe?

import pymongo
import pandas as pd
import numpy as np
from pymongo import MongoClient

client=MongoClient("Server Connection String", username = 'username', password='password')

db=client["DB_Name"]
collection=db['Table_Name']

data=collection.find({}).limit(1)

#Method 1 of Conversion of pymongo cursor to dataframe

my_data = list(map(lambda x: list(x.values()), data))

result = np.array(my_data)

df = pd.DataFrame(result)

#Method 2 of Conversion of pymongo cursor to dataframe
df=pd.DataFrame(list(data))
0

There are 0 best solutions below