diff --git a/lualib/skynet/db/mongo.lua b/lualib/skynet/db/mongo.lua index a249155b..1fa34b15 100644 --- a/lualib/skynet/db/mongo.lua +++ b/lualib/skynet/db/mongo.lua @@ -423,6 +423,18 @@ function mongo_collection:safe_delete(selector, single) return werror(r) end +function mongo_collection:safe_batch_delete(selectors, single) + local delete_tb = {} + for i = 1, #selectors do + delete_tb[i] = bson_encode({ + q = selectors[i], + limit = single and 1 or 0, + }) + end + local r = self.database:runCommand("delete", self.name, "deletes", delete_tb) + return werror(r) +end + function mongo_collection:findOne(query, selector) local conn = self.connection local request_id = conn:genId() diff --git a/test/testmongodb.lua b/test/testmongodb.lua index 0a102942..9f1fd810 100644 --- a/test/testmongodb.lua +++ b/test/testmongodb.lua @@ -186,6 +186,32 @@ local function test_safe_batch_insert() assert(length == ret:count(), "test safe batch insert failed") end +local function test_safe_batch_delete() + local ok, err, ret + local c = _create_client() + local db = c[db_name] + + db.testcoll:drop() + + local docs, length = {}, 10 + for i = 1, length do + table.insert(docs, {test_key = i}) + end + + db.testcoll:safe_batch_insert(docs) + + docs = {} + local del_num = 5 + for i = 1, del_num do + table.insert(docs, {test_key = i}) + end + + db.testcoll:safe_batch_delete(docs) + + local ret = db.testcoll:find() + assert(length == ret:count(), "test safe batch delete failed") +end + skynet.start(function() if username then print("Test auth") @@ -203,5 +229,7 @@ skynet.start(function() test_expire_index() print("test safe batch insert") test_safe_batch_insert() + print("test safe batch delete") + test_safe_batch_delete() print("mongodb test finish."); end)