mongodb - PyMongo not recognizing if the name of the collection is client -
i have cursor open on database , have collection name client
in database. when i'm trying execute query like
def connect_to_primary(host, port): """this function connect primary host in replicaset""" connection = mongoclient(str(host), int(port)) admindb = connection.admin pri_host_port_con = admindb.command("ismaster") primary_con = pri_host_port_con['primary'] pri_host_port = primary_con.replace(':', ' ').split() pri_host = pri_host_port[0] pri_port = pri_host_port[1] final_connection = mongoclient(str(pri_host), int(pri_port)) return final_connection connect_to_primary(host,port)[db].client.find({}).distinct('clientid')
i getting following error:
file "/usr/lib64/python2.7/site-packages/pymongo/database.py", line 1116, in __call__ self.__name, self.__client.__class__.__name__)) typeerror: 'database' object not callable. if meant call 'find' method on 'mongoclient' object failing because no such method exists.
what's wrong here?
the reason getting because database
instances have client
attribute returns client instance of database shown in following interactive python session.
>>> import pymongo >>> connection = pymongo.mongoclient() >>> db = connection["test"] >>> db database(mongoclient(host=['localhost:27017'], document_class=dict, tz_aware=false, connect=true), 'test') >>> db.client mongoclient(host=['localhost:27017'], document_class=dict, tz_aware=false, connect=true)
the solution use the get_collection
method or []
operator.
>>> db["client"] collection(database(mongoclient(host=['localhost:27017'], document_class=dict, tz_aware=false, connect=true), 'test'), 'client') >>> db.get_collection("client") collection(database(mongoclient(host=['localhost:27017'], document_class=dict, tz_aware=false, connect=true), 'test'), 'client')
Comments
Post a Comment