mongoc
简介
提供mongoc数据库服务
mongoc数据库操作过程为:
1、初始化数据库服务,thread_num为db worker线程数,默认为1
mongoc.setup_mongoc({thread_num:2});
2、创建连接池对象,有create_mongo_client_pool/create_mongo_client_pool_auth/create_mongo_client_pool_uri三种接口。
object client = mongoc.create_mongo_client_pool("localhost", "27017", "testdb",
this_domain(), {"connectTimeoutMS" : 500, "socketTimeoutMS" : 500});
3、连接数据库
client.connect();//这个api中才会真正去调用dll的创建连接池的接口
4、创建一个集合对象
object coll_ob = client.client_get_or_create_collection("testdb", "test");//将连接池与集合对象绑定
5、操作集合
coll_ob.insert_many(insert_datas);
//这个API中会先从连接池中pop一个连接,在连接上调用dll的mongoc_client_get_collection获得集合句柄,再对集合执行操作
6、销毁集合对象,销毁连接池对象
destruct_object(coll_ob);
destruct_object(client);
组件接口
AsyncCollection.gs
支持异步操作的集合对象
AsyncCollectionWithChecksum.gs
异步执行的带校验和功能的集合对象
AsyncGridFs.gs
支持异步操作的GridFS对象
Bulk.gs
mongoDB的批量操作对象
| 函数原型 | 函数作用 |
|---|---|
| array execute() | 执行批量操作 |
| void insert_one(mixed doc) | 增加一个批量操作 - 插入一条数据 |
| void insert_one_ex(mixed doc, map opts) | 增加一个批量操作 - 插入一条数据并返回插入结果 |
| void delete_one(map selector) | 增加一个批量操作 - 删除一条数据 |
| void delete_one_ex(map selector, map opts = ) | 增加一个批量操作 - 删除一条数据并返回删除结果 |
| void delete_many(map selector) | 增加一个批量操作 - 删除多条数据 |
| void delete_many_ex(map selector, map opts = ) | 增加一个批量操作 - 删除多条数据并返回删除结果 |
| void replace_one(map selector, mixed doc, bool upsert = true) | 增加一个批量操作 - 替换一条数据 |
| void replace_one_ex(map selector, mixed doc, map opts = ) | 增加一个批量操作 - 替换一条数据并返回替换结果 |
| void update_one(map selector, map doc, bool upsert = false) | 增加一个批量操作 - 更新一条数据 |
| void update_one_ex(map selector, map doc, map opts = ) | 增加一个批量操作 - 更新一条数据并返回更新结果 |
| void update_many(map selector, map doc, bool upsert = false) | 增加一个批量操作 - 更新多条数据 |
| void update_many_ex(map selector, map doc, map opts = ) | 增加一个批量操作 - 更新多条数据并返回更新结果 |
Client.gs
mongo数据库的客户端对象
管理一个mongoc连接池
| 函数原型 | 函数作用 |
|---|---|
| void set_max_idle_time(float seconds) | 设置连接池里的连接空闲销毁时间 |
| string get_db_name() | 获取默认的数据库名称(由配置字段db决定) |
| bool connect() | 连接数据库 |
| void disconnect() | 关闭连接池 |
| bool is_connect() | 判断连接是否建立 |
| object get_or_create_database(string db_name) | 创建一个数据库对象 |
| bool drop_database(string db_name, map opts = ) | 删除数据库 |
| object client_get_or_create_collection(string db_name, string coll_name) | 创建一个集合对象 |
| object get_or_create_gridfs(string db_name, map opts = nil) | 创建一个GridFS对象 |
| object create_session(string db_name) | 创建一个会话对象 |
| int new_client_handle() | 创建一个连接(直接创建,不是从连接池里取出) |
| void destroy_client_handle(int client_handle) | 销毁一个连接 |
| int client_pool_pop() | 从连接池中取出一个连接 |
| void client_pool_push(int client_handle) |