This commit is contained in:
baidwwy 2021-03-03 18:50:32 +08:00
parent 725e244537
commit 3381c74644
1 changed files with 30 additions and 23 deletions

View File

@ -1,24 +1,25 @@
--[[
@Author : GGELUA
@Date : 2021-02-25 13:34:40
@LastEditTime : 2021-02-28 10:02:24
@LastEditTime : 2021-03-03 18:44:48
--]]
local MG集合 = class('MG集合')
function MG集合:MG集合(col)
function MG集合:MG集合(cli,col)
self._cli = cli
self._col = col
end
function MG集合:统计(query,options,prefs)
return assert(self._col:count(query,options,prefs))
return self._col:count(query or {},options,prefs)
end
function MG集合:清空(opt)
return self._col:drop(opt)
end
function MG集合:(query,options,prefs)
function MG集合:(query,options,prefs)
local t = {}
for v in self._col:find(query,options,prefs):iterator() do
table.insert( t,v )
@ -30,28 +31,33 @@ function MG集合:遍历(query,options,prefs)
return self._col:find(query,options,prefs):iterator()
end
function MG集合:查找一条(query,options,prefs)
return assert(self._col:findOne(query, options,prefs))
function MG集合:查询一条(query,options,prefs)
local r = self._col:findOne(query, options,prefs)
return r and r:value()
end
function MG集合:插入(document,fao)
if type(fao)=='number' then
return assert(self._col:insert(document,fao))
function MG集合:查询修改(query,options)
return self._col:findAndModify(query, options)
end
function MG集合:插入(document,flag_opt)
if type(flag_opt)=='number' then
return self._col:insert(document,flag_opt)
end
return assert(self._col:insertOne(document,fao))
return self._col:insertOne(document,flag_opt)
end
--TODO insertMany
function MG集合:删除(query,flags)
return assert(self._col:remove(query,flags))
return self._col:remove(query,flags)
end
function MG集合:删除全部(query,options)
return assert(self._col:removeMany(query,options))
return self._col:removeMany(query,options)
end
function MG集合:删除一条(query,options)
return assert(self._col:removeOne(query,options))
return self._col:removeOne(query,options)
end
function MG集合:重命名(dbname, collname, force, options)
@ -59,19 +65,19 @@ function MG集合:重命名(dbname, collname, force, options)
end
function MG集合:替换(query, document, options)
return assert(self._col:replaceOne(query, document, options))
return self._col:replaceOne(query, document, options)
end
--总是返回true
function MG集合:更新(query, document, flags)
return assert(self._col:update(query, document, flags))
return self._col:update(query, document, flags)
end
function MG集合:更新全部(query, document, options)
return assert(self._col:updateMany(query, document, options))
return self._col:updateMany(query, document, options)
end
function MG集合:更新一条(query, document, options)
return assert(self._col:updateOne(query, document, options))
return self._col:updateOne(query, document, options)
end
function MG集合:取名称(query,options)
@ -85,7 +91,8 @@ end
--TODO setReadPrefs
local MG数据库 = class('MG数据库')
function MG数据库:MG数据库(db)
function MG数据库:MG数据库(cli,db)
self._cli = cli
self._db = db
end
@ -94,7 +101,7 @@ function MG数据库:创建集合(name,opt)
end
function MG数据库:取集合(name)
return MG集合(assert(self._db:getCollection(name)))
return MG集合(self._cli,assert(self._db:getCollection(name)))
end
function MG数据库:检查集合(name)
@ -117,7 +124,7 @@ end
local mongodb = require("mongo")
local MONGO = class('MONGO')
local MONGO = class('MongoDB')
MONGO.Binary = mongodb.Binary
MONGO.BSON = mongodb.BSON
@ -152,12 +159,12 @@ end
function MONGO:取数据库(dbname)
local db = assert(self._clt:getDatabase(dbname))
return MG数据库(db)
return MG数据库(self,db)
end
function MONGO:取集合(dbname,colname)
local col = assert(self._clt:getCollection(dbname,colname))
return MG集合(col)
return MG集合(self,col)
end
function MONGO:命令(dbname,command,options,prefs)