跳到主要内容

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

支持异步操作的集合对象

函数原型函数作用
object get_internal_coll()获取本对象关联的集合对象
bool create_index(string index_name, array key_list, bool is_unique = false, mixed expire_after_seconds = nil)创建索引
map get_indexes(map opts = )获取所有索引信息
bool drop_index(string index_name, map opts = )删除索引
array exist(map find_doc_map, map opts = )检查满足条件的数据是否存在
array find_many(map find_doc_map, map opts = , bool load_json = true)查询多条数据
array find_one(map find_doc_map, map opts = , bool load_json = true)查询一条数据
bool insert_one(mixed data, map opts = )插入一条数据
bool insert_many(array data_list, map opts = )插入多条数据
bool update_one(map query_data, mixed update_data, map opts = )更新一条数据
map update_one_ex(map query_data, mixed update_data, map opts = )更新一条数据,返回详细的处理结果
bool update_many(map query_data, mixed update_data, map opts = )更新多条数据
bool replace_one(map query_data, mixed update_data, map opts = )替换一条数据
map replace_one_ex(map query_data, mixed update_data, map opts = )替换一条数据,返回详细的处理结果
bool delete_one(map query_data, map opts = )删除一条数据
map delete_one_ex(map query_data, map opts = )删除一条数据,返回详细的处理结果
bool delete_many(map query_data, map opts = )删除多条数据
map delete_many_ex(map query_data, map opts = )删除多条数据,返回详细的处理结果
array aggregate(array aggregate_data, map opts = )在集合中执行聚合操作
int count(map query_data, map opts = )统计集合中文档数量
bool drop()删除集合
array find_and_modify(map query, map update, bool upsert = true, map sort = nil, map fields = nil)执行find_and_modify操作
array distinct(string key, map query = )执行distinct操作,获取指定字段的所有唯一值
array bulk_execute(function fn, ...)在集合上执行批量操作

AsyncCollectionWithChecksum.gs

异步执行的带校验和功能的集合对象

函数原型函数作用
void disable_checksum(bool flag_disable = true)调试接口, 关闭或者开启校验和检查
object get_internal_coll()获取本对象关联的集合对象
bool create_index(string index_name, array key_list, bool is_unique = false, mixed expire_after_seconds = nil)创建索引
map get_indexes(map opts = )获取所有索引信息
bool drop_index(string index_name, map opts = )删除索引
array exist(map find_doc_map, map opts = )检查满足条件的数据是否存在
array find_many(map find_doc_map, map opts = , bool load_json = true)查询多条数据
array find_one(map find_doc_map, map opts = , bool load_json = true)查询一条数据
bool insert_one(mixed data, map opts = )插入一条数据
bool insert_many(array data_list, map opts = )插入多条数据
bool update_one(map query_data, mixed update_data, map opts = )更新一条数据
map update_one_ex(map query_data, mixed update_data, map opts = )更新一条数据,返回详细的处理结果
bool update_many(map query_data, mixed update_data, map opts = )更新多条数据
bool replace_one(map query_data, mixed update_data, map opts = )替换一条数据
map replace_one_ex(map query_data, mixed update_data, map opts = )替换一条数据,返回详细的处理结果
bool delete_one(map query_data, map opts = )删除一条数据
map delete_one_ex(map query_data, map opts = )删除一条数据,返回详细的处理结果
bool delete_many(map query_data, map opts = )删除多条数据
map delete_many_ex(map query_data, map opts = )删除多条数据,返回详细的处理结果
array aggregate(array aggregate_data, map opts = )在集合中执行聚合操作
int count(map query_data, map opts = )统计集合中文档数量
bool drop()删除集合
array find_and_modify(map query, map update, bool upsert = true, map sort = nil, map fields = nil)执行find_and_modify操作
array find_and_modify_with_opts(map query, map update = nil, map sort = nil, map fields = nil, bool upsert = true, bool new = true, bool remove = false, bool by_pass = false, int max_time_ms = 0, map opts = )在集合上执行findAndModify操作,返回详细的执行结果
array distinct(string key, map query = )执行distinct操作,获取指定字段的所有唯一值
array bulk_execute(function fn, ...)在集合上执行批量操作
void fill_checksum(map data)为指定数据填充校验和'checksum'字段
void reset_checksum(map find_doc_map)重置集合里所有满足条件的数据的校验和

AsyncGridFs.gs

支持异步操作的GridFS对象

