feat: add mongo self batch insert (#1456)

* feat: add mongo self batch insert

* perf: add safe batch insert test

* perf: modify some words
This commit is contained in:
JTrancender
2021-08-18 15:11:40 +08:00
committed by GitHub
parent 740b91533d
commit fdc4b35281
2 changed files with 32 additions and 0 deletions

View File

@@ -365,6 +365,18 @@ function mongo_collection:batch_insert(docs)
sock:request(pack)
end
function mongo_collection:safe_batch_insert(docs)
for i = 1, #docs do
if docs[i]._id == nil then
docs[i]._id = bson.objectid()
end
docs[i] = bson_encode(docs[i])
end
local r = self.database:runCommand("insert", self.name, "documents", docs)
return werror(r)
end
function mongo_collection:update(selector,update,upsert,multi)
local flags = (upsert and 1 or 0) + (multi and 2 or 0)
local sock = self.connection.__sock

View File

@@ -168,6 +168,24 @@ function test_expire_index()
assert(false, "test expire index failed");
end
local function test_safe_batch_insert()
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)
local ret = db.testcoll:find()
assert(length == ret:count(), "test safe batch insert failed")
end
skynet.start(function()
if username then
print("Test auth")
@@ -183,5 +201,7 @@ skynet.start(function()
test_runcommand()
print("Test expire index")
test_expire_index()
print("test safe batch insert")
test_safe_batch_insert()
print("mongodb test finish.");
end)