您的位置:首页 > 数据库 > Mongodb

MongoDB操作命令

2016-07-26 17:14 393 查看
表,索引

1. show dbs 查看当前系统中的数据库

2. show collections 查看创建的表

3. use imooc 切换数据库 (如果没有,会自动创建)

4. db.dropDatabase() 删除当前数据库

5. 对表删除:db.imooc_collection.drop()

6. db.imooc_collection.getIndexes()查看集合索引情况

7. 创建索引:db.imooc_collection.ensureIndex({x:1}),1表示方向。1:正向,-1:逆向排序

8. 如果数据量非常大,创建索引需要消耗一定的时间,这时需要在使用数据库之前就将索引创建完毕,否则会对数据库性能造成较大影响

数据增删改查

9. db.imooc_collection.insert({x:1})

10. db.imooc_collection.find()进行查询

11. _id:全局不能重复,可以指定值

12. db.imooc_collection.insert({x:2,_id:1})

13. for(i=3;i<200 i++) db.imooc_collection.insert({x:i}) 插入多条数据

14. 为防止误操作,remove必须传参,否则报错

15. db.imooc_collection.remove({x:1}),默认删除所有满足条件的数据

16. update至少两个参数 db.imooc_collection.update({x:1},{x:999})

17. set操作符为部分操作符,按条件更新
db.imooc_collection.update({z:100},{$set:{y:999}},false,true)


18. 更新不存在数据,自动创建这条数据 db.imooc_collection.update({x:1},{x:999},true)

19. 默认情况,update只会更新第一条找到的数据(防止误操作)

20. db.imooc_collection.update({c:1},{$set:{c:2}},false,true) 倒数第二个 false表示数据不存在,则不需要创建,倒数第一个true表示批量更新,如果为false,只更新第一条

21. db.imooc_collection.find({x:1})进行查询

22. db.imooc_collection.find().count() 返回行数

23. db.imooc_collection.find().skip(3).limit(2).sort({x:1}) 按照x进行排序,跳过前3条数据,查询两条数据

启动mongodb:

1. 进入bin目录

2. mongod -dbpath “d:\mongodb\data\db”

3. 开一个cmd窗口,进入mongodb的bin目录,输入mongo或者mongo.exe

将MongoDB安装为windows服务

1. mongod –dbpath “d:\mongodb\data\db” –logpath “d:\mongodb\data\log\MongoDB.log” –install –serviceName “MongoDB”

2. 启动mongodb服务 NET START MongoDB

3. 关闭服务和删除进程 NET stop MongoDB (关闭服务)

mongod –dbpath “d:\mongodb\data\db” –logpath “d:\mongodb\data\log\MongoDB.log” –remove –serviceName “MongoDB” (删除,注意不是–install了)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: