mongodb command

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/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/8117479/viewspace-1052109/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值