数据库

 首页 > 数据库 > MongoDB > Mongodb数据库常用命令介绍

Mongodb数据库常用命令介绍

分享到:
【字体:
导读:
         摘要:(1)对于初学者,要善于使用help命令,然后看命令注释,helpdb.help()helpondbmethodsdb.mycoll.help()helponcollectionmethodsrs.help()helponreplicasetmethodshelpconnectconnectingtoadbhelphe...

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的测试中可以看出,一个会话等待,另外一个会话的读也阻塞了。

路漫漫,生活继续,工作继续………………………………….

Mongodb数据库常用命令介绍
分享到:
MongoDB 2.0新功能之compact command
MongoDB 2.0新功能之compact command 1.Compact Command整理命令     事实上我也不知道到底应该将这个命令翻译成什么比较合适,它的作用是整理collection,将其中的内容全部抽出后重新排列整齐,有点类似系统的磁盘整理,所以我就这么叫它了。在2.0版本之前,需要进行类似的操作,要使用整理命令是以collection为单位...
使用mongocola操作MongoDB数据库
使用mongocola操作MongoDB数据库前言      在开始正文之前,感谢博客园的Nosql爱好者对于MongoCola工具的试用(使用)。特别感谢 呆呆 这位朋友的Bug报告,让我纠正了一个很严重的Bug。同时也感谢以前的多个网友在博客留言中给我的意见,建议,以及BUG的修正方案。这个工具的进步离不开你们的贡献。最新版的MongoCola...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……