[英]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.