[root@localhost scripts]# more find.js
db.metric_pl.aggregate({$group: {_id: "$month", totalcnt: {$sum: 1}}}).forEach( function(c){print(c._id,"-->",c.totalcnt);} )
命令执行
/usr/local/services/mongodb/bin/mongo 192.168.1.135:28001/db_test find.js>>aa.txt
shell脚本里也可以执行,需要在$前面加上\
[root@localhost scripts]# more run_mongodb.sh
#!/bin/bash
/usr/local/services/mongodb/bin/mongo 192.168.1.135:28001 << EOF
use db_test;
db.metric_pl.aggregate([{"\$group": {_id: "\$month", totalcnt: {"\$sum": 1}}}]);
EOF