2016-02-28 71 views
0

我正在學習使用python/mongo/bootstrap製作web應用程序的在線課程。使用python訪問mongodb數據庫

我使用默認設置安裝MongoDB的

我在PowerShell中運行的mongod從安裝目錄

C:\Program Files\MongoDB\Server\3.2\bin>mongod 
2016-02-27T23:31:14.684-0500 I CONTROL [initandlisten] MongoDB starting : pid=2456 port=27017 dbpath=C:\data\db\ 64-bit host=DESKTOP-8LMCN7R 
2016-02-27T23:31:14.686-0500 I CONTROL [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2 
2016-02-27T23:31:14.687-0500 I CONTROL [initandlisten] db version v3.2.3 
2016-02-27T23:31:14.689-0500 I CONTROL [initandlisten] git version: b326ba837cf6f49d65c2f85e1b70f6f31ece7937 
2016-02-27T23:31:14.690-0500 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1p-fips 9 Jul 2015 
2016-02-27T23:31:14.692-0500 I CONTROL [initandlisten] allocator: tcmalloc 
2016-02-27T23:31:14.693-0500 I CONTROL [initandlisten] modules: none 
2016-02-27T23:31:14.704-0500 I CONTROL [initandlisten] build environment: 
2016-02-27T23:31:14.706-0500 I CONTROL [initandlisten]  distmod: 2008plus-ssl 
2016-02-27T23:31:14.707-0500 I CONTROL [initandlisten]  distarch: x86_64 
2016-02-27T23:31:14.708-0500 I CONTROL [initandlisten]  target_arch: x86_64 
2016-02-27T23:31:14.709-0500 I CONTROL [initandlisten] options: {} 
2016-02-27T23:31:14.712-0500 I -  [initandlisten] Detected data files in C:\data\db\ created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'. 
2016-02-27T23:31:14.715-0500 W -  [initandlisten] Detected unclean shutdown - C:\data\db\mongod.lock is not empty. 
2016-02-27T23:31:14.718-0500 W STORAGE [initandlisten] Recovering data from the last clean checkpoint. 
2016-02-27T23:31:14.720-0500 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=4G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0), 
2016-02-27T23:31:14.954-0500 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker 
2016-02-27T23:31:14.954-0500 I FTDC  [initandlisten] Initializing full-time diagnostic data capture with directory 'C:/data/db/diagnostic.data' 
2016-02-27T23:31:14.962-0500 I NETWORK [initandlisten] waiting for connections on port 27017 
2016-02-27T23:31:15.004-0500 I FTDC  [ftdc] Unclean full-time diagnostic data capture shutdown detected, found interim file, some metrics may have been lost. OK 
2016-02-27T23:32:10.255-0500 I NETWORK [initandlisten] connection accepted from 127.0.0.1:50834 #1 (1 connection now open) 

我從不同的PowerShell中運行蒙戈安裝目錄

C:\Program Files\MongoDB\Server\3.2\bin>mongo 
MongoDB shell version: 3.2.3 
connecting to: test 

我確認自己在我的分數是一些值

> show dbs 
fullstack 0.000GB 
local  0.000GB 
> use fullstack 
switched to db fullstack 
> show collections 
students 
> db.students.find({}) 
{ "_id" : ObjectId("56d1b951d14d4af940b77a14"), "name" : "Jose", "mark" : "99" } 
{ "_id" : ObjectId("56d211e8d14d4af940b77a16"), "name" : "Jose", "Mark" : 99 } 
{ "_id" : ObjectId("56d26bedc83f1574076c732b"), "name" : "Jose", "mark" : 99 } 
{ "_id" : ObjectId("56d26c0fc83f1574076c732c"), "name" : "Kris", "mark" : 69 } 
{ "_id" : ObjectId("56d271b1c83f1574076c732d"), "name" : "Kelly", "Grade" : 99 } 

我pyCharm運行此代碼:

import pymongo 

uri="mongodb://127.0.0.1:27017" 
client = pymongo.MongoClient(uri) 
database = client['fullstack'] 
collection = database['students'] 
students = collection.find({}) 
for student in students: 
    print(students) 

但解釋沒有返回。沒有錯誤。它只是不返回任何東西。控制檯甚至不會返回遊標轉儲。我究竟做錯了什麼?

+2

你可以保留[閱讀教程](http://api.mongodb.org/python/current/tutorial.html)(如果你搜索的話應該很容易找到)。 '.find()'的結果是一個「光標」。你需要迭代它,而不是隻是轉儲它。 –

+0

可能的重複[爲什麼當我試圖通過pymongo查詢我的mongodb數據庫時,得到一個pymongo.cursor.Cursor?](http://stackoverflow.com/questions/17489789/why-do-i-get-a-pymongo -cursor-cursor-when-trying-to-query-my-mongodb-db-via-pymo) –

+0

感謝您的鏈接,生病檢查出來。糾正我,如果我錯了,但不應該打印光標的控制檯? –

回答

0

我不確定以下哪個步驟實際上解決了問題。但這裏是我做過什麼讓Python的控制檯終於說話的mongod

-Uninstall蟒蛇
-uninstall pycharm
-uninstall MONGO
-remove Python環境變量

-reinstalled一切

現在它可以工作

0

我更新mongodb後有同樣的問題。升級pymongo修復了問題