函数原型函数作用
object get_internal_gridfs()获取本对象关联的GridFs对象
bool delete(MongoDataTypeObjectId file_id)删除指定ID的文件
buffer download(MongoDataTypeObjectId file_id)下载指定ID的文件
array find_all(map filter, map opts = nil)查询满足条件的所有文件
mixed upload(string filename, buffer data, map opts = nil)上传文件
bool upload_with_id(MongoDataTypeObjectId file_id, string filename, buffer data, map opts = nil)上传文件到GridFS存储区并指定id

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客户端对象关联的集合对象

函数原型函数作用
string get_database_name()获取数据库名称
string get_collection_name()获取集合名称
void bson_destroy_array(array bsons)销毁一个包含BSON类型数据的数组
bool create_index(string index_name, array key_list, bool is_unique = false, mixed expire_after_seconds = nil)创建索引
map get_indexes(map opts)获取所有索引信息
bool drop_index(string index_name, map opts = )删除索引
bool insert_one(mixed data, map opts = )插入一条数据
bool insert_many(array data_list, map opts = )插入多条数据
array find_many(map find_doc_map, map opts = , bool load_json = true)查询多条数据
array exist(map find_doc_map, map opts = )检查满足条件的数据是否存在
array find_one(map find_doc_map, map opts = , bool load_json = true)查询一条数据
bool update_one(map query_data, mixed update_data, map opts = )更新一条数据
map update_one_ex(map query_data, mixed update_data, map opts = )更新一条数据,返回详细的处理结果
bool update_many(map query_data, map update_data, map opts = )更新多条数据
bool replace_one(map query_data, mixed replace_data, map opts = )替换一条数据
map replace_one_ex(map query_data, mixed replace_data, map opts = )替换一条数据,返回详细的处理结果
bool delete_one(map query_data, map opts = )删除一条数据
map delete_one_ex(map query_data, map opts = )删除一条数据,返回详细的处理结果
bool delete_many(map query_data, map opts = )删除多条数据
map delete_many_ex(map query_data, map opts = )删除多条数据,返回详细的处理结果
bool drop()删除集合
int count(map query_data, map opts)统计集合中文档数量
array aggregate(array pipeline_data, map opts = )在集合中执行聚合操作
array find_and_modify(map query, map update, bool upsert = true, map sort = nil, map fields = nil)执行find_and_modify操作
array find_and_modify_with_opts(map query, map update = nil, map sort = nil, map fields = nil, bool upsert = true, bool new = true, bool remove = false, bool by_pass = false, int max_time_ms = 0, map opts = )在集合上执行findAndModify操作,返回详细的执行结果
array distinct(string key, map query = )执行distinct操作,获取指定字段的所有唯一值
array bulk_execute(function fn, ...)在集合上执行批量操作
object get_asnyc_collection(object worker = nil)获取支持异步操作的集合对象
object get_async_collection_with_checksum(array checksum_fields, object worker = nil)获取支持异步操作的带有校验和功能的集合对象
void check_cursor_error(int cursor)检查mongoDB指定游标句柄上是否有错误,如果有错误抛出异常
string get_cursor_error(int cursor)获取mongoDB指定游标句柄上的错误信息
string desc()获取集合描述

collection_op.gs

提供集合操作接口

