feat: add mongo safe batch delete (#1577)

This commit is contained in:
caiyiheng
2022-04-15 16:20:00 +08:00
committed by GitHub
parent bcd012f9cf
commit 013cb42122
2 changed files with 40 additions and 0 deletions

View File

@@ -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()

View File

@@ -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)