|  | 
|  | 1 | +---@meta | 
|  | 2 | +local mongo = {} | 
|  | 3 | + | 
|  | 4 | +---@class mongo_client | 
|  | 5 | +---@field host string | 
|  | 6 | +---@field port number | 
|  | 7 | +---@field username string | 
|  | 8 | +---@field password string | 
|  | 9 | +---@field authdb string | 
|  | 10 | +---@field authmod string | 
|  | 11 | +---@field __id number | 
|  | 12 | +---@field __sock socketchannel | 
|  | 13 | +local mongo_client = {} | 
|  | 14 | + | 
|  | 15 | +---@class mongo_db | 
|  | 16 | +---@field connection mongo_client | 
|  | 17 | +---@field name string  | 
|  | 18 | +---@field full_name string | 
|  | 19 | +---@field database mongo_db | 
|  | 20 | +---@field _cmd string dbname.$cmd | 
|  | 21 | +local mongo_db = {} | 
|  | 22 | + | 
|  | 23 | +---@class mongo_collection | 
|  | 24 | +---@field connection mongo_client | 
|  | 25 | +---@field name string | 
|  | 26 | +---@field full_name string | 
|  | 27 | +---@field database boolean | 
|  | 28 | +local mongo_collection = {} | 
|  | 29 | + | 
|  | 30 | +---@class mongo_cursor | 
|  | 31 | +local mongo_cursor = {} | 
|  | 32 | +---建立一个客户端 | 
|  | 33 | +---@param conf table {host, port, username, password, authdb, authmod} | 
|  | 34 | +---@return mongo_client | 
|  | 35 | +function mongo.client(conf) | 
|  | 36 | +end | 
|  | 37 | +---获取一个 mongo_db 对象 | 
|  | 38 | +---@param dbname string | 
|  | 39 | +---@return mongo_db | 
|  | 40 | +function mongo_client:getDB(dbname) | 
|  | 41 | +end | 
|  | 42 | + | 
|  | 43 | +---断开连接 | 
|  | 44 | +function mongo_client:disconnect() | 
|  | 45 | +end | 
|  | 46 | +---以 self.admin:runCommand(...) 来执行命令 | 
|  | 47 | +function mongo_client:runCommand(...) | 
|  | 48 | +end | 
|  | 49 | + | 
|  | 50 | +---退出登录 | 
|  | 51 | +function mongo_client:logout() | 
|  | 52 | +end | 
|  | 53 | + | 
|  | 54 | +---验证登录 | 
|  | 55 | +---@param user string | 
|  | 56 | +---@param pass string | 
|  | 57 | +function mongo_db:auth(user, pass) | 
|  | 58 | + | 
|  | 59 | +end | 
|  | 60 | +---执行命令 | 
|  | 61 | +function mongo_db:runCommand(cmd, cmd_v, ...) | 
|  | 62 | +end | 
|  | 63 | +---获取集合 | 
|  | 64 | +---@param collection string | 
|  | 65 | +---@return mongo_collection | 
|  | 66 | +function mongo_db:getCollection(collection) | 
|  | 67 | +end | 
|  | 68 | +---获取集合 | 
|  | 69 | +---@param collection string | 
|  | 70 | +---@return mongo_collection | 
|  | 71 | +function mongo_collection:getCollection(collection) | 
|  | 72 | +end | 
|  | 73 | + | 
|  | 74 | +---向集合插入文档 | 
|  | 75 | +---@param doc table | 
|  | 76 | +function mongo_collection:insert(doc) | 
|  | 77 | +end | 
|  | 78 | +---向集合安全的插入数据 | 
|  | 79 | +---@param dco table | 
|  | 80 | +function mongo_collection:safe_insert(dco) | 
|  | 81 | +end | 
|  | 82 | + | 
|  | 83 | +---插入批量数据 | 
|  | 84 | +---@param docs table[] | 
|  | 85 | +function mongo_collection:batch_insert(docs) | 
|  | 86 | + | 
|  | 87 | +end | 
|  | 88 | +---安全插入批量数据 | 
|  | 89 | +---@param docs table[] | 
|  | 90 | +function mongo_collection:safe_batch_insert(docs) | 
|  | 91 | + | 
|  | 92 | +end | 
|  | 93 | +---更新数据 | 
|  | 94 | +---@param selector table | 
|  | 95 | +---@param update table | 
|  | 96 | +---@param upsert boolean | 
|  | 97 | +---@param multi boolean | 
|  | 98 | +function mongo_collection:update(selector, update, upsert, multi) | 
|  | 99 | + | 
|  | 100 | +end | 
|  | 101 | +---安全更新数据 | 
|  | 102 | +---@param selector table | 
|  | 103 | +---@param update table | 
|  | 104 | +---@param upsert boolean | 
|  | 105 | +---@param multi boolean | 
|  | 106 | +function mongo_collection:safe_update(selector, update, upsert, multi) | 
|  | 107 | + | 
|  | 108 | +end | 
|  | 109 | + | 
|  | 110 | +---删除数据 | 
|  | 111 | +---@param selector table | 
|  | 112 | +---@param single boolean | 
|  | 113 | +function mongo_collection:delete(selector, single) | 
|  | 114 | + | 
|  | 115 | +end | 
|  | 116 | +---安全删除数据 | 
|  | 117 | +---@param selector table | 
|  | 118 | +---@param single boolean | 
|  | 119 | +function mongo_collection:safe_delete(selector, single) | 
|  | 120 | + | 
|  | 121 | +end | 
|  | 122 | +---@param query table | 
|  | 123 | +---@param selector table | 
|  | 124 | +---@return mongo_cursor | 
|  | 125 | +function mongo_collection:find(query, selector) | 
|  | 126 | + | 
|  | 127 | +end | 
|  | 128 | +---@param query table | 
|  | 129 | +---@param selector table | 
|  | 130 | +---@return table | 
|  | 131 | +function mongo_collection:findOne(query, selector) | 
|  | 132 | + | 
|  | 133 | +end | 
|  | 134 | + | 
|  | 135 | +---建立索引 | 
|  | 136 | +---* collection:createIndex { { key1 = 1}, { key2 = 1 }, unique = true } | 
|  | 137 | +---* or collection:createIndex { "key1", "key2", unique = true } | 
|  | 138 | +---* or collection:createIndex( { key1 = 1} , { unique = true } )	-- For compatibility | 
|  | 139 | +---@param arg1 table | 
|  | 140 | +---@param arg2 table | 
|  | 141 | +function mongo_collection:createIndex(arg1, arg2) | 
|  | 142 | + | 
|  | 143 | +end | 
|  | 144 | +---建立多个索引 | 
|  | 145 | +---@vararg table | 
|  | 146 | +function mongo_collection:createIndexs(...) | 
|  | 147 | + | 
|  | 148 | +end | 
|  | 149 | +mongo_collection.ensureIndex = mongo_collection.createIndex | 
|  | 150 | + | 
|  | 151 | +---删除集合 | 
|  | 152 | +function mongo_collection:drop() | 
|  | 153 | + | 
|  | 154 | +end | 
|  | 155 | +--- 删除索引 | 
|  | 156 | +---* collection:dropIndex("age_1") | 
|  | 157 | +---* collection:dropIndex("*") | 
|  | 158 | +---@param indexName string  | 
|  | 159 | +function mongo_collection:dropIndex(indexName) | 
|  | 160 | + | 
|  | 161 | +end | 
|  | 162 | + | 
|  | 163 | +---查找并修改 | 
|  | 164 | +---* collection:findAndModify({query = {name = "userid"}, update = {["$inc"] = {nextid = 1}}, }) | 
|  | 165 | +---* keys, value type | 
|  | 166 | +---* query, table | 
|  | 167 | +---* sort, table | 
|  | 168 | +---* remove, bool | 
|  | 169 | +---* update, table | 
|  | 170 | +---* new, bool | 
|  | 171 | +---* fields, bool | 
|  | 172 | +---* upsert, boolean | 
|  | 173 | +---@param doc table | 
|  | 174 | +function mongo_collection:findAndModify(doc) | 
|  | 175 | + | 
|  | 176 | +end | 
|  | 177 | + | 
|  | 178 | +---排序 | 
|  | 179 | +---* cursor:sort { key = 1 } or cursor:sort( {key1 = 1}, {key2 = -1}) | 
|  | 180 | +---@param key table | 
|  | 181 | +---@param key_v table | 
|  | 182 | +function mongo_cursor:sort(key, key_v, ...) | 
|  | 183 | +end | 
|  | 184 | +---跳过多少行 | 
|  | 185 | +---@param amount number | 
|  | 186 | +function mongo_cursor:skip(amount) | 
|  | 187 | +end | 
|  | 188 | +---限制行数 | 
|  | 189 | +---@param amount number | 
|  | 190 | +function mongo_cursor:limit(amount) | 
|  | 191 | +end | 
|  | 192 | +---统计行数 | 
|  | 193 | +---@param with_limit_and_skip boolean | 
|  | 194 | +function mongo_cursor:count(with_limit_and_skip) | 
|  | 195 | +end | 
|  | 196 | +---是否有下一行 | 
|  | 197 | +---@return boolean | 
|  | 198 | +function mongo_cursor:hasNext() | 
|  | 199 | +end | 
|  | 200 | +---下一行 | 
|  | 201 | +---@return table | 
|  | 202 | +function mongo_cursor:next() | 
|  | 203 | +end | 
|  | 204 | +---关闭游标 | 
|  | 205 | +function mongo_cursor:close() | 
|  | 206 | +end | 
|  | 207 | +return mongo | 
0 commit comments