2016-06-21 47 views
20

我想要一個集合中的所有文檔編寫一個函數來MongoDB中返回包含在mycollection的所有文件獲取使用Pymongo

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) 

然而,在函數返回:Process finished with exit code 0

+0

你有沒有花括號在查找方法嘗試?嘗試光標= db.mycollection.find() – YOBA

+0

結果相同: 過程完成退出代碼0 – MAYA

+0

好吧,你怎麼執行這個腳本? (還請調整縮進) – YOBA

回答

20

下面是示例代碼當您從命令提示符運行時,它工作正常。

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) 

請檢查集合名稱。

+1

這是OP已經在做的事情。 – styvane

+1

它與我所做的一樣 – MAYA

+0

我粘貼了上面的代碼,以確認代碼中沒有問題。這與當地的環境有關。另外,我提到它可以在命令提示符下正常工作。 – notionquest