函数原型函数作用
bool create_index(int coll_handle, string coll_name, string index_name, array key_list, bool is_unique = false, mixed expire_after_seconds = nil)在集合上创建一个索引
map get_indexes(int coll_handle, map opts = )获取集合上的所有索引信息
bool drop_index(int coll_handle, string index_name, map opts = )删除集合上的一个索引
bool insert_one(int coll_handle, mixed data, map opts = )向集合中插入一条数据
void bson_destroy_array(array bsons)销毁一个包含BSON类型数据的数组
bool insert_many(int coll_handle, array data_list, map opts = )向集合中插入多条数据
array find_many(int coll_handle, map find_doc_map, map opts = , bool load_json = true)查询多条数据
array exist(int coll_handle, map find_doc_map, map opts = )检查满足条件的数据是否存在
array find_one(int coll_handle, map find_doc_map, map opts = , bool load_json = true)查询一条数据
bool update_one(int coll_handle, map query_data, mixed update_data, map opts = )更新一条数据
map update_one_ex(int coll_handle, map query_data, mixed update_data, map opts = )更新一条数据,返回详细的处理结果
bool update_many(int coll_handle, map query_data, map update_data, map opts = )更新多条数据
bool replace_one(int coll_handle, map query_data, mixed replace_data, map opts = )替换一条数据
map replace_one_ex(int coll_handle, map query_data, mixed replace_data, map opts = )替换一条数据,返回详细的处理结果
bool delete_one(int coll_handle, map query_data, map opts = )删除一条数据
map delete_one_ex(int coll_handle, map query_data, map opts = )删除一条数据,返回详细的处理结果
bool delete_many(int coll_handle, map query_data, map opts = )删除多条数据
map delete_many_ex(int coll_handle, map query_data, map opts = )删除多条数据,返回详细的处理结果
bool drop(int coll_handle, map opts = )删除集合
int count(int coll_handle, map query_data, map opts)统计集合文档数量
array aggregate(int coll_handle, array pipeline_data, map opts = )在集合上执行聚合操作
array find_and_modify_with_opts(int coll_handle, map query, map update = nil, map sort = nil, map fields = nil, bool upsert = true, bool new = true, bool remove = false, bool by_pass = false, int max_time_ms = 0, map opts = )在集合上执行findAndModify操作,返回详细的执行结果
array find_and_modify(int coll_handle, map query, map update, bool upsert = true, map sort = nil, map fields = nil)执行find_and_modify操作
array distinct(int coll_handle, string coll_name, string key, map query = )执行distinct操作,获取指定字段的所有唯一值
array bulk(int coll_handle, function fn, ...)执行批量操作
void check_cursor_error(int cursor)检查mongoDB指定游标句柄上是否有错误,如果有错误抛出异常
string get_cursor_error(int cursor)获取mongoDB指定游标句柄上的错误信息
int calc_checksum(map data, array checksum_fields = nil)计算校验和
bool verify_checksum(map data, array checksum_fields = nil)验证校验和
void fill_checksum(map data, array checksum_fields = nil)为数据填充校验和'checksum'字段值

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客户端对象关联的处理大文件的对象

函数原型函数作用
object get_async_gridfs(object worker = nil)获取支持异步操作的GridFS对象
bool delete(MongoDataTypeObjectId file_id)删除指定ID的文件
buffer download(MongoDataTypeObjectId file_id)下载指定ID的文件
array find_all(map filter, map opts = nil)查询满足条件的所有文件
mixed upload(string filename, buffer data, map opts = nil)上传文件
bool upload_with_id(MongoDataTypeObjectId file_id, string filename, buffer data, map opts = nil)上传文件到GridFS存储区并指定id

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)创建的集合对象

函数原型函数作用
bool create_index(string index_name, array key_list, bool is_unique = false, mixed expire_after_seconds = nil)创建索引
map get_indexes(map opts = )获取所有索引信息
bool drop_index(string index_name, map opts = )删除索引
bool insert_one(mixed data, map opts = )插入一条数据
bool insert_many(array data_list, map opts = )插入多条数据
array find_many(map find_doc_map, map opts = , bool load_json = true)查询多条数据
array exist(map find_doc_map, map opts = )检查满足条件的数据是否存在
array find_one(map find_doc_map, map opts = , bool load_json = true)查询一条数据
bool update_one(map query_data, mixed update_data, map opts = )更新一条数据
map update_one_ex(map query_data, mixed update_data, map opts = )更新一条数据,返回详细的处理结果
bool update_many(map query_data, map update_data, map opts = )更新多条数据
bool replace_one(map query_data, mixed replace_data, map opts = )替换一条数据
map replace_one_ex(map query_data, mixed replace_data, map opts = )替换一条数据,返回详细的处理结果
bool delete_one(map query_data, map opts = )删除一条数据
map delete_one_ex(map query_data, map opts = )删除一条数据,返回详细的处理结果
bool delete_many(map query_data, map opts = )删除多条数据
map delete_many_ex(map query_data, map opts = )删除多条数据,返回详细的处理结果
bool drop()删除集合
int count(map query_data, map opts)统计集合中文档数量
array aggregate(array pipeline_data, map opts = )在集合中执行聚合操作
array find_and_modify(map query, map update, bool upsert = true, map sort = nil, map fields = nil)执行find_and_modify操作
array distinct(string key, map query = )在集合上执行findAndModify操作,返回详细的执行结果
array bulk_execute(function fn, ...)在集合上执行批量操作

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数据类型 - 日期

成员变量

变量名类型初始值须初始化描述
valueint0可选value这里是毫秒数("1970-01-01-08:00:00")开始

成员方法

函数原型函数作用

MongoDataTypeObjectId

继承自 MongoDataType

Mongo数据类型 - ObjectId

成员变量

变量名类型初始值须初始化描述
valuestringnil可选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();