簡體   English   中英

使用python訪問mongodb數據庫

[英]Access mongodb database with python

我正在學習使用python / mongo / bootstrap制作Web應用程序的在線課程。

我使用默認設置安裝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中運行mongo

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)

但是解釋器不返回任何內容。 沒有錯誤。 它只是什么都不返回。 控制台甚至不返回游標轉儲。 我究竟做錯了什么?

我不確定以下哪個步驟可以真正解決問題。 但是,這是我為使Python控制台最終與mongod對話所做的工作

-卸載python
-卸載pycharm
-卸載mongo
-刪除python環境變量

-重新安裝一切

現在可以了

更新mongodb之后,我遇到了同樣的問題。 升級pymongo解決了這個問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM