跳到主要内容

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);

组件接口

client.gs

函数原型函数作用
string get_db_name()获取默认的数据库名称(由配置字段db决定)
bool connect()连接数据库
bool is_connect()判断连接是否建立
object get_or_create_database(string db_name)在此连接上根据db名创建一个db并返回
object client_get_or_create_collection(string db_name, string coll_name)在此连接上根据db名和集合名,创建并返回一个集合
object get_or_create_gridfs(string db_name, map opts = nil)在此连接上根据db名创建并返回一个gridfs
object create_session(string db_name)在此连接上为db创建并返回一个会话
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

函数原型函数作用
int get_latest_run_time()获取最后运行的时间

database.gs

函数原型函数作用
int get_latest_run_time()获取最后运行的时间

gridfs.gs

封装GridFS bucket API

函数原型函数作用
int get_latest_run_time()获取最后运行的时间
bool delete(MongoDataTypeObjectId file_id)给定file_id,从GridFS中删除此存储文件的files集合文档和关联的chunks
buffer download(MongoDataTypeObjectId file_id)下载file_id指定的存储文件的内容
array find_all(map filter, map opts = nil)查询GridFS中的文件
mixed upload(string filename, buffer data, map opts = nil)上传文件到GridFS存储区
bool upload_with_id(MongoDataTypeObjectId file_id, string filename, buffer data, map opts = nil)上传文件到GridFS存储区并指定id

mongoc.gs

函数原型函数作用
void setup_mongoc(map cfg)根据cfg配置初始化mongoc服务
int get_idle_timeout()获取闲置超时时间
function get_idle_callback()获取闲置超时的函数回调
bool is_inited()是否已经初始化
object create_mongo_client_pool(string host, mixed port, string db, domain db_domain, map opts = )创建mongo连接池,无authon信息
object create_mongo_client_pool_auth(string host, mixed port, string db, string user, string pwd, domain db_domain, map opts = )创建mongo连接,带authon信息
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

函数原型函数作用
array transaction(function fn, ...)用一个worker执行事务

枚举

bson_type_t


BSON_TYPE_EOD = 0x00,
BSON_TYPE_DOUBLE = 0x01,
BSON_TYPE_UTF8 = 0x02,
BSON_TYPE_DOCUMENT = 0x03,
BSON_TYPE_ARRAY = 0x04,
BSON_TYPE_BINARY = 0x05,
BSON_TYPE_UNDEFINED = 0x06,
BSON_TYPE_OID = 0x07,
BSON_TYPE_BOOL = 0x08,
BSON_TYPE_DATE_TIME = 0x09,
BSON_TYPE_NULL = 0x0A,
BSON_TYPE_REGEX = 0x0B,
BSON_TYPE_DBPOINTER = 0x0C,
BSON_TYPE_CODE = 0x0D,
BSON_TYPE_SYMBOL = 0x0E,
BSON_TYPE_CODEWSCOPE = 0x0F,
BSON_TYPE_INT32 = 0x10,
BSON_TYPE_TIMESTAMP = 0x11,
BSON_TYPE_INT64 = 0x12,
BSON_TYPE_DECIMAL128 = 0x13,
BSON_TYPE_MAXKEY = 0x7F,
BSON_TYPE_MINKEY = 0xFF,