pymongo find query returns empty/partial cursor in high load

266 views
Skip to first unread message

Shahin Azad

unread,
Oct 21, 2015, 9:39:52 AM10/21/15
to mongodb-user
We have an extensive use of `mongodb` together with `django` using `pymongo` engine.

The way we use `pymongo` in our views is something like this:

    from pymongo import MongoClient
    con = MongoClient('URI')
    collection = con['somedoc']['somecol']

    @cache_page(some_time)
    def my_view(request):
        key = request.POST.get('key')
        query = collection.find({'key': key}, limit=24)
        res = [app for app in query]
        return JsonResponse({'list': res})

Most of the time this view will return desired response, but in some rare situations (high load hours), the `res` list will be empty. In this situations `query.count()` will return `0` or something less than 24, which we absolutely sure there is far more documents in that collection at the time.

I tried to set `allow_partial_results` to `False` explicitly, but with no change this thing will happen again. I also `indexed` the `key` in database to get a better performance, it reduced times of happening this situations, but it is still happening some times.

We also have another application written in `Node` using `mongoose` engine on the same `mongo` database, but we are not seeing this behavior at all.

we are on `python 3`, `django 1.8` and `pymongo 3.0.3`.

What are we doing wrong here? What can cause this problem?

Dwight Merriman

unread,
Oct 21, 2015, 10:10:05 AM10/21/15
to mongodb-user
i don't know much about pymongo driver but i would suggest the following as general troubleshooting

- sounds like based on the mongoose app you know the data is really there, but you could confirm that with a 4 line mongo shell script that does a test query once a minute or something and logs # returned.

- if you sometimes get > 0 and < 24, and there really is >= 24 in the db, my first guess is that the connection to the db reset mid-query / mid-cursor.  i would take the timestamp when this happens from your app and check logs, and look for connection closes/opens, on both sides but i believe they are logged on the server for sure.

Shahin Azad

unread,
Oct 21, 2015, 3:52:49 PM10/21/15
to mongodb-user
Thank you for your suggestions. I'll give them a try asap.

But so far this is what I've tested:

Today I removed other MongoClient definitions from all python code (which problem happens in) and there is just one client now which all read/write processes will use it. I wanted to make sure for any reason this is not a locking problem.

I also sat maxPoolSize to None in MongoClient definition.


Now happening of the problem is much less, but it happens again sometimes.


Also there are other services which will read/write to this mongo instance and there is no replica running.


Thank you.
Reply all
Reply to author
Forward
0 new messages