remote_mongoc
简介
mongodb远程操作;通过mongo server中继再对mongodb进行操作:先连接mongo server,再向mongo server发送指令,由mongo server对mongodb进行操作;
和mongo server之接的连接是由pkg.mongoserver_client创建的;
另外,mongodb本地操作一般来说是指通过pkg.mongoc直接对mongodb进行操作。
附上mongoc api首页
组件接口
Client.gs
mongodb远程操作对象
本对象负责和mongo server进行通讯,发送指令通过mongo server对目标mongodb数据库进行操作;
对象所维护的网络连接是由pkg.mongoserver_client对象创建;
| 函数原型 | 函数作用 |
|---|---|
| bool connect() | 连接mongo server,并且确保mongo数据库可用(mongo server将由pkg.mongoc创建一个到目标数据库的本地连接对象) |
| array send_request(string cmd, map args = , int timeout = 30) | 阻塞模式向mongo server发送指令并等待结果 |
| object get_remote_collection(string coll_name) | 获取一个对特定数据集合(collection)进行远程操作的对象 |
RemoteCollection.gs
远程集合操作对象
一般的,和pkg.mongoc中的异步集合操作对象(async_collection)接口保持一致
remote_mongoc.gs
| 函数原型 | 函数作用 |
|---|---|
| object create_client(map para, domain d) | 创建一个mongodb远程操作对象 |
样例
#pragma parallel
import gs.lang.*;
import gs.util.*;
import pkg.remote_mongoc;
public object test(string account, map data)
{
// 本次连接的mongo数据库
// 使用这些信息在mongo server上通过pkg.mongo_clients创建本地连接
map db_config = {
"db" : {
"name" : "aaa",
"host" : "localhost",
"port" : "27017",
"db" : "aaa_remote_test",
"auth" : {},
},
"collections" : {
"account" : {
"account" : true,
},
},
/*
"para" : {
"name" : "real_name",
"db" : "real_db",
},
*/
};
map para = {
"ip" : "127.0.0.1", // mongo server的ip
"port" : 8411, // mongo server的port
"token" : "MSTK28735324169", // 连接用的令牌
"info" : {
"name" : "server1=>server7", // 这个连接的名称,需要保证唯一
},
"db_config" : db_config,
};
object ob = remote_mongoc.create_client(para, this_domain());
if (! ob.connect())
{
ob.close();
return nil;
}
object coll_ob = ob.get_remote_collection("account");
map query_data = {
"account" : account,
};
map opts = {
"upsert" : true,
};
if (! coll_ob=>replace_one(query_data, data, opts))
return ob;
return ob;
}