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) | 将连接放回连接池 |
array transaction(string db_name, function fn, ...) | 在db上创建一个会话,并在此会话上执行事务 |
array run_command_in_db(string db_name, map command) | 在指定的数据库里执行一条命令 |
Collection.gs
和mongoDB客户端对象关联的集合对象
collection_op.gs
提供集合操作接口
Database.gs
和mongo客户端对象关联的数据库对象
函数原型 | 函数作用 |
---|---|
string get_database_name() | 获取数据库名称 |
object create_or_get_collection(string coll_name) | 创建或获取一个指定名称的集合对象 |
object create_or_get_gridfs() | 创建或获取一个GridFS对象 |
bool is_collection_exist(string coll_name) | 判断指定名称的集合是否存在 |
array get_collection_names(map opts = ) | 获取数据库中所有集合名称 |
GridFs.gs
和MongoDB客户端对象关联的处理大文件的对象
mongoc.gs
函数原型 | 函数作用 |
---|---|
void setup_mongoc(map cfg = nil) | 根据cfg配置初始化mongoc服务 |
float get_idle_timeout() | 获取连接池闲置超时时长 |
bool is_inited() | 是否已经初始化 |
object create_mongo_client_pool(string host, mixed port, string db, domain db_domain, map opts = ) | 创建客户端连接对象(无验证) |
object create_mongo_client_pool_auth(string host, mixed port, string db, string user, string pwd, domain db_domain, map opts = ) | 创建客户端连接对象(认证) |
object create_mongo_client_pool_uri(string uri, domain db_domain) | 使用uri地址创建客户端连接对象 |
string get_version() | 获取mongoc版本 |
array get_versions() | 获取mongoc版本,包括major\minor\micro版本信息 |
string new_oid_string() | 生成一个oid字符串 |
MongoDataTypeDate new_date(mixed value = nil) | 生成一个日期类型的数据 |
MongoDataTypeObjectId new_oid(string value = nil) | 生成一个object idl类型的数据 |
Session.gs
和mongoDB客户端对象关联的会话对象
函数原型 | 函数作用 |
---|---|
object create_collection(string name) | 创建一个集合对象 |
array transaction(function fn, ...) | 执行事务 |
SessionCollection.gs
由会话(Sesison)创建的集合对象
UtilFunction.gs
pkg.mongoc的工具函数库文件
函数原型 | 函数作用 |
---|---|
int bson_new_from_json_str(string data_str) | 将json字符串转换为bson数据 |
mixed parse_bson_ptr(int ptr) | 解析bson数据 |
int make_opts_handle(map opts) | 把选项词典转换为bson数据 |
int adler32(string text) | 计算Adler-32校验和 |
map check_and_transfer_int_keys(mixed data, map transfer_map = ) | 检查并转换词典/数组中所有词典元素的键 |
mixed revert_int_keys_and_binary(mixed data) | 还原所有int键的值和buffer |
int bson_dumps(mixed data) | 将数据转换为bson格式 |
map bson_restore(int bson_handle) | 从bson数据中恢复出对应的map |
bool bson_equal(int bson_ptr1, int bson_ptr2) | 比较两个bson数据是否相同 |
void bson_destroy(int bson_t) | 销毁bson数据 |
类
MongoDataType
Mongo数据基础类型
成员变量
变量名 | 类型 | 初始值 | 须初始化 | 描述 |
---|
成员方法
函数原型 | 函数作用 |
---|
MongoDataTypeDate
继承自 MongoDataType
Mongo数据类型 - 日期
成员变量
变量名 | 类型 | 初始值 | 须初始化 | 描述 |
---|---|---|---|---|
value | int | 0 | 可选 | value这里是毫秒数("1970-01-01-08:00:00")开始 |
成员方法
函数原型 | 函数作用 |
---|
MongoDataTypeObjectId
继承自 MongoDataType
Mongo数据类型 - ObjectId
成员变量
变量名 | 类型 | 初始值 | 须初始化 | 描述 |
---|---|---|---|---|
value | string | nil | 可选 | value是object id字符串 |
成员方法
函数原型 | 函数作用 |
---|
样例
import pkg.mongoc;
public void sample()
{
mongoc.setup_mongoc({});
object client = mongoc.create_mongo_client_pool("localhost", "27017", "testdb",
this_domain(), {"connectTimeoutMS" : 500, "socketTimeoutMS" : 500});
client.connect();
object coll_ob = client.client_get_or_create_collection("testdb", "test");
coll_ob.drop();
coll_ob = client.client_get_or_create_collection("testdb", "test");
map insert_data = {
"string" : "test_1",
"map" : {"a" : 1, "b" : "sss"},
"array" : [1, 2, 3, 4],
"int" : 99,
"bool" : true,
};
// insert one
coll_ob.insert_one(insert_data);
array find_data = coll_ob.find_many({"string" : "test_1"}, {"projection" : {"_id" : 0}});
find_data = coll_ob.find_many({"string" : "test_2"}, {"projection" : {"_id" : 0}});
// update one
coll_ob.update_one({"string" : "test_1"}, {"$set" : {"update" : "test_test"}});
insert_data["update"] = "test_test";
find_data = coll_ob.find_many({"string" : "test_1"}, {"projection" : {"_id" : 0}});
map replace_data = {
"string" : "test_replace",
"map" : {"aa" : 0, "bb" : "www"},
"array" : [10, 9, 8, 7, 6],
};
// replace one
coll_ob.replace_one({"string" : "test_1"}, replace_data);
insert_data["update"] = "test_test";
find_data = coll_ob.find_many({"string" : "test_replace"}, {"projection" : {"_id" : 0}});
// delete one
coll_ob.delete_one({"string" : "test_replace"});
find_data = coll_ob.find_many({"string" : "test_replace"});
// aggregate
for (int i = 1 upto 10)
{
map insert_data = {
"string" : sprintf("aa%O", i),
"name" : "m68",
"id" : i,
};
coll_ob.insert_one(insert_data);
}
array ret = coll_ob.aggregate([{"$group" : {"_id" : "$name", "num_tutorial" : {"$sum" : 1}}}]);
destruct_object(coll_ob);
destruct_object(client);
}
sample();