MongoDB 环境配置和基本操作-----专属学习笔记

mongod.conf配置文件内容如下:

#MongoDB服务器启动的端口
port = 27017
#数据存储目录路径(可以是绝对路径也可以是相对路径)
dbpath = E:\mongodb\data
#MongoDB日志存储路径
#logpath = E:\mongodb\log\mongod.log
#日志详细程度配置v(1-5个等级)
#verbose = vvvvv 
#用户名密码权限验证功能是否开启(true 是 false 否)
#auth = true

本机操作(方便的自己指令使用)

mongod -f "E:\mongodb\conf\mongod.conf"
E:\mongodb-win32-x86_64-3.2.2\bin
mongo 127.0.0.1:27017/test
------------------------------------------------------------------
https://downloads.mongodb.org/win32/mongodb-win32-x86_64-3.2.2.zip
```
步骤一:(启动MongoDB服务器)1.在cmd中进入安装目录:F:\mongodb-win32-i386-2.6.6\bin;
       2.执行开启mongodb服务命令:⑴f: ⑵cd  f:\mongodb-win32-i386-2.6.6\bin 
       ⑶mongod -dbpath "F:\mongodb\data"
步骤二:(启动客户端操作MongoDB数据库)1.开启另外一个cmd,【开始】、搜索框输入cmd、回车,开启新        的cmd窗口。输入命令进行测试:⑴f: ⑵cd f:\mongodb-win32-i386-2.6.6\bin ⑶mongo
       2.开始操作MongoDB数据库。


小结:在安装完成MongoDB时启动服务器可以1.在F盘下新建mongodb文件夹进入到该文件夹分别新建conf,data,log文件夹 2.在conf文件夹中新建mongod.conf配置文件,内容为(1)port = 27017 #MongoDB服务器启动的端口 (2)dbpath = E:\mongodb\data #数据存储目录路径(可以是绝对路径也可以是相对路径) (3)logpath = E:\mongodb\log\mongod.log #MongoDB日志存储路径 3.通过cmd DOS命令进入到 f:\mongodb-win32-i386-2.6.6\bin 文件夹下通过DOS命令启动mongodb服务器,指令为:mongod -f "F:\mongodb\conf\mongod.conf" 这样mongodb服务器就启动好了。


启动对应的mongodb服务器端的数据库对应的客户端
步骤一: cmd指定进入到 f:\mongodb-win32-i386-2.6.6\bin 文件夹下 使用DOS指令 mongo 127.0.0.1(mongodb服务器ip地址):27017(服务器端口)/test(客户端连接的数据库) 


关闭mongodb服务器指令
步骤一:连接在127.0.0.1(mongodb服务器ip地址):27017(服务器端口)/test(客户端连接的数据库) 客户端下的cmd窗口下输入 db.shutdownServer() 指令回车即可关闭服务器端mongodb服务器




-----------------------------------------------------------------------------------------------------------
mongodb数据库简单操作指令实战
show dbs #查看服务器中所有的数据库
show tables #查看当下数据库中所有的数据表
use imooc #使用数据库
db.dropDatabase() #删除数据库


#创建数据库
use imooc #新建库并使用
db.imooc_collection.insert({x:1}) #新建imooc_collection表并往表中插入x=1这条数据
show dbs #查询mongodb服务器中所有的数据库


#查看创建的数据库表
show collections


#查询数据库表中的数据
db.imooc_collection.find()


#向表中插入指定_id的数据
db.imooc_collection.insert({x:3,_id:1})


#从表中查询出满足条件的数据(where x = 1)
db.imooc_collection.find({x:1})


#用js语法循环向表中插入数据
for(i=3;i<10;i++)db.imooc_collection.insert({x:i})


#对数据库表中的数据进行统计
db.imooc_collection.find().count()


#过滤前三条数据(skip()) 并 只查出满足条件的前两条数据(limit()) 并 进行排序(sort())
db.imooc_collection.find().skip(3).limit(2).sort({x:1})


#表数据更新操作
db.imooc_collection.update({x:1},{x:999})  #表中x=1的数据更新为x=999
db.imooc_collection.find({x:999})


#表中数据存储多个字段执行更新操作
db.imooc_collection.insert({x:100,y:100,z:100})
db.imooc_collection.update({z:100},{$set:{y:99}}) #表中z=100的那条数据更新为y=99
db.imooc_collection.find({z:100})
更新后结果
{ "_id" : ObjectId("5a8f6c693989bbf2c7b24bce"), "x" : 100, "y" : 99, "z" : 100 }


#更新表中的某条数据,当这条数据不存在则自动往该表中插入这条数据
db.imooc_collection.update({y:100},{y:999},true)
db.imooc_collection.find({y:999})


#同时更新表中满足更新条件的多条数据
db.imooc_collection.insert({c:1}) #插入三条c=1的数据
db.imooc_collection.update({c:1},{$set:{c:2}},false,true)
db.imooc_collection.find({c:1}) #不存在
db.imooc_collection.find({c:2}) #存在3条数据


#删除表中数据(注:删除操作不允许不传参数)
db.imooc_collection.remove({c:2}) #删除满足条件c=2 的所有数据


#对于某张表进行删除操作
db.imooc_collection.drop()
show tables


#创建索引(数据量较大不使用索引数据查询速度较慢)操作
1.创建_id索引
use imooc
db.imooc_collection.insert({x:1})


2.创建单键索引
db.imooc_collection.insert({x:1})
db.imooc_collection.getIndexes() #查询数据表中的索引
db.imooc_collection.ensureIndex({x:1}) #创建索引 #x=1 为正向排序 ,x=-1 为逆向排序


3.创建多键(数组)索引
db.imooc_2.insert({x:[1,2,3,4,5]})


4.复合索引(当我们的查询条件不只有一个时,就需要建立复合索引)
没建索引前
db.imooc_2.find({x:11,y:22})
结果:没有数据
建立索引后
db.imooc_2.ensureIndex({x:1,y:1})
db.imooc_2.find({x:11,y:22})
结果:有相对应的数据集被查询出来结果显示为:
{ "_id" : ObjectId("5a9d4143354942906d06ef4b"), "x" : 11, "y" : 22, "z" : 33 }


5.过期索引(是在一段时间后会过期的索引,在索引过期后,相应的数据会被删除)
建立索引
db.imooc_2.ensureIndex({time:1},{expireAfterSeconds:30})
db.imooc_2.insert({time:new Date()})
db.imooc_2.find()
结果
{ "_id" : ObjectId("5a9de0ef0fc5665410b5a276"), "time" : ISODate("2018-03-06T00:29:35.724Z") } 该条数据30s后被删除


6.全文索引(对字符串与字符串数组创建全文可搜索的索引)
创建全文索引
db.articles.ensureIndex({key:"text"})
db.articles.ensureIndex({key_1:"text",key_2:"text"})
db.articles.ensureIndex({"$**":"text"})db
db.imooc_2.ensureIndex({"article":"text"}) #创建全文索引
db.imooc_2.insert({"article":"aa bb cc dd ee"})  #插入数据
db.imooc_2.insert({"article":"aa bb rr gg"})
db.imooc_2.insert({"article":"aa bb cc hh gg"})
查询条件
db.imooc_2.find({$text:{$search:"aa"}})
结果
{ "_id" : ObjectId("5a9e07420fc5665410b5a27b"), "article" : "aa bb rr gg" }
{ "_id" : ObjectId("5a9e07500fc5665410b5a27c"), "article" : "aa bb cc hh gg" }
{ "_id" : ObjectId("5a9e07370fc5665410b5a27a"), "article" : "aa bb cc dd ee" }
查询条件
db.imooc_2.find({$text:{$search:"rr"}})
结果
{ "_id" : ObjectId("5a9e07420fc5665410b5a27b"), "article" : "aa bb rr gg" }
条件:查询包含aa bb 不包含cc 的数据
db.imooc_2.find({$text:{$search:"aa bb -cc"}})
结果
{ "_id" : ObjectId("5a9e07420fc5665410b5a27b"), "article" : "aa bb rr gg" }
条件: 查询既包含aa又包含bb又包含cc的文档(与查询操作)
db.imooc_2.find({$text:{$search:"\"aa\" \"bb\" \"cc\""}})
结果
{ "_id" : ObjectId("5a9e07500fc5665410b5a27c"), "article" : "aa bb cc hh gg" }
{ "_id" : ObjectId("5a9e07370fc5665410b5a27a"), "article" : "aa bb cc dd ee" }


7.全文索引相似度查询
条件:查询包含aa 和 bb 的数据和每条数据与查询条件的相似度
db.imooc_2.find({$text:{$search:"aa bb"}},{score:{$meta:"textScore"}})
结果
{ "_id" : ObjectId("5a9e15520fc5665410b5a27d"), "article" : "aa bb", "score" : 1.5 }
{ "_id" : ObjectId("5a9e07420fc5665410b5a27b"), "article" : "aa bb rr gg", "score" : 1.25 }
{ "_id" : ObjectId("5a9e07500fc5665410b5a27c"), "article" : "aa bb cc hh gg", "score" : 1.2 }
{ "_id" : ObjectId("5a9e07370fc5665410b5a27a"), "article" : "aa bb cc dd ee", "score" : 1.2 }
条件:查询包含aa 和 bb 的数据和每条数据与查询条件的相似度并且根据相似度的的大小由高到低排序
db.imooc_2.find({$text:{$search:"aa bb"}},{score:{$meta:"textScore"}}).sort({score:{$meta:"textScore"}})


全文索引使用限制
(1)每次查询,只能指定一个$text查询
(2)$text查询不能出现在$nor查询中
(3)查询中如果包含了$text,hint不再起作用
(4)MongoDB全文索引还不支持中文


9.地理位置索引
分类
(1)2D索引:平面地理位置索引
创建方式:db.collection.ensureIndex({w:"2d"})
位置表示:经纬度[经度,纬度]
取值范围:经度[-180,180]纬度[-90,90]
查询方式
①$near查询:查询距离某个点最近的点 
例db.location.find({w:{$near:[1,1]}}) (默认系统返回距离该点距离为100的所有的点数据)
db.location.find({w:{$near:[1,1],$maxDistance:10}})($maxDistance设置离该点距离为10的所有的点数据)
②$geoWithin查询:查询某个形状内的点
形状的表示:
㈠$box:矩形,使用
{$box:[[<x1>,<y1>],[<x2>,<y2>]]}表示.
例:db.location.find({w:{$geoWithin:{$box:[[0,0],[3,3]]}}})


㈡$center:圆形,使用
{$center:[[<x1>,<y1>],r]}表示.
例:db.location.find({w:{$geoWithin:{$center:[[0,0],5]}}})


㈢$polygon:多边形,使用
{$polygon:[[<x1>,<y1>],[<x2>,<y2>],[<x3>,<y3>]]}表示.
例:db.location.find({w:{$geoWithin:{$polygon:[[0,0],[0,1],[2,5],[6,1]]}}})


③geoNear查询:
geoNear使用runCommand命令进行使用,常用使用如下
运行:db.runCommand({geoNear:"location",near:[1,2],maxDistance:10,num:1})
结果:
{
        "waitedMS" : NumberLong(0),
        "results" : [
                {
                        "dis" : 0,
                        "obj" : {
                                "_id" : ObjectId("5a9e36180fc5665410b5a289"),
                                "w" : [
                                        1,
                                        2
                                ]
                        }
                }
        ],
        "stats" : {
                "nscanned" : 3,
                "objectsLoaded" : 1,
                "avgDistance" : 0,
                "maxDistance" : 0,
                "time" : 47
        },
        "ok" : 1
}


(2)2Dsphere索引:球面地理位置索引
创建方式:db.collection.ensureIndex({w:"2dsphere"})
位置表示方式:
GeoJSON:描述一个点,一条直线,多边形等形状
格式:{type:"",coordinates:[<coordinates>]}
区别:计算地理位置时使用的平面还是球面
查找方式:
(1)查找距离某个点一定距离内的点
(2)查找包含在某区域内的点




#索引的属性
为索引指定名称(名字,name指定)
db.imooc_2.ensureIndex({x:1,y:1,z:1,m:1},{name:"normal_index"})
db.imooc_2.dropIndex("normal_index") #根据索引名称删除索引


为索引指定唯一键(唯一性,unique指定)
db.imooc_2.ensureIndex({m:1,n:1},{unique:true})


为索引设定稀疏性
db.imoo_2.insert({"m":1})
db.imoo_2.insert({"n":1})
db.imooc_2.ensureIndex({m:1},{sparse:true})
不能再稀疏索引上查找文档不存在的记录 .hint("")强制执行指定的索引
db.imooc_2.find({m:{$exists:false}}).hint("m_1")


#索引构建情况分析:
索引好处:加快索引相关的查询


索引不好:增加磁盘空间消耗,降低写入性能


如何评判当前索引构建情况:
1.mongostat工具介绍
mongostat:查看mongodb运行状态的程序
使用说明:mongostat -h 127.0.0.1:27017
idx miss 的值如果不为零则表示建立的索引有问题,需要进行处理.
2.profile集合介绍
db.getProfilingStatus()
db.setProfilingLevel(2)
3.日志介绍
4.explain分析
执行条件:db.imooc_2.find({x:4191}).explain()
结果:
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "imooc.imooc_2",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "n" : {
                                "$eq" : 1
                        }
                },
                "winningPlan" : {
                        "stage" : "COLLSCAN",
                        "filter" : {
                                "n" : {
                                        "$eq" : 1
                                }
                        },
                        "direction" : "forward"
                },
                "rejectedPlans" : [ ]
        },
        "serverInfo" : {
                "host" : "WIN10-801102048",
                "port" : 27017,
                "version" : "3.2.2",
                "gitVersion" : "6e71d0d568e134c029203593b00a0103e7cdf30b"
        },
        "ok" : 1
}


#MongoDB安全概览
1.最安全的是物理隔离:不现实


2.网络隔离其次


3.防火墙再其次


4.用户名密码在最后
①创建用户名密码语法:createUser(2.6之前为addUser)
②{user:"<name>",pwd:"<cleartext password>",customData:{<any information>},roles:[{role:"<role>",db:"<database>"}]}
③角色类型:内建类型(read,readWrite,dbAdmin,dbOwner,userAdmin)
示例:
db.createUser({user:"jzwx",pwd:"jzwx",roles:[{role:"userAdmin",db:"admin"},{role:"read",db:"test"}]}) #创建用户名密码为jzwx的用户该用户具有admin的权限管理用户并且有对test数据库读取数据的权限
运行指令结果显示为:
Successfully added user: {
        "user" : "jzwx",
        "roles" : [
                {
                        "role" : "userAdmin",
                        "db" : "admin"
                },
                {
                        "role" : "read",
                        "db" : "test"
                }
        ]
}
设置好用户名密码后.
在配置文件(E:\mongodb\conf\mongod.conf)中添加将auth设置为true开启权限认证功能auth = true,重启mongodb服务器.重新连接服务器.
登录时输入账号密码格式:mongo 127.0.0.1:27017 -u jzwx -p jzwx


```




猜你喜欢

转载自blog.csdn.net/sinat_30026065/article/details/79464190