diff --git a/lualib/mongo.lua b/lualib/mongo.lua index 38ac20dd..6399465d 100644 --- a/lualib/mongo.lua +++ b/lualib/mongo.lua @@ -282,25 +282,39 @@ end -- collection:createIndex({username = 1}, {unique = true}) function mongo_collection:createIndex(keys, option) - local name - for k, v in pairs(keys) do - assert(v == 1) - name = (name == nil) and k or (name .. "_" .. k) + local name = option.name + option.name = nil + + if not name then + for k, v in pairs(keys) do + name = (name == nil) and k or (name .. "_" .. k) + name = name .. "_" .. v + end end + local doc = {}; doc.name = name doc.key = keys for k, v in pairs(option) do - if v then - doc[k] = true - end + doc[k] = v end return self.database:runCommand("createIndexes", self.name, "indexes", {doc}) end mongo_collection.ensureIndex = mongo_collection.createIndex; + +function mongo_collection:drop() + return self.database:runCommand("drop", self.name) +end + +-- collection:dropIndex("age_1") +-- collection:dropIndex("*") +function mongo_collection:dropIndex(indexName) + return self.database:runCommand("dropIndexes", self.name, "index", indexName) +end + -- collection:findAndModify({query = {name = "userid"}, update = {["$inc"] = {nextid = 1}}, }) -- keys, value type -- query, table diff --git a/test/testmongodb.lua b/test/testmongodb.lua new file mode 100644 index 00000000..f1fa81e0 --- /dev/null +++ b/test/testmongodb.lua @@ -0,0 +1,90 @@ +local skynet = require "skynet" +local mongo = require "mongo" +local bson = require "bson" + +local host, db_name = ... + +function test_insert_without_index() + local db = mongo.client({host = host}) + + db[db_name].testdb:dropIndex("*") + db[db_name].testdb:drop() + + local ret = db[db_name].testdb:safe_insert({test_key = 1}); + assert(ret and ret.n == 1) + + local ret = db[db_name].testdb:safe_insert({test_key = 1}); + assert(ret and ret.n == 1) +end + +function test_insert_with_index() + local db = mongo.client({host = host}) + + db[db_name].testdb:dropIndex("*") + db[db_name].testdb:drop() + + db[db_name].testdb:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index"}) + + local ret = db[db_name].testdb:safe_insert({test_key = 1}) + assert(ret and ret.n == 1) + + local ret = db[db_name].testdb:safe_insert({test_key = 1}) + assert(ret and ret.n == 0) +end + +function test_find_and_remove() + local db = mongo.client({host = host}) + + db[db_name].testdb:dropIndex("*") + db[db_name].testdb:drop() + + db[db_name].testdb:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index"}) + + local ret = db[db_name].testdb:safe_insert({test_key = 1}) + assert(ret and ret.n == 1) + + local ret = db[db_name].testdb:findOne({test_key = 1}) + assert(ret and ret.test_key == 1) + + db[db_name].testdb:delete({test_key = 1}) + + local ret = db[db_name].testdb:findOne({test_key = 1}) + assert(ret == nil) +end + + +function test_expire_index() + local db = mongo.client({host = host}) + + db[db_name].testdb:dropIndex("*") + db[db_name].testdb:drop() + + db[db_name].testdb:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index", expireAfterSeconds = 1, }) + db[db_name].testdb:ensureIndex({test_date = 1}, {expireAfterSeconds = 1, }) + + local ret = db[db_name].testdb:safe_insert({test_key = 1, test_date = bson.date(os.time())}) + assert(ret and ret.n == 1) + + local ret = db[db_name].testdb:findOne({test_key = 1}) + assert(ret and ret.test_key == 1) + + for i = 1, 1000 do + skynet.sleep(11); + + local ret = db[db_name].testdb:findOne({test_key = 1}) + if ret == nil then + return + end + end + + assert(false, "test expire index failed"); +end + +skynet.start(function() + test_insert_without_index() + test_insert_with_index() + test_find_and_remove() + test_expire_index() + + print("mongodb test finish."); +end)