首页 > 技术文章 > mongodb 常用的命令

wangshouchang 2017-05-29 20:00 原文

mongodb 常用的命令

对数据库的操作,以及登录

1 进入数据库

use admin

2 增加或修改密码

db.addUser('wsc', '123')

3查看用户列表

db.system.users.find()

4 #用户认证

db.auth('wsc', 123)

5 #删除用户

db.removeUser('wsc')

6 #查看所有用户

show users

7 #查看所有数据库

show dbs

8 #查看所有的collection集合

show collections

9 #查看各个collection的状态

db.printCollectionStats()

10 #查看主从复制状态

db.repairDatabase()

11 #修复数据库

db.repairDatabase()

12 #设置profiling,0:off 1:slow 2 all

db.setProfilingLevel(1)

13 #查看profiling

show profiling

14 #拷贝数据库

db.copyDatabase('wsc', 'wsc copy')

15 #删除集合collection

db.wsc.drop()

16 #删除当前数据库

db.dropDatabase()




mongodb的增删改查

1 #存储嵌套的对象

db.foo.save(
	{
		‘name’:wsc,
		’age’:25,’address’:
		{
			‘city’:’shnaghai’,
			’Province’:’shnaghai’
		}
	}
)

2#存储数组对象

db.foo.save(
	{
		‘name’:wsc,
		’age’:25,
		’address’:
		[
			‘anhui Province’,
			’anhui Province’
		]
	}
)

3 #根据query条件修改,如果不存在则插入,允许修改多条记录

db.foo.update(
	{
		‘age’:’25’
	},
	{
		‘$set’:
		{
			‘name’:’xingoo’
		}
	},
	upsert=true,
	multi=true
)

4 #删除yy=5的记录

db.foo.remove({'name':'wsc'})

5 #删除所有的记录

db.foo.remove()




索引

1 #增加索引:1 asc -1 desc

db.foo.ensureIndex(
	{
		firstname:1,
		lastname:-1
	},
	{
		unieap:true
	}
)

2 #索引子对象

db.foo.ensureIndex({‘Al.Em’:!})

3 #查看索引信息

db.foo.getIndexes()
db.foo.getIndexKeys()

4 #根据索引名删除索引

db.foo.dropIndex(‘Al.Em_1’)




查询,利用条件操作符进行查询,并和sql语句进行对比

条件操作符

 1 gt ---- >
 2 $lt ---- <
 3 $gte ---- >=
 4 $lte ---- <=
 5 $ne ---- != 、<>
 6 $in ---- in
 7 $nin ---- not in
 8 $all ---- all
 9 $or ---- or
 10 $not ---- 反匹配

1 查询所有记录

db.foo.find()   --- select * from foo

2 查询某列非重复的记录

db.foo.distinct('wsc') ---- select distinct name from foo

3 查询age = 22的记录

db.foo.find({"age":22)  ----- select * from foo where age = 22

4 查询age > 22的记录

db.foo.find({age:{$gt:22}}) ---- select * from foo where age > 22

5 查询age < 22 的记录

db.foo.find({age:{$lt:22}}) ---- select * from foo where age < 22

6 查询age <= 22 的记录

db.foo.find({age:{$lte:22}}) ---- select * from foo where age <= 22

7 查询age => 22 且 age <= 26 的记录

db.foo.find({age:{$gte:22,$lte:26}}) ---- select * from foo where age => 22 and age <= 26

8 #查询name中包含wsc的数据

db.foo.find({name:/wsc/}  ---- select * from foo where name like '%wsc%'

9 #查询name中以wsc开头的数据

db.foo.find({name:/^wsc/}) ---- select * from foo where name like 'wsc%'

10 #查询指定列name、age的数据

db.foo.find({}, {name:1, age;1})  ---- select name , age from foo 

11 #查询制定列name、age数据,并且age > 22

db.foo.find({age:{$lt:22}}, {name:1, age;1})  ---- select name , age from foo where age > 22

12 #按照年龄排序

db.foo.find().sort({age:1}) 升序
db.foo.find().sort({age:-1}) 降序

13 #查询name=wsc .age=25的数据

db.foo.find({name:'wsc', age:25}) ---- select * from foo where name = 'wsc' and age = '25'

14#查询前5条数据

db.foo.find().limit(5)  ---- select top 5 from fo0

15 #查询10条以后的数据

db.foo.find().skip(10)  ---- select * from foo where id not in (select top 10 from foo)

16 #查询在5-10之间的数据

db.foo.find().limit(10).skip(5)

17 #or与查询

db.foo.find({$or:[{age:22},{age:25}]})  ---- select * from foo where age = 22 or age = 25
18 #查询第一条数据
db.foo.findOne()
db.foo.find.limit(1)  ---- select top 1 * from foo

19 #查询某个结果集的记录条数

db.foo.find({age:{$gte:25}}).count()  ---- select count(*)  from where age >= 20

20 #按照某列进行排序(不懂)

db.foo.find({sex:{$exit:true}}).count()  ---- select count(sex) from foo

21 #查询age取模10等于0的数据

db.foo.find('this.age%10 == 0')
db.foo.find({age:{$mod:[10,0]}}) 

22 #匹配所有

db.foo.find({age:{$all:[22,25]}})

23 #查询不匹配name=X*带头的记录

db.foo.find({name:{$not:/^x.*/}})

24 #排除返回age字段

db.foo.find({name:'wsc'},{age:0})

25 #判断字段是否存在

db.foo.find({name:{$exits:true}})

对collection的管理

1 #查看collection数据大小

db.wsc.dataSize()

2 #查看collection状态

db.wsc.stats()

3 #查询所有索引的大小

db.wsc.totalIndexSize()

参考 连接
http://www.100mian.com/mianshi/mongodb/49183.html

推荐阅读