mongodb command

wangxiangtao發表於2011-07-07

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中,是不存在集合的概念的,而mongodbdb ,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 鎖定模式也不進行讀阻塞。 以下用3session 模擬鎖機制

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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章