MongoDB DBA常用的NoSQL语句 参考学习

 

查看帮忙命令

 

 > hlep  --server级别 
> db.help()  --db级别
> db. collectionname . help()   --集合级别

 

 

查看所有数据库

 

>show dbs 
--新建的数据库并不在数据库的列集合中,要显示它,我们需要向新建的数据库创建集合。

 

 

查看当前数据库

 

>db 

 

 

创建数据库

 

> use DATABASE_NAME 

 

切换到某个数据

 

> use DATABASE_NAME 

 

删除当前数据库,会把磁盘上的数据库文件一并删除

 

> db .dropDatabase() 

 

修复数据库

 

>  db .repairDatabase() 

 

拷贝数据库test为test999

 


>
 db.copyDatabase( ‘test‘, ‘test999‘) 

 

查看所有集合(关系型数据库叫表)

 

 > show collections 
> show tables

 

 

查看各集合的状态

 

>  db .printCollectionStats() 

 

 

新建集合

 

 

db.createCollection( "集合名", {集合的大小属性:大小值,集合的增长属性:增长值,集合的最大容量属性:最大容量值,,,等等}) 
>db.createCollection( "table1")

 

 

删除集合table1

 

>
db .table1 .drop() 

 

重命名集合table1为table101

 

>
db .table1 .renameCollection(" table101") 

 

 

查看集合table1的所在的数据库名称

 

>
db .table1 .getDB() 

 

查看集合table1的状态

 

db .table1 .stats() 

 

查询集合

 

>db.table1.
find()  --查询集合的所有数据 

>db.table1.findOne()  --查询集合的第一条数据

>db.table1.count()  --总行数

>db.table1.totalSize()  --集合的总大小

>db.table1.storageSize()  --集合的储存空间大小

>db.table1.distinct( "hid")  --只查询列hid,并列出该列的不重复的值

>db.table1. find({ "hid": 2})  --hid=2

>db.table1. find({ "hid": 2, "hid2": 3})  --hid=2 and hid2=3

>db.table1. find({$ or:[{ "hid": 2},{ "hid2": 3}]})  --hid=2 or hid2=3

>db.table1. find({ "hid":{$gt: 1}})  --hid>1

>db.table1. find({ "hid":{$gte: 1}})  --hid>=1

>db.table1. find({ "hid":{$lt: 2}})  --hid<2

>db.table1. find({ "hid":{$lte: 2}})  --hid<=2

>db.table1. find({ "hid":/ 2/})  --hid like ‘%2%‘

>db.table1. find({ "hid":/^ 2/})  --hid like ‘2%‘

>db.table1. find({},{ "hid": 1, "go2": 1})  --查询指定的两列hid和go2

>db.table1. find({ "hid": 23},{ "hid": 1, "go2": 1})  --select hid,go2 from table1 where hid=23

>db.table1. find({},{ "hid": true, "go2": true}) --查询指定的两列hid和go2

>db.table1. find({ "hid": 23},{ "hid": true, "go2": true}) --select hid,go2 from table1 where hid=23

>db.table1. find(). sort({ "hid": 1})  --查询结果按hid字段顺序排序

>db.table1. find(). sort({ "hid": -1})  --查询结果按hid字段降序排序

>db.table1. find().limit( 2)  --查询前两条数据

>db.table1. find().skip( 2)  --查询第2条以后的所有数据

>db.table1. find().limit( 3).skip( 2)  --查询第2条以后的后面3条数据

>db.table1. find({ "hid": 2}).count()  --查询hid=2的总行数

 

 

插入集合(关系型数据库叫行,mongodb叫文档,insert后不需要手工提交,其他会话可以看到)

方法1 

 

db.collectionname.
insert({字段名:  "字段值"}) 

>db.table1. insert({hid: "1"})

 

方法2

 

db.集合名.save({字段名: 
"字段值"}) 

