mongodb新手入门,mongodb命令学习

下面来总结一下mongodb新手入门的经常使用命令吧。要是您是mongodb新手,能够看下。mongodb

1,show dbs 查询mongodb里面的数据库列表

enter image description here

若是想查看当前链接在哪一个数据库下面,能够直接输入db数据库

enter image description here

想切换到test数据库下面 use test数组

enter image description here

二、db.getCollectionNames() 或者show collections 有哪些数据库表名字

enter image description here

效果差很少,而后能够对数据库表进行操做服务器

三、查询条数 db.foo.count()

enter image description here

四、数据库的增删改

存储嵌套的对象

db.foo.save({'name':'ysz','address':{'city':'beijing','post':100096},'phone':[138,139]})

enter image description here

查看存储的对象:

enter image description here

存储数组对象

db.user_addr.save({'Uid':'yushunzhi@sohu.com','Al':['test-1@sohu.com','test-2@sohu.com']})
 #根据query条件修改,若是不存在则插入,容许修改多条记录  db.foo.update({'yy':5},{'$set':{'xx':2}},upsert=true,multi=true) #删除yy=5的记录  db.foo.remove({'yy':5}) #删除全部的记录  db.foo.remove()

五、索引

增长索引:1(ascending),-1(descending)post

db.things.ensureIndex({firstname: 1, lastname: 1}, {unique: true});

索引子对象

 db.user_addr.ensureIndex({'Al.Em': 1})

查看索引信息

db.deliver_status.getIndexes() db.deliver_status.getIndexKeys()

根据索引名删除索引

 db.user_addr.dropIndex('Al.Em_1')

六、查询

 # 查找全部  >db.foo.find() #查找一条记录  >db.foo.findOne() #根据条件检索10条记录  >db.foo.find({'msg':'Hello 1'}).limit(10) #sort排序  > db.deliver_status.find({'From':'yushunzhi@sohu.com'}).sort({'Dt',-1}) >db.deliver_status.find().sort({'Ct':-1}).limit(1) #count操做  > db.user_addr.count() >#distinct操做 >db.foo.distinct('msg') #操做  >db.foo.find({"timestamp": {"$gte" : 2}}) #子对象的查找  > db.foo.find({'address.city':'beijing'})

七、 管理

 查看collection数据的大小 >db.deliver_status.dataSize() #查看colleciont状态  >db.deliver_status.stats() #查询全部索引的大小  > db.deliver_status.totalIndexSize()

八、备份与恢复

先介绍下命令语法:spa

mongodump -h dbhost -d dbname -o dbdirectoryrest

-h:MongDB所在服务器地址,例如:127.0.0.1,固然也能够指定端口号:127.0.0.1:27017code

-d:须要备份的数据库实例,例如:test对象

-o:备份的数据存放位置,例如:c:\data\dump,固然该目录须要提早创建,在备份完成后,系统自动在dump目录下创建一个test目录,这个目录里面存放该数据库实例的备份数据。排序

mongorestore -h dbhost -d dbname --directoryperdb dbdirectory

-h:MongoDB所在服务器地址

-d:须要恢复的数据库实例,例如:test,固然这个名称也能够和备份时候的不同,好比test2

--directoryperdb:备份数据所在位置,例如:c:\data\dump\test,这里为何要多加一个test,而不是备份时候的dump,读者本身查看提示吧!

--drop:恢复的时候,先删除当前数据,而后恢复备份的数据。就是说,恢复后,备份后添加修改的数据都会被删除,慎用哦!

实际操做:

//cd 到bin目录下 cd c:\data\db\bin //备份 mongodump -h 127.0.0.1:27017 -d test -o c:\data\dump //恢复 mongorestore -h 127.0.0.1:27017 -d test --directoryerdb c:\data\dump\test
相关文章
相关标签/搜索