【发布时间】:2016-06-11 06:32:01
【问题描述】:
我正在学习使用 python/mongo/bootstrap 制作网络应用程序的在线课程。
我使用默认设置安装 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)
但是解释器什么也不返回。没有错误。它只是不返回任何东西。控制台甚至不返回游标转储。我究竟做错了什么?
【问题讨论】:
-
你可以保留reading the tutorial(如果你搜索的话应该很容易找到)。
.find()的结果是一个“光标”。您需要对其进行迭代,而不仅仅是转储它。 -
感谢您提供的链接,请检查一下。如果我错了,请纠正我,但控制台不应该打印光标吗?
-
你错了。所以更正了。之前也有人问过这个问题。这就是为什么你有重复的链接和伟大的 “这有助于回答我的问题” 的东西,它会提示你做某事。我想知道那是什么?
-
迭代或不迭代不是问题。如果我将: >print(students) 替换为 >for student in students: > print(student) 控制台仍然没有返回任何内容。在做出假设并留下无用的 cmets 之前,请花时间实际提问
标签: mongodb python-3.x mongodb-query pymongo database