1

I'm trying to use PyMongo to select the most recent 6 entires in a DB sorted by Unix time. I want to avoid having to use cursors, so have been using the parameters in the find() method. The desired result in a Mongo query is below:

db.pings.find().sort({ serverTime: 1 }).limit(6)

In PyMongo, I'm doing the below:

pings = pings.find({'sessionId':sessionId}, sort=[('serverTime', pymongo.DESCENDING)], limit=6)

I've seen similar questions (such as this: pymongo sort() limit() different?) which don't specifically corespond to PyMongo. Please can someone confirm to me that the above will sort the results before it selects the top 6, rather than selecting the top 6 and then sorting?

Thanks,

Sam

Community
  • 1
  • 1
Sam Heather
  • 1,493
  • 3
  • 19
  • 42
  • 1
    It should select the first 6 matching results, ordered by server time, as opposed to selecting 6 results and sorting those by server time. Are you seeing different behavior? – wdberkeley Oct 15 '14 at 18:50
  • @wdberkeley I'm not seeing different behaviour, and I did test, but I just wanted to check this was defined behaviour as opposed to something that is undefined (ie it works now as the result of something else, but may change without warning). Thanks! – Sam Heather Oct 15 '14 at 22:44

1 Answers1

0

it would sort and then select the top 6

ryh
  • 221
  • 1
  • 6