辅助节点上的MongoDB聚合框架
MongoDB aggregation framework on secondary node
如何使用c++驱动程序在辅助节点上执行聚合框架任务?
下面是一个总是在主节点上执行的示例:
DBClientConnection c;
bo res;
vector<bo> pipeline;
pipeline.push_back( BSON( "$match" << BSON( "firstName" << "Stephen" ) ) );
c.connect( "localhost:12345" );
c.runCommand( "test", BSON( "aggregate" << "people" << "pipeline" << pipeline ), res );
cout << res.toString() << endl;
我需要在二级执行它。
虽然我还没有使用MongoDB的C++驱动程序,但只需将读取首选项设置为secondary,就可以很容易地在secondary上运行聚合。例如,在外壳上:
mongo -u admin -p <pwd> --authenticationDatabase admin --host
RS-repl0-0/server-1.servers.example.com:27017,server-2.servers.example.com:27017
RS-repl0-0:PRIMARY> use test
switched to db test
RS-repl0-0:PRIMARY> db.setSlaveOk() // Ok to run commands on a slave
RS-repl0-0:PRIMARY> db.getMongo().setReadPref('secondary') // Set read pref
RS-repl0-0:PRIMARY> db.getMongo().getReadPrefMode()
secondary
RS-repl0-0:PRIMARY> db.zips.aggregate( [
... { $group: { _id: "$state", totalPop: { $sum: "$pop" } } },
... { $match: { totalPop: { $gte: 10*1000*1000 } } }
... ] )
{ "_id" : "CA", "totalPop" : 29754890 }
{ "_id" : "FL", "totalPop" : 12686644 }
...
可以从MongoDB日志中验证这确实在辅助服务器上运行:
...
2016-12-05T06:20:14.783+0000 I COMMAND [conn200] command test.zips command: aggregate { aggregate: "zips", pipeline: [ { $group: { _id: "$state", totalPop: { $sum: "$pop" } } }, {
$match: { totalPop: { $gte: 10000000.0 } } } ], cursor: {} } keyUpdates:0 writeConflicts:0 numYields:229 reslen:338 locks:{ Global: { acquireCount: { r: 466 } }, Database: { acquire
Count: { r: 233 } }, Collection: { acquireCount: { r: 233 } } } protocol:op_command 49ms
...
请注意,这也适用于分片MongoDB集群的辅助设备。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 在没有Xcode的情况下在Mac捆绑包中嵌入框架
- catch框架有没有办法比较流或文件
- 如何将不同的可执行文件合并到一个窗口框架中进行编码?像浏览器一样
- 使用Qt框架在c ++类中创建API调用
- 如何在ECS框架中更新组件数据和通知系统
- VSCode IntelliSense无法识别SDL框架的SDL_image扩展库
- 用于C++的静态二进制检测或二进制重写工具和框架
- GoogleMock是否仍然打算与任何测试框架一起使用?
- 带有框架的 QGraphicsTextItem 的 QPropertyAnimation 会使文本抖动
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- Mongocxx无法使用SSL连接到mongoDB
- 获取 OID(类型::b_oid)作为 MongoDB C++驱动程序中的字符串
- 修改生成文件以简化框架 #include 路径
- 无敌框架并受到伤害等级
- 如何在提升构建中设置环境变量以进行提升单元测试框架?
- 使用 gtest 框架在单元测试代码中检查目标对象的私有变量的最佳实践是什么?
- Xcode 中的加速框架以获得 A 的 QR 分解,但找不到 zgeqrf
- 是否可以在 Xcode C++项目中包含框架?
- 辅助节点上的MongoDB聚合框架