MongoDB常用命令

MongoDB入门教程(1)

      (一)MongoDB介绍

       MongoDB是一个NoSQL数据库,它是跨平台的,基于分布式文件存储的数据库,由C++语言编写的(这点很重要,则表示在Linux下需要安装gcc环境)。MongoDB是以文档的形式存储数据,数据结构由键值(key:value)对组成,类似JSON。

       MongoDB结构,最小的单位为文档(类似MySQL的行),每一个文档用的是BSON形式来存储(类似JSON),文档的上一层为集合(类似MySQL的表),再上一级为库(类似MySQL的数据库)。

 

         (二)MongoDB安装

          MongoDB的安装分为Linux和Windows。在Windows下的安装及其简单.

          https://jingyan.baidu.com/article/6079ad0eb74fa828fe86db5f.html    windows下安装教程。

          在Linux下的安装大致分为Ubuntu版本和CentOS两种版本。

          https://jingyan.baidu.com/article/0a52e3f4217e65bf62ed729a.html      CentOS下安装教程。

          https://www.cnblogs.com/zhangdaicong/p/7492494.html   Ubuntu下安装教程。

 

        (三)MongoDB的指令操作

          按照上面的安装完成后,就可以使用MongDB了,操作的指令都是一样的,我自己使用的是Linux,所以下面都以Linux为例。

        找到你安装MongDB目录,如果不知道,可以通过ps -ef | grep mongo 来查看mongodb所在的位置,在Linux下,MonggoDB是随着Linux一起启动的。如果你不小心关闭了,可以通过命令service mongodb start来启动服务(注意,此处的启动服务不是使用MongDB),通过命令 service mongodb stop关闭服务。

          下面才是真正的使用MongoDB,上面已经教了怎么查找mongpdb的位置,找到位置之后呢,通过命令 ./mongo 进入到MongoDB的真正服务中,也就是到了此处才是真正的使用MongoDB。

           总结一波:


 
 
  1. pe -ef | grep mongodb 查看mongodb的目录和看是否启动
  2. service mongodb start 启动mongodb服务
  3. service mongodb stop 关闭mongodb服务
  4. ./mongo 真正使用mongodb服务

          在mongodb中,我们就要学习mongodb 的指令了,类似于学习sql一样,注意:在MongoDB中,是区分大小写的,要特别注意这一点。

         先从库级别的指令开始:


 
 
  1. > show dbs; 查看数据库,会显示当前的数据库,以及占用的内存大小。
  2. local 0. 078125GB 注意:该命令不会显示空的数据库,也就是说你新建的数据库没数据是不会显示的。
  3. > db 查看当前用户连接的是哪个库,没错就是这 2个字符。
  4. test
  5. > use xuye 创建一个库,名为xuye
  6. switched to db xuye
  7. > use xuye 注意上面我们db查看当前库是test,现在我们切换到xuye这个库了。
  8. switched to db xuye use的命令有两个作用,一个是切换库,如果这个库不存在就是创建库。
  9. > db 查看当前连接的库,此时使用的是xuye这个库。
  10. xuye
  11. > db.dropDatabase() 删除当前连接的数据库,类似对对象调用方法的操作,db表示当前的用户
  12. { "dropped" : "xuye", "ok" : 1 } dropDatabase()函数表示执行删除操作,下面的是返回值。
  13. > show users 显示当前用户,没有数据则不显示

         集合(类似MySQL的表级别)级别的指令:

         需要注意的是,在集合中,可以随意存储任何想干或者不想干的数据,并不像关系型数据库中已经定死了这个列必须是什么值,必须按照这个格式,如果现在不理解,待会看下面。


 
 
  1. > db.createCollection( "user",{size: 20,capped: true,autolndexld: true, max: 50})
  2. { "ok" : 1 }
  3. 在当前库下创建了一个集合,该集合自动添加索引,并且该集合为固定长度, 20字节,集合中最多包包含 50个文档。

          db.createCollection(name,option),其中option是一个json格式,可选参数以及信息如下。

         一些集合级别的其它操作


 
 
  1. > db.getCollectionNames()
  2. [ "system.indexes", "user" ]
  3. > show collections
  4. system.indexes
  5. user
  6. 两个指令都是获得到当前库下的所有集合名。
  7. > db.getCollection( "user") 获得到单个的集合信息
  8. xuye.user
  9. > db.printCollectionStats(); 会显示当前库下集合的创建信息,例如MySQL的查看表设置的信息。
  10. user
  11. {
  12. "ns" : "xuye.user",
  13. "count" : 0,
  14. "size" : 0,
  15. "storageSize" : 4096,
  16. "numExtents" : 1,
  17. "nindexes" : 1,
  18. "lastExtentSize" : 4096,
  19. "paddingFactor" : 1,
  20. "systemFlags" : 1,
  21. "userFlags" : 0,
  22. "totalIndexSize" : 8176,
  23. "indexSizes" : {
  24. "_id_" : 8176
  25. },
  26. "capped" : true,
  27. "max" : 50,
  28. "ok" : 1
  29. }
  30. > db.user.drop() 删除当前库下的user集合。
  31. true

          文档级别的操作指令:


 
 
  1. > db.student.insert({ "name": "xuye", "school": "hafo", "numbe": 141420014})
  2. 插入一条文档,我并没有创建集合student,直接写插入语句,如果表不存在会自动创建(一般都是使用此方式来建表,而不是去用createColletion()创建表,此方式的容量会自动增长。)
  3. > db.student.insert({ "class": 101, "number": 1401420, "teach": "xuye"})
  4. 在插入一条文档,可以发现,两条文档之间的格式要求是没有任何关系的,甚至你可以随便插入不是关于学生此类的信息都可以,但是一般一个表只插入一类信息的文档(约定成俗的)。
  5. > db.student.find() 查看该集合中所有的文档。
  6. { "_id" : ObjectId( "5b7d1aa979c725e3580ed2db") }
  7. { "_id" : ObjectId( "5b7d1b1d79c725e3580ed2dc"), "name" : "xuye", "school" : "hafo", "numbe" : 141420014 }
  8. { "_id" : ObjectId( "5b7d1c57c8ff91d6ecaf79d8"), "class" : 101, "number" : 1401420, "teach" : "xuye" }
  9. 此处还需要注意,我们并没有插入_ id这个东西,这是mongodb自动帮我们生成的一个 id,是唯一的,所以也就不需要我们去生成 id了。
  10. > db.student.find().pretty()
  11. { "_id" : ObjectId( "5b7d1aa979c725e3580ed2db") }
  12. {
  13. "_id" : ObjectId( "5b7d1b1d79c725e3580ed2dc"),
  14. "name" : "xuye",
  15. "school" : "hafo",
  16. "numbe" : 141420014
  17. } 格式化显示查询的语句,应该还有几条,我只取了其中一条粘贴在这里,就类似代码的格式化,好看一些。
  18. > db.student.insert({ "name": "xuye", "number": 140246, "deskmate":{ "name": "xiaobai", "number": 140265}})
  19. 插入一条复杂一些的文档,可以嵌套,和json一样,只要符合键值对的形式都可以使用。如果多个嵌套的话,要使用中括号,如下。
  20. db.student.insert({ "name": "xuye", "number": 140246, "deskmate":[{ "name": "xiaobai", "number": 140265},
  21. { "name": "xiaohei", "number": "142064"}]})

             分页查询:


 
 
  1. > db.books.insert({ "name": "spring", "price": 20.01})
  2. > db.books.insert({ "name": "springMvc", "price": 19.99})
  3. > db.books.insert({ "name": "java", "price": 67.84})
  4. > db.books.insert({ "name": "linux", "price": 289.78})
  5. > db.books.insert({ "name": "python", "price": 29.78})
  6. > db.books.insert({ "name": "c++", "price": 157.68})
  7. 先插入一些数据,书名和书的价格。
  8. > db.books. find()
  9. { "_id" : ObjectId( "5b7d4a588e6123a7a302be25"), "name" : "spring", "price" : 20.01 }
  10. { "_id" : ObjectId( "5b7d4a6b8e6123a7a302be27"), "name" : "springMvc", "price" : 19.99 }
  11. { "_id" : ObjectId( "5b7d4a7f8e6123a7a302be28"), "name" : "java", "price" : 67.84 }
  12. { "_id" : ObjectId( "5b7d4a898e6123a7a302be29"), "name" : "linux", "price" : 289.78 }
  13. { "_id" : ObjectId( "5b7d4a9f8e6123a7a302be2a"), "name" : "python", "price" : 29.78 }
  14. { "_id" : ObjectId( "5b7d4aad8e6123a7a302be2b"), "name" : "c++", "price" : 157.68 }
  15. 查看数据,下面进行分页。
  16. > db.books. find().skip( 0).limit( 3)
  17. { "_id" : ObjectId( "5b7d4a588e6123a7a302be25"), "name" : "spring", "price" : 20.01 }
  18. { "_id" : ObjectId( "5b7d4a6b8e6123a7a302be27"), "name" : "springMvc", "price" : 19.99 }
  19. { "_id" : ObjectId( "5b7d4a7f8e6123a7a302be28"), "name" : "java", "price" : 67.84 }
  20. 分页查询,表示查到的数据,从第一条开始,显示 3条。类似 SQL的limit 0, 3。也可以记为,跳过 0条,显示 3条。
  21. > db.books. find().limit( 3).skip( 0)
  22. { "_id" : ObjectId( "5b7d4a588e6123a7a302be25"), "name" : "spring", "price" : 20.01 }
  23. { "_id" : ObjectId( "5b7d4a6b8e6123a7a302be27"), "name" : "springMvc", "price" : 19.99 }
  24. { "_id" : ObjectId( "5b7d4a7f8e6123a7a302be28"), "name" : "java", "price" : 67.84 }
  25. limit和skip两个函数可以更换位置,结果都一样。
  26. > db.books. find().limit( 3) 等同于db.books. find().skip( 0).limit( 3)
  27. > db.books. find().skip( 5) 跳过前 5条,显示第六条以后的所有文档。

             带条件的查询:(and、or、gt、lt、正则、去重、排序等查询)


 
 
  1. > db.books.find({ "name": "spring"})
  2. { "_id" : ObjectId( "5b7d4a588e6123a7a302be25"), "name" : "spring", "price" : 20.01 }
  3. 查询name为spring的书籍,在find函数的里面加上json格式的条件即可。
  4. > db.books.find({ "name": "spring", "price": 20.01})
  5. { "_id" : ObjectId( "5b7d4a588e6123a7a302be25"), "name" : "spring", "price" : 20.01 }
  6. 多条件查询,使用逗号分隔即可,类似 select * form books where name= "spring" and price= 20.01
  7. > db.books.find({$or:[{ "name": "spring"},{ "name": "java"}]})
  8. { "_id" : ObjectId( "5b7d4a588e6123a7a302be25"), "name" : "spring", "price" : 20.01 }
  9. { "_id" : ObjectId( "5b7d4a7f8e6123a7a302be28"), "name" : "java", "price" : 67.84 }
  10. 查询nam为spring或者java的书籍,格式为find({$or : [{},{}]}) 中括号里面填写每一个json格式的条件,类似 select * form books where name= "spring" or name= "java"
  11. > db.books.find({ "price":{$gte : 100}})
  12. { "_id" : ObjectId( "5b7d4a898e6123a7a302be29"), "name" : "linux", "price" : 289.78 }
  13. { "_id" : ObjectId( "5b7d4aad8e6123a7a302be2b"), "name" : "c++", "price" : 157.68 }
  14. 查询价格大于等于 100的书籍。
  15. > db.books.find({ "price":{$lte : 20}})
  16. { "_id" : ObjectId( "5b7d4a6b8e6123a7a302be27"), "name" : "springMvc", "price" : 19.99 }
  17. 查询价格小于等于 20的书籍。
  18. > db.books.find({ "price":{$gte: 100,$lte: 200}})
  19. { "_id" : ObjectId( "5b7d4aad8e6123a7a302be2b"), "name" : "c++", "price" : 157.68 }
  20. 查询价格大于等于 100并且小于等于 200的书籍。
  21. > db.books.find({$or:[{ "price":{$gt: 200}},{ "price":{$lt: 20}}]})
  22. { "_id" : ObjectId( "5b7d4a6b8e6123a7a302be27"), "name" : "springMvc", "price" : 19.99 }
  23. { "_id" : ObjectId( "5b7d4a898e6123a7a302be29"), "name" : "linux", "price" : 289.78 }
  24. 查询价格大于 200或者价格小于 20的书籍, or的操作有点另类,一定要写在一开始的key上。
  25. 注意: gt为大于,gte为大于等于, lt为小于,lte为小于等于,e为equals。

                因为带提交查询的指令内容太多,上面这一部分主要是大于和小于,and和or的指令。下面是去重,正则的条件查询。


 
 
  1. > db.books.insert({ "name": "spring", "price": 22.05})
  2. 先插入一条name同为spring的文档
  3. > db.books.distinct( "name")
  4. [ "spring", "springMvc", "java", "linux", "python", "c++" ]
  5. 根据name去重,类似 select distinct name from books
  6. > db.books.distinct( "name",{ "price":{$lt: 30}})
  7. [ "spring", "springMvc", "python" ]
  8. 查询出价格小于 30的书籍,然后根据name去重,类似 select distinct name from books where price< 30
  9. distinct命令和find类似,只不过多了一个去重功能。
  10. > db.books.find({ "name":/spring/})
  11. { "_id" : ObjectId( "5b7d4a588e6123a7a302be25"), "name" : "spring", "price" : 20.01 }
  12. { "_id" : ObjectId( "5b7d4a6b8e6123a7a302be27"), "name" : "springMvc", "price" : 19.99 }
  13. { "_id" : ObjectId( "5b7d58deabdfa1fbe797fa0b"), "name" : "spring", "price" : 22.05 }
  14. 使用正则表达式查询,/等同于%,类似 select * from books where name like %spring%
  15. > db.books.find({ "name":/spring/}).skip( 1).limit( 2)
  16. { "_id" : ObjectId( "5b7d4a6b8e6123a7a302be27"), "name" : "springMvc", "price" : 19.99 }
  17. { "_id" : ObjectId( "5b7d58deabdfa1fbe797fa0b"), "name" : "spring", "price" : 22.05 }
  18. 搜索后的分页查询,在搜索栏输入内容后的查询功能,类似 select * from books where name like %spring% limit 1, 2。正则表达式内容较多,具体可自己网上查询。
  19. > db.books.find({},{ "price": 1})
  20. { "_id" : ObjectId( "5b7d4a588e6123a7a302be25"), "price" : 20.01 }
  21. { "_id" : ObjectId( "5b7d4a6b8e6123a7a302be27"), "price" : 19.99 }
  22. { "_id" : ObjectId( "5b7d4a7f8e6123a7a302be28"), "price" : 67.84 }
  23. { "_id" : ObjectId( "5b7d4a898e6123a7a302be29"), "price" : 289.78 }
  24. { "_id" : ObjectId( "5b7d4a9f8e6123a7a302be2a"), "price" : 29.78 }
  25. { "_id" : ObjectId( "5b7d4aad8e6123a7a302be2b"), "price" : 157.68 }
  26. { "_id" : ObjectId( "5b7d58deabdfa1fbe797fa0b"), "price" : 22.05 }
  27. 只显示price列,格式find({},{key1: 1,key2: 1}),第一个{}表示查询的条件,第二个为显示的列信息,多个用逗号隔开,类似 select price from books,但是_id它是必须显示的。
  28. > db.books.find({},{ "price": 1}).sort({ "price": 1})
  29. { "_id" : ObjectId( "5b7d4a6b8e6123a7a302be27"), "price" : 19.99 }
  30. { "_id" : ObjectId( "5b7d4a588e6123a7a302be25"), "price" : 20.01 }
  31. { "_id" : ObjectId( "5b7d58deabdfa1fbe797fa0b"), "price" : 22.05 }
  32. { "_id" : ObjectId( "5b7d4a9f8e6123a7a302be2a"), "price" : 29.78 }
  33. { "_id" : ObjectId( "5b7d4a7f8e6123a7a302be28"), "price" : 67.84 }
  34. { "_id" : ObjectId( "5b7d4aad8e6123a7a302be2b"), "price" : 157.68 }
  35. { "_id" : ObjectId( "5b7d4a898e6123a7a302be29"), "price" : 289.78 }
  36. 查询后只显示price列,并且按照价格升序,类似 select price from books order by price
  37. > db.books.find({},{ "price": 1}).sort({ "price": -1})
  38. 与上面相反,按价格降序。
  39. > db.books.find().count()
  40. 7
  41. 返回查询结果的总记录数。
  42. > db.books.findOne()
  43. 查询第一条记录

          修改和删除指令:


 
 
  1. > db.books.update({ "name": "java"},{$ set:{ "name": "javase", "price": 50.01}})
  2. 把name为java的这条数据更新为name=javase和price= 50.01。格式update({},{}),第一个{}为要更改的数据,第二个为要修改为什么数据。类似update books set name= "javase" and price= 50.01 where name= "java"
  3. 注意,如果存在多个name为java的则只修改一个,下面看如何更新多个。之前我们存在名为spring的书籍有两本。现在把这两本价格都修改为 50.
  4. > db.books.update({ "name": "spring"},{$ set : { "price": 50}},{multi: true})
  5. > db.books. find()
  6. { "_id" : ObjectId( "5b7d4a588e6123a7a302be25"), "name" : "spring", "price" : 50 }
  7. { "_id" : ObjectId( "5b7d58deabdfa1fbe797fa0b"), "name" : "spring", "price" : 50 }
  8. 结果为name为spring的都修改了,{multi: true}表示是否修改多行,默认是为 false
  9. > db.books.remove({ "name": "spring"})
  10. > db.books. find()
  11. { "_id" : ObjectId( "5b7d4a6b8e6123a7a302be27"), "name" : "springMvc", "price" : 19.99 }
  12. { "_id" : ObjectId( "5b7d4a898e6123a7a302be29"), "name" : "linux", "price" : 289.78 }
  13. { "_id" : ObjectId( "5b7d4a9f8e6123a7a302be2a"), "name" : "python", "price" : 29.78 }
  14. { "_id" : ObjectId( "5b7d4aad8e6123a7a302be2b"), "name" : "c++", "price" : 157.68 }
  15. 删除name为spring的书籍,默认是存在多少个就删除多少个,与update相反。
  16. > db.books.remove({}) 删除当前集合的所有数据,不要乱用。

          索引的操作指令:


 
 
  1. > db.books.ensureIndex({ "name": 1, "price":- 1})
  2. 创建组合索引,mongodb会自动为索引创建名字,如果{}里面为一个,则为单个索引,多个就为组合索引,其中 1和- 1表示索引的方向。
  3. > db.books.ensureIndex({ "price": 1},{ "name": "index_price"})
  4. 创建一个索引,并指定索引的名字。
  5. > db.books.ensureIndex({ "name": 1},{ "unique": true})
  6. 为name创建一个唯一索引
  7. > db.books.getIndexes()
  8. 查看当前集合中的索引信息。
  9. > db.system.indexes. find()
  10. { "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.student", "name" : "_id_" }
  11. { "v" : 1, "key" : { "_id" : 1 }, "ns" : "test.books", "name" : "_id_" }
  12. { "v" : 1, "key" : { "name" : 1, "price" : 1 }, "ns" : "test.books", "name" : "name_1_price_1" }
  13. { "v" : 1, "key" : { "price" : 1 }, "ns" : "test.books", "name" : "index_price" }
  14. { "v" : 1, "key" : { "name" : 1 }, "unique" : true, "ns" : "test.books", "name" : "name_1" }
  15. 索引的信息存在每个数据库的system.indexes集合里面,这里是查看所有的索引信息,需要与上面区别开。
  16. > db.books.dropIndex( "name_1")
  17. { "nIndexesWas" : 4, "ok" : 1 }
  18. 删除在books集合中索引名为name_1的索引
  19. > db.books.dropIndexes()
  20. {
  21. "nIndexesWas" : 3,
  22. "msg" : "non-_id indexes dropped for collection",
  23. "ok" : 1
  24. }
  25. 删除在books集合中的所有索引。

           基础部分到此结束,下面部分的内容是关于mongodb的权限和用户的操作指令,以及语句块和在java中的使用。

           

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值