mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 20:23:06 +00:00
@@ -7,6 +7,7 @@ local md5 = require "md5"
|
|||||||
local crypt = require "crypt"
|
local crypt = require "crypt"
|
||||||
local rawget = rawget
|
local rawget = rawget
|
||||||
local assert = assert
|
local assert = assert
|
||||||
|
local table = table
|
||||||
|
|
||||||
local bson_encode = bson.encode
|
local bson_encode = bson.encode
|
||||||
local bson_encode_order = bson.encode_order
|
local bson_encode_order = bson.encode_order
|
||||||
@@ -395,8 +396,19 @@ function mongo_collection:find(query, selector)
|
|||||||
} , cursor_meta)
|
} , cursor_meta)
|
||||||
end
|
end
|
||||||
|
|
||||||
function mongo_cursor:sort(key_list)
|
-- cursor:sort { key = 1 } or cursor:sort( {key1 = 1}, {key2 = -1})
|
||||||
self.__sortquery = bson_encode {['$query'] = self.__query, ['$orderby'] = key_list}
|
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
|
||||||
|
key = bson_encode_order(table.unpack(key_list))
|
||||||
|
end
|
||||||
|
self.__sortquery = bson_encode {['$query'] = self.__query, ['$orderby'] = key}
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -38,24 +38,27 @@ function test_find_and_remove()
|
|||||||
db[db_name].testdb:dropIndex("*")
|
db[db_name].testdb:dropIndex("*")
|
||||||
db[db_name].testdb:drop()
|
db[db_name].testdb:drop()
|
||||||
|
|
||||||
db[db_name].testdb:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index"})
|
db[db_name].testdb:ensureIndex({test_key = 1}, {test_key2 = -1}, {unique = true, name = "test_index"})
|
||||||
|
|
||||||
local ret = db[db_name].testdb:safe_insert({test_key = 1})
|
local ret = db[db_name].testdb:safe_insert({test_key = 1, test_key2 = 1})
|
||||||
assert(ret and ret.n == 1)
|
assert(ret and ret.n == 1)
|
||||||
|
|
||||||
local ret = db[db_name].testdb:safe_insert({test_key = 2})
|
local ret = db[db_name].testdb:safe_insert({test_key = 1, test_key2 = 2})
|
||||||
assert(ret and ret.n == 1)
|
assert(ret and ret.n == 1)
|
||||||
|
|
||||||
local ret = db[db_name].testdb:findOne({test_key = 1})
|
local ret = db[db_name].testdb:safe_insert({test_key = 2, test_key2 = 3})
|
||||||
assert(ret and ret.test_key == 1)
|
assert(ret and ret.n == 1)
|
||||||
|
|
||||||
local ret = db[db_name].testdb:find({test_key = {['$gt'] = 0}}):sort({test_key = -1}):skip(1):limit(1)
|
local ret = db[db_name].testdb:findOne({test_key2 = 1})
|
||||||
assert(ret:count() == 2)
|
assert(ret and ret.test_key2 == 1)
|
||||||
|
|
||||||
|
local ret = db[db_name].testdb:find({test_key2 = {['$gt'] = 0}}):sort({test_key = 1}, {test_key2 = -1}):skip(1):limit(1)
|
||||||
|
assert(ret:count() == 3)
|
||||||
assert(ret:count(true) == 1)
|
assert(ret:count(true) == 1)
|
||||||
if ret:hasNext() then
|
if ret:hasNext() then
|
||||||
ret = ret:next()
|
ret = ret:next()
|
||||||
end
|
end
|
||||||
assert(ret and ret.test_key == 1)
|
assert(ret and ret.test_key2 == 1)
|
||||||
|
|
||||||
db[db_name].testdb:delete({test_key = 1})
|
db[db_name].testdb:delete({test_key = 1})
|
||||||
db[db_name].testdb:delete({test_key = 2})
|
db[db_name].testdb:delete({test_key = 2})
|
||||||
|
|||||||
Reference in New Issue
Block a user