在mongodb环境下可以用find()函数,但是在python环境下用find函数返回的是一个地址呢?请问怎么用能返回数据?
mongodb下是正常的:
> db.posts.find()
{ "_id" : ObjectId("5587bf580e3c5241da958200"), "text" : "my first blog post", "tags" : [ "mongodb", "python", "pymongo" ], "author" : "jim" }
{ "_id" : ObjectId("5587c04d0e3c5241da958201"), "text" : "my second posts", "author" : "mike" }
而在python下find_one可以,find()不行:
>>> import pymongo
>>> from pymongo import MongoClient
>>> client = MongoClient()
>>> client = MongoClient('localhost',27017)
>>> db = client.testdel
>>> mycol = db.mycol
>>> mycol.find_one()
{u'description': u'MongoDB is no sql database', u'tags': [u'mongodb', u'database', u'NoSQL'], u'url': u'http://www.yiibai.com', u'title': u'MongoDB Overview', u'likes': 100.0, u'_id': ObjectId('5584b5a183a7c7ad13947748'), u'by': u'tutorials point'}
>>> for post in mycol.find()
File "<stdin>", line 1
for post in mycol.find()
^
SyntaxError: invalid syntax
>>> for post in db.mycol.find()
File "<stdin>", line 1
for post in db.mycol.find()
^
SyntaxError: invalid syntax
>>> mycol.find()
<pymongo.cursor.Cursor object at 0xb6d7bc4c>
把错误也一块写进去了,希望不会让大家感觉混乱.为什么find_one()可以正常使用而find()就不行呢?希望老师不吝赐教.