90

I want to write a function to return all the documents contained in mycollection in mongodb

from pymongo import MongoClient

if __name__ == '__main__':
    client = MongoClient("localhost", 27017, maxPoolSize=50)
    db=client.mydatabase
    collection=db['mycollection']
    cursor = collection.find({})
    for document in cursor:
        print(document)

However, the function returns: Process finished with exit code 0

Arpit Solanki
  • 9,567
  • 3
  • 41
  • 57
MAYA
  • 1,243
  • 1
  • 12
  • 20
  • have you tried without braces in the find method? try cursor = db.mycollection.find() – YOBA Jun 21 '16 at 10:19
  • the same result : Process finished with exit code 0 – MAYA Jun 21 '16 at 10:39
  • Ok, how are you executing this script? (Also please adjust the indentation) – YOBA Jun 21 '16 at 10:43
  • I'm using Pycharm 2.0.3 – MAYA Jun 21 '16 at 10:45
  • Ok this is a pycharm specific usage problem because your code is fine. Also check that you run directly your script as the main one, since you have \__name\__ == '\__main\__' in your code, try using a console to check the connectivity with the database. – YOBA Jun 21 '16 at 10:52
  • I checked the connectivity and It's OK. SO, I could print all collection of my database – MAYA Jun 21 '16 at 10:55
  • Yes, you can print all the documents in the collection. – notionquest Jun 21 '16 at 10:57
  • how I can do this? It returns the same output : Process finished with exit code 0 – MAYA Jun 21 '16 at 11:00
  • I tried the same code as OP posted in my Linux machine with different db and collection. It worked well for me. – Girish Gupta Jun 21 '16 at 11:23

4 Answers4

103

Here is the sample code which works fine when you run from command prompt.

from pymongo import MongoClient

if __name__ == '__main__':
    client = MongoClient("localhost", 27017, maxPoolSize=50)
    db = client.localhost
    collection = db['chain']
    cursor = collection.find({})
    for document in cursor:
          print(document)

Please check the collection name.

razieh babaee
  • 298
  • 5
  • 19
notionquest
  • 37,595
  • 6
  • 111
  • 105
61

pymongo creates a cursor. Hence you'll get the object 'under' the cursor. To get all objects in general try:

list(db.collection.find({}))

This will force the cursor to iterate over each object and put it in a list()

Have fun...

geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
12

I think this will work fine in your program.

cursor = db.mycollection # choosing the collection you need

for document in cursor.find():
    print (document)
Gautam Savaliya
  • 1,403
  • 2
  • 20
  • 31
Isuru Maldeniya
  • 131
  • 1
  • 2
  • 3
    Here `cursor = db.mycollection` do not name variable as cursor it would ideal to name as `collection`, Just not to confuse as `cursor.find():` should be `collection.find():` which returns a `cursor` that can be iterated over to get the values out. – whoami - fakeFaceTrueSoul May 11 '20 at 15:31
0

it works fine for me,try checking the exact database name and collection name. and try changing from db=client.mydatabase to db=client['mydatabase'] .

If your database name is such that using attribute style access won’t work (like test-database), you can use dictionary style access instead. source !

Doddi girish
  • 75
  • 1
  • 7