Amrut Chougale
Amrut Chougale

Reputation: 11

Not able to get all records in mongoDB using pymongo

I am having a program which uses pymongo to get the records in the mongoDB. But, when I find records with a query I am getting blank cursor with no records.

Below is a code :

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

A program which accesses this class:

runmongo.py

from MongoDBConnection import MongoDBConnection

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

The Output I am getting is a cursor object with no records in it.

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

Manually tried the same and I am getting proper output

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

Can someone let me know, why am I not able to get the desired records from DB.

Upvotes: 1

Views: 737

Answers (1)

Abhijeet Kasurde
Abhijeet Kasurde

Reputation: 4127

The problem is in accessing database and collection line itself.

When you try to access database and collection using attribute style like

retDb = connection.db

it assumes that 'db' is name of database instead of 'test' in your example.

Using dictionary style access will solve your problem something like this

retDb = connection[db]

Code goes like this :

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

Upvotes: 1

Related Questions