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。
总结一波:
-
pe -ef | grep mongodb 查看mongodb的目录和看是否启动
-
service mongodb
start 启动mongodb服务
-
service mongodb
stop 关闭mongodb服务
-
./mongo 真正使用mongodb服务
在mongodb中,我们就要学习mongodb 的指令了,类似于学习sql一样,注意:在MongoDB中,是区分大小写的,要特别注意这一点。
先从库级别的指令开始:
-
> show dbs; 查看数据库,会显示当前的数据库,以及占用的内存大小。
-
local
0.
078125GB 注意:该命令不会显示空的数据库,也就是说你新建的数据库没数据是不会显示的。
-
-
> db 查看当前用户连接的是哪个库,没错就是这
2个字符。
-
test
-
-
>
use xuye 创建一个库,名为xuye
-
switched to db xuye
-
-
>
use xuye 注意上面我们db查看当前库是test,现在我们切换到xuye这个库了。
-
switched to db xuye
use的命令有两个作用,一个是切换库,如果这个库不存在就是创建库。
-
-
> db 查看当前连接的库,此时使用的是xuye这个库。
-
xuye
-
-
> db.dropDatabase() 删除当前连接的数据库,类似对对象调用方法的操作,db表示当前的用户
-
{
"dropped" :
"xuye",
"ok" :
1 } dropDatabase()函数表示执行删除操作,下面的是返回值。
-
-
> show users 显示当前用户,没有数据则不显示
集合(类似MySQL的表级别)级别的指令:
需要注意的是,在集合中,可以随意存储任何想干或者不想干的数据,并不像关系型数据库中已经定死了这个列必须是什么值,必须按照这个格式,如果现在不理解,待会看下面。
-
> db.createCollection(
"user",{size:
20,capped:
true,autolndexld:
true,
max:
50})
-
-
{
"ok" :
1 }
-
在当前库下创建了一个集合,该集合自动添加索引,并且该集合为固定长度,
20字节,集合中最多包包含
50个文档。
db.createCollection(name,option),其中option是一个json格式,可选参数以及信息如下。
一些集合级别的其它操作
-
> db.getCollectionNames()
-
[
"system.indexes", "user" ]
-
-
> show collections
-
system.indexes
-
user
-
-
两个指令都是获得到当前库下的所有集合名。
-
-
> db.getCollection(
"user") 获得到单个的集合信息
-
xuye.user
-
-
> db.printCollectionStats(); 会显示当前库下集合的创建信息,例如MySQL的查看表设置的信息。
-
user
-
{
-
"ns" :
"xuye.user",
-
"count" :
0,
-
"size" :
0,
-
"storageSize" :
4096,
-
"numExtents" :
1,
-
"nindexes" :
1,
-
"lastExtentSize" :
4096,
-
"paddingFactor" :
1,
-
"systemFlags" :
1,
-
"userFlags" :
0,
-
"totalIndexSize" :
8176,
-
"indexSizes" : {
-
"_id_" :
8176
-
},
-
"capped" :
true,
-
"max" :
50,
-
"ok" :
1
-
}
-
-
> db.user.drop() 删除当前库下的user集合。
-
true
-
文档级别的操作指令:
-
> db.student.insert({
"name":
"xuye",
"school":
"hafo",
"numbe":
141420014})
-
插入一条文档,我并没有创建集合student,直接写插入语句,如果表不存在会自动创建(一般都是使用此方式来建表,而不是去用createColletion()创建表,此方式的容量会自动增长。)
-
-
> db.student.insert({
"class":
101,
"number":
1401420,
"teach":
"xuye"})
-
在插入一条文档,可以发现,两条文档之间的格式要求是没有任何关系的,甚至你可以随便插入不是关于学生此类的信息都可以,但是一般一个表只插入一类信息的文档(约定成俗的)。
-
-
-
> db.student.find() 查看该集合中所有的文档。
-
{
"_id" : ObjectId(
"5b7d1aa979c725e3580ed2db") }
-
{
"_id" : ObjectId(
"5b7d1b1d79c725e3580ed2dc"),
"name" :
"xuye",
"school" :
"hafo",
"numbe" :
141420014 }
-
{
"_id" : ObjectId(
"5b7d1c57c8ff91d6ecaf79d8"),
"class" :
101,
"number" :
1401420,
"teach" :
"xuye" }
-
此处还需要注意,我们并没有插入_
id这个东西,这是mongodb自动帮我们生成的一个
id,是唯一的,所以也就不需要我们去生成
id了。
-
-
> db.student.find().pretty()
-
{
"_id" : ObjectId(
"5b7d1aa979c725e3580ed2db") }
-
{
-
"_id" : ObjectId(
"5b7d1b1d79c725e3580ed2dc"),
-
"name" :
"xuye",
-
"school" :
"hafo",
-
"numbe" :
141420014
-
} 格式化显示查询的语句,应该还有几条,我只取了其中一条粘贴在这里,就类似代码的格式化,好看一些。
-
-
> db.student.insert({
"name":
"xuye",
"number":
140246,
"deskmate":{
"name":
"xiaobai",
"number":
140265}})
-
插入一条复杂一些的文档,可以嵌套,和json一样,只要符合键值对的形式都可以使用。如果多个嵌套的话,要使用中括号,如下。
-
db.student.insert({
"name":
"xuye",
"number":
140246,
"deskmate":[{
"name":
"xiaobai",
"number":
140265},
-
{
"name":
"xiaohei",
"number":
"142064"}]})
-
-
-
分页查询:
-
> db.books.insert({
"name":
"spring",
"price":
20.01})
-
> db.books.insert({
"name":
"springMvc",
"price":
19.99})
-
> db.books.insert({
"name":
"java",
"price":
67.84})
-
> db.books.insert({
"name":
"linux",
"price":
289.78})
-
> db.books.insert({
"name":
"python",
"price":
29.78})
-
> db.books.insert({
"name":
"c++",
"price":
157.68})
-
先插入一些数据,书名和书的价格。
-
-
> db.books.
find()
-
{
"_id" :
ObjectId(
"5b7d4a588e6123a7a302be25"),
"name" :
"spring",
"price" :
20.01 }
-
{
"_id" :
ObjectId(
"5b7d4a6b8e6123a7a302be27"),
"name" :
"springMvc",
"price" :
19.99 }
-
{
"_id" :
ObjectId(
"5b7d4a7f8e6123a7a302be28"),
"name" :
"java",
"price" :
67.84 }
-
{
"_id" :
ObjectId(
"5b7d4a898e6123a7a302be29"),
"name" :
"linux",
"price" :
289.78 }
-
{
"_id" :
ObjectId(
"5b7d4a9f8e6123a7a302be2a"),
"name" :
"python",
"price" :
29.78 }
-
{
"_id" :
ObjectId(
"5b7d4aad8e6123a7a302be2b"),
"name" :
"c++",
"price" :
157.68 }
-
查看数据,下面进行分页。
-
-
> db.books.
find().skip(
0).limit(
3)
-
{
"_id" :
ObjectId(
"5b7d4a588e6123a7a302be25"),
"name" :
"spring",
"price" :
20.01 }
-
{
"_id" :
ObjectId(
"5b7d4a6b8e6123a7a302be27"),
"name" :
"springMvc",
"price" :
19.99 }
-
{
"_id" :
ObjectId(
"5b7d4a7f8e6123a7a302be28"),
"name" :
"java",
"price" :
67.84 }
-
分页查询,表示查到的数据,从第一条开始,显示
3条。类似
SQL的limit
0,
3。也可以记为,跳过
0条,显示
3条。
-
-
> db.books.
find().limit(
3).skip(
0)
-
{
"_id" :
ObjectId(
"5b7d4a588e6123a7a302be25"),
"name" :
"spring",
"price" :
20.01 }
-
{
"_id" :
ObjectId(
"5b7d4a6b8e6123a7a302be27"),
"name" :
"springMvc",
"price" :
19.99 }
-
{
"_id" :
ObjectId(
"5b7d4a7f8e6123a7a302be28"),
"name" :
"java",
"price" :
67.84 }
-
limit和skip两个函数可以更换位置,结果都一样。
-
-
> db.books.
find().limit(
3) 等同于db.books.
find().skip(
0).limit(
3)
-
-
> db.books.
find().skip(
5) 跳过前
5条,显示第六条以后的所有文档。
带条件的查询:(and、or、gt、lt、正则、去重、排序等查询)
-
> db.books.find({
"name":
"spring"})
-
{
"_id" : ObjectId(
"5b7d4a588e6123a7a302be25"),
"name" :
"spring",
"price" :
20.01 }
-
查询name为spring的书籍,在find函数的里面加上json格式的条件即可。
-
-
> db.books.find({
"name":
"spring",
"price":
20.01})
-
{
"_id" : ObjectId(
"5b7d4a588e6123a7a302be25"),
"name" :
"spring",
"price" :
20.01 }
-
多条件查询,使用逗号分隔即可,类似
select * form books where name=
"spring"
and price=
20.01
-
-
> db.books.find({$or:[{
"name":
"spring"},{
"name":
"java"}]})
-
{
"_id" : ObjectId(
"5b7d4a588e6123a7a302be25"),
"name" :
"spring",
"price" :
20.01 }
-
{
"_id" : ObjectId(
"5b7d4a7f8e6123a7a302be28"),
"name" :
"java",
"price" :
67.84 }
-
查询nam为spring或者java的书籍,格式为find({$or : [{},{}]}) 中括号里面填写每一个json格式的条件,类似
select * form books where name=
"spring"
or name=
"java"
-
-
> db.books.find({
"price":{$gte :
100}})
-
{
"_id" : ObjectId(
"5b7d4a898e6123a7a302be29"),
"name" :
"linux",
"price" :
289.78 }
-
{
"_id" : ObjectId(
"5b7d4aad8e6123a7a302be2b"),
"name" :
"c++",
"price" :
157.68 }
-
查询价格大于等于
100的书籍。
-
-
> db.books.find({
"price":{$lte :
20}})
-
{
"_id" : ObjectId(
"5b7d4a6b8e6123a7a302be27"),
"name" :
"springMvc",
"price" :
19.99 }
-
查询价格小于等于
20的书籍。
-
-
> db.books.find({
"price":{$gte:
100,$lte:
200}})
-
{
"_id" : ObjectId(
"5b7d4aad8e6123a7a302be2b"),
"name" :
"c++",
"price" :
157.68 }
-
查询价格大于等于
100并且小于等于
200的书籍。
-
-
> db.books.find({$or:[{
"price":{$gt:
200}},{
"price":{$lt:
20}}]})
-
{
"_id" : ObjectId(
"5b7d4a6b8e6123a7a302be27"),
"name" :
"springMvc",
"price" :
19.99 }
-
{
"_id" : ObjectId(
"5b7d4a898e6123a7a302be29"),
"name" :
"linux",
"price" :
289.78 }
-
查询价格大于
200或者价格小于
20的书籍,
or的操作有点另类,一定要写在一开始的key上。
-
-
注意:
gt为大于,gte为大于等于,
lt为小于,lte为小于等于,e为equals。
因为带提交查询的指令内容太多,上面这一部分主要是大于和小于,and和or的指令。下面是去重,正则的条件查询。
-
> db.books.insert({
"name":
"spring",
"price":
22.05})
-
先插入一条name同为spring的文档
-
-
> db.books.distinct(
"name")
-
[
"spring", "springMvc", "java", "linux", "python", "c++" ]
-
根据name去重,类似
select distinct name
from books
-
-
> db.books.distinct(
"name",{
"price":{$lt:
30}})
-
[
"spring", "springMvc", "python" ]
-
查询出价格小于
30的书籍,然后根据name去重,类似
select distinct name
from books
where price<
30
-
distinct命令和find类似,只不过多了一个去重功能。
-
-
> db.books.find({
"name":/spring/})
-
{
"_id" : ObjectId(
"5b7d4a588e6123a7a302be25"),
"name" :
"spring",
"price" :
20.01 }
-
{
"_id" : ObjectId(
"5b7d4a6b8e6123a7a302be27"),
"name" :
"springMvc",
"price" :
19.99 }
-
{
"_id" : ObjectId(
"5b7d58deabdfa1fbe797fa0b"),
"name" :
"spring",
"price" :
22.05 }
-
使用正则表达式查询,/等同于%,类似
select *
from books
where name like %spring%
-
-
> db.books.find({
"name":/spring/}).skip(
1).limit(
2)
-
{
"_id" : ObjectId(
"5b7d4a6b8e6123a7a302be27"),
"name" :
"springMvc",
"price" :
19.99 }
-
{
"_id" : ObjectId(
"5b7d58deabdfa1fbe797fa0b"),
"name" :
"spring",
"price" :
22.05 }
-
搜索后的分页查询,在搜索栏输入内容后的查询功能,类似
select *
from books
where name like %spring% limit
1,
2。正则表达式内容较多,具体可自己网上查询。
-
-
> db.books.find({},{
"price":
1})
-
{
"_id" : ObjectId(
"5b7d4a588e6123a7a302be25"),
"price" :
20.01 }
-
{
"_id" : ObjectId(
"5b7d4a6b8e6123a7a302be27"),
"price" :
19.99 }
-
{
"_id" : ObjectId(
"5b7d4a7f8e6123a7a302be28"),
"price" :
67.84 }
-
{
"_id" : ObjectId(
"5b7d4a898e6123a7a302be29"),
"price" :
289.78 }
-
{
"_id" : ObjectId(
"5b7d4a9f8e6123a7a302be2a"),
"price" :
29.78 }
-
{
"_id" : ObjectId(
"5b7d4aad8e6123a7a302be2b"),
"price" :
157.68 }
-
{
"_id" : ObjectId(
"5b7d58deabdfa1fbe797fa0b"),
"price" :
22.05 }
-
只显示price列,格式find({},{key1:
1,key2:
1}),第一个{}表示查询的条件,第二个为显示的列信息,多个用逗号隔开,类似
select price
from books,但是_id它是必须显示的。
-
-
> db.books.find({},{
"price":
1}).sort({
"price":
1})
-
{
"_id" : ObjectId(
"5b7d4a6b8e6123a7a302be27"),
"price" :
19.99 }
-
{
"_id" : ObjectId(
"5b7d4a588e6123a7a302be25"),
"price" :
20.01 }
-
{
"_id" : ObjectId(
"5b7d58deabdfa1fbe797fa0b"),
"price" :
22.05 }
-
{
"_id" : ObjectId(
"5b7d4a9f8e6123a7a302be2a"),
"price" :
29.78 }
-
{
"_id" : ObjectId(
"5b7d4a7f8e6123a7a302be28"),
"price" :
67.84 }
-
{
"_id" : ObjectId(
"5b7d4aad8e6123a7a302be2b"),
"price" :
157.68 }
-
{
"_id" : ObjectId(
"5b7d4a898e6123a7a302be29"),
"price" :
289.78 }
-
查询后只显示price列,并且按照价格升序,类似
select price
from books order
by price
-
-
> db.books.find({},{
"price":
1}).sort({
"price":
-1})
-
与上面相反,按价格降序。
-
-
> db.books.find().count()
-
7
-
返回查询结果的总记录数。
-
-
> db.books.findOne()
-
查询第一条记录
修改和删除指令:
-
> db.books.update({
"name":
"java"},{$
set:{
"name":
"javase",
"price":
50.01}})
-
把name为java的这条数据更新为name=javase和price=
50.01。格式update({},{}),第一个{}为要更改的数据,第二个为要修改为什么数据。类似update books
set name=
"javase" and price=
50.01
where name=
"java"
-
注意,如果存在多个name为java的则只修改一个,下面看如何更新多个。之前我们存在名为spring的书籍有两本。现在把这两本价格都修改为
50.
-
-
> db.books.update({
"name":
"spring"},{$
set : {
"price":
50}},{multi:
true})
-
> db.books.
find()
-
{
"_id" :
ObjectId(
"5b7d4a588e6123a7a302be25"),
"name" :
"spring",
"price" :
50 }
-
{
"_id" :
ObjectId(
"5b7d58deabdfa1fbe797fa0b"),
"name" :
"spring",
"price" :
50 }
-
结果为name为spring的都修改了,{multi:
true}表示是否修改多行,默认是为
false。
-
-
> db.books.remove({
"name":
"spring"})
-
> db.books.
find()
-
{
"_id" :
ObjectId(
"5b7d4a6b8e6123a7a302be27"),
"name" :
"springMvc",
"price" :
19.99 }
-
{
"_id" :
ObjectId(
"5b7d4a898e6123a7a302be29"),
"name" :
"linux",
"price" :
289.78 }
-
{
"_id" :
ObjectId(
"5b7d4a9f8e6123a7a302be2a"),
"name" :
"python",
"price" :
29.78 }
-
{
"_id" :
ObjectId(
"5b7d4aad8e6123a7a302be2b"),
"name" :
"c++",
"price" :
157.68 }
-
删除name为spring的书籍,默认是存在多少个就删除多少个,与update相反。
-
-
> db.books.remove({}) 删除当前集合的所有数据,不要乱用。
索引的操作指令:
-
> db.books.ensureIndex({
"name":
1,
"price":-
1})
-
创建组合索引,mongodb会自动为索引创建名字,如果{}里面为一个,则为单个索引,多个就为组合索引,其中
1和-
1表示索引的方向。
-
-
> db.books.ensureIndex({
"price":
1},{
"name":
"index_price"})
-
创建一个索引,并指定索引的名字。
-
-
> db.books.ensureIndex({
"name":
1},{
"unique":
true})
-
为name创建一个唯一索引
-
-
> db.books.getIndexes()
-
查看当前集合中的索引信息。
-
-
-
> db.system.indexes.
find()
-
{
"v" :
1,
"key" : {
"_id" :
1 },
"ns" :
"test.student",
"name" :
"_id_" }
-
{
"v" :
1,
"key" : {
"_id" :
1 },
"ns" :
"test.books",
"name" :
"_id_" }
-
{
"v" :
1,
"key" : {
"name" :
1,
"price" :
1 },
"ns" :
"test.books",
"name" :
"name_1_price_1" }
-
{
"v" :
1,
"key" : {
"price" :
1 },
"ns" :
"test.books",
"name" :
"index_price" }
-
{
"v" :
1,
"key" : {
"name" :
1 },
"unique" :
true,
"ns" :
"test.books",
"name" :
"name_1" }
-
索引的信息存在每个数据库的system.indexes集合里面,这里是查看所有的索引信息,需要与上面区别开。
-
-
> db.books.dropIndex(
"name_1")
-
{
"nIndexesWas" :
4,
"ok" :
1 }
-
删除在books集合中索引名为name_1的索引
-
-
> db.books.dropIndexes()
-
{
-
"nIndexesWas" :
3,
-
"msg" :
"non-_id indexes dropped for collection",
-
"ok" :
1
-
}
-
删除在books集合中的所有索引。
-
基础部分到此结束,下面部分的内容是关于mongodb的权限和用户的操作指令,以及语句块和在java中的使用。