-1

I am trying to read the data from MongoDB compass by using Python. But for me always gives only one record can anyone please suggest the actual code?. I am using the mycollection.find() command to get the record

giving only one record off information . my db has nearly 23k rcords. exception: all records from DB

  • thanks for reponse .. i am using the same.. but only one record is getting...i am using pymongo – Rama Devi May 29 '23 at 08:43
  • Could you share the code on how you're trying to fetch the records? – mutantkeyboard May 29 '23 at 08:53
  • ymongo.MongoClient("mongodb+srv://CMPUser:SpYjYAybhDGafKlm@coreqa-pl-0.h8krr.mongodb.net") # Database Name db = client["ConsentDB"] # Collection Name mycollection = db["Consents"] all_records=mycollection.find() print(all_records) for row in all_records: print(row) – Rama Devi Jun 01 '23 at 07:01
  • Please edit your question to include a [mcve] – Andrey Bienkowski Jun 01 '23 at 09:22

1 Answers1

0

What do you mean?

You want to get all the records and do something with them?

In that case, you can simply do something like this:

collection = mycollection.find()

#iterate through the data

for data in collection:
    #do something with data
    print(data)

p/s: Assuming you're using PyMongo.

If you want to read database in batches of let's say 1000 records, yo can use something similar to this solution -> https://stackoverflow.com/a/75813785/1737811

Hopefully this answers your question.

mutantkeyboard
  • 1,614
  • 1
  • 16
  • 44
  • import pymongo import pandas as pd client = pymongo.MongoClient("mongodb+srv://CMPUser:SpYjYAybhDGafKlm@coreqa-pl-0.h8krr.mongodb.net") # Database Name db = client["ConsentDB"] # Collection Name mycollection = db["Consents"] all_records=mycollection.find() print(all_records) for row in all_records: print(row) – Rama Devi May 29 '23 at 08:46