mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 20:23:06 +00:00
Merge pull request #244 from learno/lua53
扩展mongo库对sort, skip, limit, count方法的支持
This commit is contained in:
@@ -279,9 +279,45 @@ function mongo_collection:find(query, selector)
|
|||||||
__cursor = nil,
|
__cursor = nil,
|
||||||
__document = {},
|
__document = {},
|
||||||
__flags = 0,
|
__flags = 0,
|
||||||
|
__skip = 0,
|
||||||
|
__sortquery = nil,
|
||||||
|
__limit = 0,
|
||||||
} , cursor_meta)
|
} , cursor_meta)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function mongo_cursor:sort(key_list)
|
||||||
|
self.__sortquery = bson_encode {['$query'] = self.__query, ['$orderby'] = key_list}
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
|
function mongo_cursor:skip(amount)
|
||||||
|
self.__skip = amount
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
|
function mongo_cursor:limit(amount)
|
||||||
|
self.__limit = amount
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
|
function mongo_cursor:count(with_limit_and_skip)
|
||||||
|
local cmd = {
|
||||||
|
'count', self.__collection.name,
|
||||||
|
'query', self.__query,
|
||||||
|
}
|
||||||
|
if with_limit_and_skip then
|
||||||
|
local len = #cmd
|
||||||
|
cmd[len+1] = 'limit'
|
||||||
|
cmd[len+2] = self.__limit
|
||||||
|
cmd[len+3] = 'skip'
|
||||||
|
cmd[len+4] = self.__skip
|
||||||
|
end
|
||||||
|
local ret = self.__collection.database:runCommand(table.unpack(cmd))
|
||||||
|
assert(ret and ret.ok == 1)
|
||||||
|
return ret.n
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
-- collection:createIndex({username = 1}, {unique = true})
|
-- collection:createIndex({username = 1}, {unique = true})
|
||||||
function mongo_collection:createIndex(keys, option)
|
function mongo_collection:createIndex(keys, option)
|
||||||
local name = option.name
|
local name = option.name
|
||||||
@@ -348,10 +384,11 @@ function mongo_cursor:hasNext()
|
|||||||
local sock = conn.__sock
|
local sock = conn.__sock
|
||||||
local pack
|
local pack
|
||||||
if self.__data == nil then
|
if self.__data == nil then
|
||||||
pack = driver.query(request_id, self.__flags, self.__collection.full_name,0,0,self.__query,self.__selector)
|
local query = self.__sortquery or self.__query
|
||||||
|
pack = driver.query(request_id, self.__flags, self.__collection.full_name, self.__skip, -self.__limit, query, self.__selector)
|
||||||
else
|
else
|
||||||
if self.__cursor then
|
if self.__cursor then
|
||||||
pack = driver.more(request_id, self.__collection.full_name,0,self.__cursor)
|
pack = driver.more(request_id, self.__collection.full_name, -self.__limit, self.__cursor)
|
||||||
else
|
else
|
||||||
-- no more
|
-- no more
|
||||||
self.__document = nil
|
self.__document = nil
|
||||||
|
|||||||
@@ -43,10 +43,22 @@ function test_find_and_remove()
|
|||||||
local ret = db[db_name].testdb:safe_insert({test_key = 1})
|
local ret = db[db_name].testdb:safe_insert({test_key = 1})
|
||||||
assert(ret and ret.n == 1)
|
assert(ret and ret.n == 1)
|
||||||
|
|
||||||
|
local ret = db[db_name].testdb:safe_insert({test_key = 2})
|
||||||
|
assert(ret and ret.n == 1)
|
||||||
|
|
||||||
local ret = db[db_name].testdb:findOne({test_key = 1})
|
local ret = db[db_name].testdb:findOne({test_key = 1})
|
||||||
assert(ret and ret.test_key == 1)
|
assert(ret and ret.test_key == 1)
|
||||||
|
|
||||||
|
local ret = db[db_name].testdb:find({test_key = {['$gt'] = 0}}):sort({test_key = -1}):skip(1):limit(1)
|
||||||
|
assert(ret:count() == 2)
|
||||||
|
assert(ret:count(true) == 1)
|
||||||
|
if ret:hasNext() then
|
||||||
|
ret = ret:next()
|
||||||
|
end
|
||||||
|
assert(ret and ret.test_key == 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})
|
||||||
|
|
||||||
local ret = db[db_name].testdb:findOne({test_key = 1})
|
local ret = db[db_name].testdb:findOne({test_key = 1})
|
||||||
assert(ret == nil)
|
assert(ret == nil)
|
||||||
|
|||||||
Reference in New Issue
Block a user