0

Hi I have a database name:Tmobile and it has a collection with two files Mynew and 30dc..

See image

Database image

Now i am trying to loop through the files and print any element /file from it.

Below is my code:

import pandas as pd
import json
from pandas.io.json import json_normalize
from unittest.mock import inplace
from pymongo import MongoClient
from idlelib.rpc import response_queue
from pandas import DataFrame

connection = MongoClient('localhost', 27017)
db=connection.Tmobile
collection=db.Collections
#print(collection)
for collection1 in collection.find():
    print(collection1)

But when i try this i get the below error:

Traceback (most recent call last):
  File "C:\Users\esrilka\eclipse-workspace\My First PyDev Project\dbsample.py", line 13, in <module>
['30dc6d110c7a0d3d371177ac0a3624bc_1', 'Mynew']
    for collection in collection.find():
AttributeError: 'list' object has no attribute 'find'
sri lakshmi
  • 47
  • 1
  • 9
  • What are you trying to achieve my calling a method that doesn't exist? – cs95 May 15 '18 at 04:46
  • hi i would like to load/read the json file present inside the collection.can i know how to achieve that – sri lakshmi May 15 '18 at 04:58
  • What is `collection.find()`? Where is the documentation for this? – cs95 May 15 '18 at 05:00
  • I just tried that..in that way.Not sure if its correc.Can you suggest how to acheive that – sri lakshmi May 15 '18 at 05:09
  • With `db.collection_name` you get the mongodb collection with the collection_name, where as with `db.collection_names` you get a list of all collection names. So, you know the difference, the list will not have a find method. You may just try using `db.Collections` that should return you the collection with the name `Collections` from your example. – prabhakar May 15 '18 at 05:09
  • https://stackoverflow.com/questions/24985684/mongodb-show-all-contents-from-all-collections – sri lakshmi May 15 '18 at 05:10
  • Yes, your collection_name is `Collections`, so you need to use `collection=db.Collections` in stead of `collection=db.collection_names(include_system_collections=False)` – prabhakar May 15 '18 at 05:13
  • i chnaged it but what i get is "Collection(Database(MongoClient(host=['localhost:27017'], document_class=dict, tz_aware=False, connect=True), 'Tmobile'), 'Collections')" – sri lakshmi May 15 '18 at 05:18
  • Bu ti am trying to print the elements inside the collections – sri lakshmi May 15 '18 at 05:19
  • What is your output with the second print statement in for loop? Do you get any error? – prabhakar May 15 '18 at 05:27
  • Dint get any output for second print statement.Its blank – sri lakshmi May 15 '18 at 05:40
  • Hope you undertand my question.i would like to print the complete json say "Mynew" which is present inside Collection.However i dont want to mention the name,it should be kind of iterative – sri lakshmi May 15 '18 at 05:40
  • It should print all documents inside `Collections`, I am not sure what is the issue. Please update your edits to the post here, so that I can see what is wrong – prabhakar May 15 '18 at 05:48
  • Updated the code..please check – sri lakshmi May 15 '18 at 05:58
  • OK, Please `print(collection.count())` before the for loop and see what you get. – prabhakar May 15 '18 at 06:00
  • I get the output as 0 – sri lakshmi May 15 '18 at 06:09
  • :), ha, no reason why the second print statement prints empty, that means you dont have the json documents as claimed in your example. Go, check your database.. – prabhakar May 15 '18 at 06:11
  • did you check the mongo database with mongo console? – prabhakar May 15 '18 at 07:23
  • Hi Finally,got the result with the below code – sri lakshmi May 15 '18 at 09:13
  • connection = MongoClient('localhost', 27017) db=connection.Tmobile collection=db.TmobileCollection temp_data=pd.DataFrame() for item in collection.find(): temp_creationdate =pd.DataFrame({"creation-date": [item["creation-date"]]}) – sri lakshmi May 15 '18 at 09:13

0 Answers0