无法使用pymongo获取mongoDB中的所有记录

时间:2015-08-22 07:44:20

标签: mongodb python-2.7 pymongo

我正在使用pymongo来获取mongoDB中的记录。 但是,当我找到带有查询的记录时,我得到的是没有记录的空白光标。

以下是代码:

MongoDBConnection.py

import bottle
import pymongo
from pymongo import MongoClient

class MongoDBConnection():
    def __init__(self):
        pass
    def getConnection(self,host):
        try:
            retConnection = MongoClient(host)
        except Exception  as e:
            print 'Exception occurred while creating connection with mongoDB, value: \n '
            print e
        return retConnection

    def showAllRecords(self,host,db,collection):
        #pdb.set_trace()
        hconn = self.getConnection(host)
        hdb = self.getDBDetails(hconn,db)
        hcoll = self.getCollectionDetails(hdb,collection)

        #get all the details about the collection
        query = {"name" : "SP"}
        try:
            cursor = hcoll.find(query)
        except Exception as e:
            print "Unexpected error:", type(e), e
        return cursor

    def getDBDetails(self,connection,db):
        # create a database connection
        try :
            retDb = connection.db
        except Exception as e:
            print 'Exception occurred while connecting to database %s, value: \n ' % db
            print e
        return retDb

    def getCollectionDetails(self,db,collection):
        #create a handle for collection
        try :
            retCollection = db.collection
        except Exception as e:
            print 'Exception occurred while getting details for  collection:  %s, value: \n ' % collection
            print e
        return retCollection

访问此课程的程序:

runmongo.py

from MongoDBConnection import MongoDBConnection

a = MongoDBConnection()
test = a.showAllRecords("mongodb://localhost","test","documents")
print test
for i in test:
    print i

我得到的输出是一个没有记录的游标对象。

[root@localhost tmp]# python runmongo.py
<pymongo.cursor.Cursor object at 0x288cd90>
[root@localhost tmp]#

手动尝试相同,我得到正确的输出

> use test
switched to db test
> db.documents.find({'name':'SP'})
{ "_id" : "doc2", "name" : "SP" }
>

有人可以告诉我,为什么我无法从DB获得所需的记录。

1 个答案:

答案 0 :(得分:1)

问题在于访问数据库和集合行本身。

当您尝试使用

等属性样式访问数据库和集合时
retDb = connection.db

它假设'db'是数据库的名称,而不是示例中的'test'。

使用字典样式访问将解决您的问题

retDb = connection[db]

代码是这样的:

import pymongo
from pymongo import MongoClient

class MongoDBConnection():
    def __init__(self):
        pass

    def getConnection(self,host):
        try:
            retConnection = MongoClient(host)
        except Exception  as e:
            print 'Exception occurred while creating connection with mongoDB, value: \n '
            print e
        return retConnection

    def showAllRecords(self,host,db,collection):
        #pdb.set_trace()
        hconn = self.getConnection(host)
        hdb = self.getDBDetails(hconn,'test')
        hcoll = self.getCollectionDetails(hdb,'documents')

        #get all the details about the collection
        query = {"name" : "SP"}
        try:
            cursor = hcoll.find(query)
        except Exception as e:
            print "Unexpected error:", type(e), e
        return cursor

    def getDBDetails(self,connection,db):
        # create a database connection
        try :
            retDb = connection[db] # NOTE : dictionary style access
        except Exception as e:
            print 'Exception occurred while connecting to database %s, value: \n ' % db
            print e
        return retDb

    def getCollectionDetails(self,db,collection):
        #create a handle for collection
        try :
            retCollection = db[collection] # NOTE : dictionary style access
        except Exception as e:
            print 'Exception occurred while getting details for  collection:  %s, value: \n ' % collection
            print e
        return retCollection