Mongodb 常用命令介绍
(1)对于初学者,要善于使用 help 命令, 然后看命令注释,
> help
db.help() help on db methods
db.mycoll.help() help on collection methods
rs.help() help on replica set methods
help connect connecting to a db help
help admin administrative help
help misc misc things to know
show dbs show database names
show collections show collections in current database
show users show users in current database
show profile show most recent system.profile entries with time >= 1ms
use set current database
db.foo.find() list objects in collection foo
[@more@]Mongodb 常用命令介绍
(1)对于初学者,要善于使用 help 命令, 然后看命令注释,
> help
db.help() help on db methods
db.mycoll.help() help on collection methods
rs.help() help on replica set methods
help connect connecting to a db help
help admin administrative help
help misc misc things to know
show dbs show database names
show collections show collections in current database
show users show users in current database
show profile show most recent system.profile entries with time >= 1ms
use set current database
db.foo.find() list objects in collection foo
db.foo.find( { a : 1 } ) list objects in foo where a == 1
it result of the last line evaluated; use to further iterate
exit quit the mongo shell
(2) 一个服务器上可以 包含多个数据库(dbs),一个db 包含多个集合(collections), 此结构与mysql非常类似,当然在关系型数据库mysql中,是不存在集合的概念的,而mongodb中db ,collection 都是隐式创建的
> show dbs //列出当前服务器的所有数据库
admin
local
richinfo
test
> use test1 // 切换数据库
switched to db test1
> db // 显示当前数据库
test1
> show collections //列出当前数据库中所有的集合
> db.ee_info.save({name:"gabriel",position:"DBA",age:29,sex:"F"});
> show collections
ee_info
system.indexes
> show dbs
admin
local
richinfo
test
test1
以上隐式创建了 一个 test1 的db; ee_info的集合。
(3) mongodb 的数据库复制功能 db.copyDatabase() 可以在local 和remote 服务器上进行数据库的复制,db.cloneDatabase() 也可以实现remote 数据库的复制。
> show dbs
admin
local
richinfo
test
test1
> db.copyDatabase("test1","test2");
{ "ok" : 1 }
> show dbs
admin
local
richinfo
test
test1
test2
> use test2
switched to db test2
> db.ee_info.find();
{ "_id" : ObjectId("4e155c1ec26522805e8d4d6e"), "name" : "gabriel", "position" : "DBA", "age" : 29, "sex" : "F" }
> db.dropDatabase();
{ "dropped" : "test2", "ok" : 1 }
> show dbs
admin
local
richinfo
test
test1
(4) 使用getSisterDB() 获取其他数据库的引用, 一般情况下我们使用 use 语句可以实现数据库的切换, 但是如果我们在current database A 操作 database B 中的集合, getSisterDB() 就能很好的引用
> use test
switched to db test
> db
test
> test1.ee_info.find();
Thu Jul 7 16:21:41 ReferenceError: test1 is not defined (shell):0
> test1=db.getSisterDB("test1");
test1
> test1.ee_info.find();
{ "_id" : ObjectId("4e155c1ec26522805e8d4d6e"), "name" : "gabriel", "position" : "DBA", "age" : 29, "sex" : "F" }
> db
Test
(4) 作为DBA可以使用fsync 强制性的将内存中的数据写于数据文件。Asymc 表示异步写
> db.runCommand({fsync:1});
{ "errmsg" : "access denied; use admin db", "ok" : 0 }
> use admin
switched to db admin
> db.runCommand({fsync:1});
{ "numFiles" : 8, "ok" : 1 }
> db.runCommand({fsync:1,async:true});
{ "numFiles" : 8, "ok" : 1 }
(5)作为DBA 有时为了备份,整理数据库,需要将系统锁定,mongodb 的 锁定模式也不进行读阻塞。 以下用3个session 模拟锁机制
Session1 锁定操作:
> db
admin
> db.copyDatabase("test1","test2");
{ "ok" : 1 }
> show dbs
admin
local
richinfo
test
test1
test2
> db.runCommand({fsync:1,lock:1});
{
"info" : "now locked against writes, use db.$cmd.sys.unlock.findOne() to unlock",
"ok" : 1
}
>
Session2:
[mongodb@tes102 ~]$ cd /soft/mongodb-linux-x86_64-1.6.5/bin/
[mongodb@tes102 bin]$ ./mongo 192.168.18.102:10001
MongoDB shell version: 1.6.5
connecting to: 192.168.18.102:10001/test
> use test1
switched to db test1
> db.ee_info.find(); //读没有阻塞
{ "_id" : ObjectId("4e155c1ec26522805e8d4d6e"), "name" : "gabriel", "position" : "DBA", "age" : 29, "sex" : "F" }
> db.ee_info.save({name:"wangxiangtao",age:23}); //写已经产生等待
Session3
[root@tes102 ~]# su - oracle
su: user oracle does not exist
[root@tes102 ~]# su - mongodb
[mongodb@tes102 ~]$ cd /soft/mongodb-linux-x86_64-1.6.5
[mongodb@tes102 mongodb-linux-x86_64-1.6.5]$ cd bin/
[mongodb@tes102 bin]$ ./mongo 192.168.18.102:10001
MongoDB shell version: 1.6.5
connecting to: 192.168.18.102:10001/test
> use test2
switched to db test2
> db.ee_info.find();
^[[A^[[A^[[A^[[A^[[A^[[A^[[A^[[A^[[A^[[A // 直接等待
-----------------------在session1 直接解锁, session2, session3 均恢复正常
> db.$cmd.sys.unlock.findOne();
{ "ok" : 1, "info" : "unlock requested" }
>
在session3 中查找数据:
> test2=db.getSisterDB("test2");
test2
> db.ee_info.find();
{ "_id" : ObjectId("4e155c1ec26522805e8d4d6e"), "name" : "gabriel", "position" : "DBA", "age" : 29, "sex" : "F" }
{ "_id" : ObjectId("4e1571770a2bdd1c776e5239"), "name" : "wangxiangtao", "age" : 23 }
> test1=db.getSisterDB("test1");
test1
> db.ee_info.find();
{ "_id" : ObjectId("4e155c1ec26522805e8d4d6e"), "name" : "gabriel", "position" : "DBA", "age" : 29, "sex" : "F" }
{ "_id" : ObjectId("4e1571770a2bdd1c776e5239"), "name" : "wangxiangtao", "age" : 23 }
>
(5) 验证集合的正确性:
> show collections
system.indexes
tt
> db.tt.validata();
Thu Jul 7 17:22:02 TypeError: db.tt.validata is not a function (shell):0
> db.tt.validate();
{
"ns" : "blog.tt",
"result" : "nvalidaten firstExtent:0:2500 ns:blog.ttn lastExtent:0:2500 ns:blog.ttn # extents:1n datasize?:52 nrecords?:1 lastExtentSize:3328n padding:1n first extent:n loc:0:2500 xnext:null xprev:nulln nsdiag:blog.ttn size:3328 firstRecord:0:25b0 lastRecord:0:25b0n 1 objects found, nobj:1n 68 bytes data w/headersn 52 bytes data wout/headersn deletedList: 0000000100000000000n deleted: n: 1 size: 3084n nIndexes:1n blog.tt.$_id_ keys:1n",
"ok" : 1,
"valid" : true,
"lastExtentSize" : 3328
}
>
总结: mongodb 与oracle mysql 比起来, 差距还是蛮大的, 特别从lock 的测试中可以看出,一个会话等待,另外一个会话的读也阻塞了。
路漫漫,生活继续,工作继续………………………………….
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8117479/viewspace-1052109/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/8117479/viewspace-1052109/