尽管我没有使用MongoDB的C++驱动程序,但通过简单地将读取首选项设置为辅助,可以轻松地在辅助上运行聚合。对于例如在外壳:
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集群的次级为好。
来源:This post关于如何执行辅助汇总。
你能举一个例子,说明你有什么以及你想从中得到什么?这是一个非常模糊的问题。 – Eumcoz
@Eumcoz,我编辑了我的问题并提供了示例。 – Lonelius