mongo.sort

This commit is contained in:
Cloud Wu
2017-04-20 18:37:27 +08:00
parent ad36be1bd7
commit dab5a4e308
4 changed files with 22 additions and 11 deletions

View File

@@ -396,16 +396,21 @@ function mongo_collection:find(query, selector)
} , cursor_meta)
end
local function unfold(list, key, ...)
if key == nil then
return list
end
local next_func, t = pairs(key)
local k, v = next_func(t) -- The first key pair
table.insert(list, k)
table.insert(list, v)
return unfold(list, ...)
end
-- cursor:sort { key = 1 } or cursor:sort( {key1 = 1}, {key2 = -1})
function mongo_cursor:sort(key, key_v, ...)
if key_v then
local key_list = {}
for _, kp in ipairs {key, key_v, ...} do
local next_func, t = pairs(kp)
local k, v = next_func(t, v) -- The first key pair
table.insert(key_list, k)
table.insert(key_list, v)
end
local key_list = unfold({}, key, key_v , ...)
key = bson_encode_order(table.unpack(key_list))
end
self.__sortquery = bson_encode {['$query'] = self.__query, ['$orderby'] = key}

View File

@@ -158,7 +158,7 @@ local function dispatch_by_order(self)
local func, co = pop_response(self)
if not co then
-- close signal
wakeup_all(self, errmsg)
wakeup_all(self, "channel_closed")
break
end
local ok, result_ok, result_data, padding = pcall(func, self.__sock)