>db.table1.save({hid: 2,hname: "hao2"})

循环插入table1集合

for ( var i =  0; i <  30; i++) db.table1.save({hid:  "u_" + i, age:  22 + i, sex: i %  2});

 

 

更新集合( 关系型数据库叫行,mongodb叫文档 )

 

>db.table1.update({
‘hid‘:2},{ $set:{ ‘hid‘:4}},{multi: true}) 

把hid字段的值由2修改为4

update默认修改第一条发现的行,multi: true集合示修改多行

 

 

删除集合( 关系型数据库叫行,mongodb叫文档 )

 

>db.table1.
remove({ ‘hid2‘: 5})  --删除hid2为5的行 

>db.table1. remove({})  --删除所有行

 

 

创建索引

 

创建索引后,数据库目录下会多出一个index-开头的文件

 

>db.table1.ensureIndex({
"hid" : 1}) 

>db.table1.ensureIndex({ "hid" : 1, "hid2" :- 1},{ unique: true});

1表示升级排序,- 1表示降序排序, "hid" : 1, "hid2" :- 1表示组合索引, unique: true表示唯一性索引

 

 

查看集合table1的索引信息

 

>
db .table1 .getIndexes() 

 

删除集合table1的索引

 

>
db .table1 .dropIndexes() 

 

创建用户

 

>
db .createUser({ user: "admin",pwd: "admin",roles:[{role: "userAdminAnyDatabase",db: "admin"}]}); 

> db .createUser({ user: "admin1",pwd: "admin1",roles:[{role: "root",db: "admin"}]});

 

 

查询用户的三种方法

 

 

> use  admin   

> show  users 

> db .system .users .find()

> db .system .users .find() .pretty()

 

 

查询当前用户

 


db .runCommand({ connectionStatus: 1}) 

 

 

创建replicate的语句,创建replicate前,两个节点启动的时候必须要加上replSet参数,且replSet参数值必须一样,例如为replicate1

 

 

 > use admin 

> config={_id: ‘replicate1‘,members:[{_id:0,host: ‘172.22.1.157:27017‘},{_id:1,host: ‘172.22.1.158:27017‘}]}

> rs.initiate(config)

> rs.status(config)

> rs.status()

> show dbs

再到从库执行如下

> rs.slaveOk()

> show dbs

 

 

查看replicate复制状态

 

>  db .printReplicationInfo() 

 

 

查询集合table1的shard版本信

 

>
db .table1 .getShardVersion() 

 

 

查看shard分片信

 

> db .printShardingStatus() 

 

 

启动

 

使用mongod命令,后面接各项参数

mongod -f /mongodb/mongodb.conf

 

 

关闭

方法1、

 

 


use  admin 
db.shutdownServer()

 

 

方法2、(如果启动用了-f,则关闭的时候也要加-f)

 

 

mongod -- shutdown 

mongod -- shutdown -f /mongodb/mongodb.conf

 

方法3、(不要加-9,否则下次启动会无法启动,需要删除mongod.lock文件或使用mongod启动时必须加上–repair才能启动成功)

 


kill <mongod process 
ID> 

 

mongodb切换日志

运行时间长了,mongodb的日志会很大,可以执行切换,生成新的日志,把旧的日志删除,执行这个语句,不影响mongodb服务的运行

 

 

use  admin 

db .runCommand({ logRotate: 1})

 

执行后,会生成新的名称不变的日志文件,之前的日志会以时间格式命名保存下来

如果是replicate环境,不会影响replicate,primary执行这个语句只切换自己的日志,不影响secondary环境的服务,也不会切换secondary环境的日志;secondary执行这个语句只切换自己的日志,不影响primary环境的服务,也不会切换primary环境的日志

mongodb的日志文件,重启后还在,且会把重启涉及的关闭启动信息追加到这个日志文件中

–原文地址:http://blog.itpub.net/30126024/viewspace-2645995/