Olivine-Labs / resty-mongol

Lua MongoDB driver
MIT License
46 stars 23 forks source link

cannot sort cursor before limit #18

Open keyleo opened 4 years ago

keyleo commented 4 years ago

Let us say, we have 1000 records, and I want to fetch latest 100 records by timestamp.

when i use local r = col:find(criteria,nil,100) r:sort({["timestamp"]=-1})

the cursor return sorted result of first 100 results.

what I want is sort 1000 records first, and then limit 100 records.

sagrada1788 commented 3 years ago

it is a bug. you can modify this file src/cursor.lua in line 42:

function cursor_methods:limit(n) assert(n) self.limit_n = n return limit end

modify

return limit

to

return self

sagrada1788 commented 3 years ago

then use like this

r:sort({["timestamp"]=-1}):limit(100)