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)

View File

@@ -12,7 +12,6 @@ end
local function console_main_loop()
local stdin = socket.stdin()
socket.lock(stdin)
while true do
local cmdline = socket.readline(stdin, "\n")
local split = split_cmdline(cmdline)
@@ -23,7 +22,6 @@ local function console_main_loop()
pcall(skynet.newservice, cmdline)
end
end
socket.unlock(stdin)
end
skynet.start(function()

View File

@@ -7,10 +7,18 @@ local host, db_name = ...
function test_insert_without_index()
local db = mongo.client({host = host})
local r = db:runCommand("buildInfo",1)
for k, v in pairs(r) do
print(k,v)
end
db[db_name].testdb:dropIndex("*")
db[db_name].testdb:drop()
local ret = db[db_name].testdb:safe_insert({test_key = 1});
for k,v in pairs(ret) do
print(k,v)
end
assert(ret and ret.n == 1)
local ret = db[db_name].testdb:safe_insert({test_key = 1});
@@ -85,7 +93,7 @@ function test_expire_index()
for i = 1, 1000 do
skynet.sleep(11);
local ret = db[db_name].testdb:findOne({test_key = 1})
if ret == nil then
return