1
votes

I'm having a strange issue with Pymongo's find_one function. I have a db called "cluster_db" hosted on my local machine. It has a collection called 'clusters'. When I run the query in the mongo shell I get following output.

> db
cluster_db
> db.clusters.findOne({_id:-8488068664808428000})
{
    "_id" : NumberLong("-8488068664808427924"),
    "members" : [
        {
            "participationCoeff" : 1,
            "tweetID" : NumberLong("-8488068664808427924")
        }
    ]
}
> 

Now, during my code initialization phase I have a constant defined in a module 'dbutil' like this:

DB_CONNECTION           = MongoClient('localhost', 27017)
CLUSTER_DB_HANDLE       = DB_CONNECTION['cluster_db']

After this, within a function, I'm making following call.

dbutil.CLUSTER_DB_HANDLE.clusters.find_one({'_id':clusterID})

However, the above call always returns 'None'. If I go to MongoShell and run the exact same query with same clusterID, I see the result.

I know that it is a strange error, but somehow I'm not able to figure out as to why this is happening. Everywhere else, I'm being able to successfully make calls to 'clusters' collection in cluster_db using dbutil.CLUSTER_DB_HANDLE.clusters

2
In the shell, does the _id of the returned document really not match the _id in the findOne? - JohnnyHK
Yes! The problem is that _id is stored as NumberLong() but still I'm converting my clusterID to long using python 'long' method before I use find_one (). But still It's not working! - VaidAbhishek
So what are you specifically trying to do in Python that's not working? - JohnnyHK

2 Answers

3
votes

Just came across this myself and it turns out that the id I passed in is int type and I had to convert it by calling int(id), e.g:

db.Employees.find_one({'id' : int(id)}) // this works
db.Employees.find_one({'id' : id}) //this doesn't work

Hope this helps someone.

0
votes

is clusterID == -8488068664808428000 ? why don't you try CLUSTER_DB_HANDLE.clusters.find_one({'_id':-8488068664808428000}) ?