奇淫技巧
我们Mongodb客户端使用PHP,按照常规的调试方法无非就是print_r()
var_dump
,如果遇到简单的应用倒是无妨,可遇到复杂的,每次刷新页面打开浏览器终端,查看结果,一次次的刷新,一秒秒过去,等待着结果是否感到很烦躁?这里教你一招
aggregate($params);
上面的$params是聚合分析输入的一坨参数,问题出在这里,那么我们
print_r(json_encode($params, true));
die();
aggregate($params);
假如你使用的是Robomongo GUI 客户端,把打印的结果转移到这里,刷新问题解决!
mongodump
--help
-h, --host=<hostname>
-u, --username=<username> username for authentication
-p, --password=<password> password for authentication
-d, --db=<database-name> database to use
-c, --collection=<collection-name> collection to use
-o, --out=<filename> output file; if not specified
-q, --query=<json> query filter, as a JSON string
mongodump -d user -o /data/backup
mongostore
--help
-h, --host=<hostname>
-u, --username=<username> username for authentication
-p, --password=<password> password for authentication
-d, --db=<database-name> database to use
-c, --collection=<collection-name> collection to use
--drop if collection exsit drop it
-o, --out=<filename> output file; if not specified
-q, --query=<json> query filter, as a JSON string
mongorestore -d user --drop data/backup/user/
mongoexport
--help
-h, --host=<hostname>
-u, --username=<username> username for authentication
-p, --password=<password> password for authentication
-d, --db=<database-name> database to use
-c, --collection=<collection-name> collection to use
-o, --out=<filename> output file; if not specified
-q, --query=<json> query filter, as a JSON string
更多参阅 https://docs.mongodb.com/manual/reference/program/mongoexport/
导出db是user,collection是user的数据
mongoexport --db user --collection user --out ~/user.json
mongoexport
--help
-h, --host=<hostname>
-u, --username=<username> username for authentication
-p, --password=<password> password for authentication
-d, --db=<database-name> database to use
-c, --collection=<collection-name> collection to use
更多参阅 https://docs.mongodb.com/manual/reference/program/mongoimport/
mongoimport --db zips --collection zips ~/Downloads/zips.json
db.zips.ensureIndex({pop: 1});
db.zips.ensureIndex({city: 1, state: 1});
db.user.ensureIndex({familly.father: 1});
db.zips.ensureIndex({loc:'2d'});
db.zips.ensureIndex({state: 1}, {unique: true});
db.zips.ensureIndex({state: 1}, {sparse: true});
加入有一些文档不存在state属性,那么不予显示该文档。
db.getCollection('zips').find({
city: 'AGAWAM'
}).explain()
db.zips.getIndexes();
db.runCommand({serverStatus: 1});
serverStatus命令的输出内容较为丰富。统计的信息包含错误、B树访问率、打开连接数、以及总的插入、更新、查询和删除。下面是一段节选后的命令输出:
{
"host" : "MtaoMacBook-Pro.local",
"advisoryHostFQDNs" : [
"duforddemacbook-pro.local"
],
"version" : "3.2.6",
"process" : "mongod",
"pid" : NumberLong(486),
"uptime" : 17094,
"uptimeMillis" : NumberLong(17094108),
"uptimeEstimate" : 4284,
"localTime" : ISODate("2016-05-20T05:14:40.232Z"),
"asserts" : {
"regular" : 0,
"warning" : 0,
"msg" : 0,
"user" : 6,
"rollovers" : 0
},
"connections" : {
"current" : 14,
"available" : 3262,
"totalCreated" : NumberLong(43)
},
"extra_info" : {
"note" : "fields vary by platform",
"page_faults" : 3116
},
"globalLock" : {
"totalTime" : NumberLong("17094093000"),
"currentQueue" : {
"total" : 0,
"readers" : 0,
"writers" : 0
},
"activeClients" : {
"total" : 21,
"readers" : 0,
"writers" : 0
}
},
"writeBacksQueued" : false,
"mem" : {
"bits" : 64,
"resident" : 108, //所占物理内存数量
"virtual" : 2586, //虚拟内存字节数
"supported" : true,
"mapped" : 0,
"mappedWithJournal" : 0
},
....
},
"ok" : 1
}
globalLock部分很重要,揭示了服务器花在写锁上的总时间,currentQueue是表示具体的瓶颈表述。 mem部分显示了mongod进程是如何使用内存的
db.runCommand({top:1});
top命令会显示每个数据库的操作计数器。如果应用使用了多个物理数据库,想看看平均耗时,那么这是个很有用的命令。
当前正在做什么
Mongodb的核心。对于大多数应用程序而言,在一台服务器上保存完整数据集是完全可以接受的。但随着数据量的增长,以及应用程序对读写吞吐量的要求越来越高,普通服务器的方案渐渐变得捉襟见肘了。尤其是这些服务器可能无法分配足够的内存,或者没有足够的CPU核数来有效处理工作负荷。分片技术横空出世。
(未完待续)