以下是mongodb的后续各种索引命令补充:
26 多键索引的创建
如上所示,如果x为一个数组的话,则多键索引自动创建了
27 创建复合索引
注意:在查询的时候,z,y的顺序不对也是查询不到值的
28 过期索引
{ "_id" : ObjectId("5b3ad3d980f52d6d4be55924"), "time" : ISODate("2018-07-03T01:39:37.435Z") }
过期索引不能是复合索引,因为不能指定两个时间,另外删除机制是,后台每60秒刷新的定时器,定期删除的,所以时间不一定准确。
29 全文索引的设置
30全文索引的查询
31 或者的形式查找
32 查找不包含的方式
查找aa或者bb不包含gg的
33 用与的方式查找
查找同时包含aa,bb,cc的
34 全文索引的相似度查询,实现百度的排名搜索
score的分数越高,相似度越高
{ "_id" : ObjectId("5b3b11d580f52d6d4be55925"), "article" : "aa bb cc dd ee", "score" : 1.2 }
{ "_id" : ObjectId("5b3b11e080f52d6d4be55926"), "article" : "aa bb rr gg", "score" : 1.25 }
{ "_id" : ObjectId("5b3b11e880f52d6d4be55927"), "article" : "aa bb hh qwerqwer", "score" : 1.25 }
{ "_id" : ObjectId("5b3b15ee80f52d6d4be55928"), "article" : "aa bb", "score" : 1.5 }
35 使用score来排序查询
{ "_id" : ObjectId("5b3b15f480f52d6d4be55929"), "article" : "aa bb cc", "score" : 2 }
{ "_id" : ObjectId("5b3b15ee80f52d6d4be55928"), "article" : "aa bb", "score" : 1.5 }
{ "_id" : ObjectId("5b3b11e080f52d6d4be55926"), "article" : "aa bb rr gg", "score" : 1.25 }
{ "_id" : ObjectId("5b3b11e880f52d6d4be55927"), "article" : "aa bb hh qwerqwer", "score" : 1.25 }
36 全局索引的限制
每次查询,只能指定一个$text
$text不能出现在$nor的查询中
查询中如果包含了$text,hint不再起作用
$text中还不支持中文
37 索引的默认的name都是
key_value_key_value
38 指定索引的unique的值
39 删除索引
40 指定唯一索引
唯一性控制
41地理位置的所有,2d的形式
42 查询离一个点最近的点
注意:面会返回100个查询最近的点
42 以规定距离来继续查询
注意:没有min操作
43 查询以正方形的框框里面的点
含义:在坐标上连线这两个点,为斜对角线,组成正方形,在这个正方形中的点会查询出来
44 以圆形为查询条件
含义:数组为点集合,5为半径
45 以多边形为规则查询
46 以runcommand的形式返回值
db.runCommand()
{
"results" : [
{
"dis" : 0, 数据距离
"obj" : { 返回的文档
"_id" : ObjectId("5b3dc893e5a6f611b8291799"),
"w" : [
"stats" : {
"nscanned" : 1, 扫描了哪些数据
"objectsLoaded" : 1,
"avgDistance" : 0, 平均距离
"maxDistance" : 0, 最大距离
"time" : 0 花费的时间
},
"ok" : 1
}
47 索引的好坏
好处:加快了查询
坏处:增加磁盘空间消耗,降低写入性能
48 mongostat的使用
qr|qw队列,数量大的话性能就不行了
ar是连接数量
49 查看所有的状态值
./bin/mongostat --all
很重要
50 profile的状态
> db.getProfilingStatus()
{ "was" : 0, "slowms" : 100 }
0代表关闭的,1代表所有操作超过slowms的值的所有操作,2代表记录所有操作
51 设置was的值
db.setProfilingLevel(2)
52 查询profile的值
自然倒排序,限制为一条
53 解释profile的意思
{ "op" : "query", "ns" : "test.system.profile", "query" : { }, "ntoreturn" : 0, "ntoskip" : 0, "nscanned" : 0, "nscannedObjects" : 1, "keyUpdates" : 0, "writeConflicts" : 0, "numYield" : 0, "locks" : { "Global" : { "acquireCount" : { "r" : NumberLong(2) } }, "MMAPV1Journal" : { "acquireCount" : { "r" : NumberLong(1) } }, "Database" : { "acquireCount" : { "r" : NumberLong(1) } }, "Collection" : { "acquireCount" : { "R" : NumberLong(1) } } }, "nreturned" : 1, "responseLength" : 409, "millis" : 0, "execStats" : { "stage" : "COLLSCAN", "filter" : { "$and" : [ ] }, "nReturned" : 1, "executionTimeMillisEstimate" : 0, "works" : 3, "advanced" : 1, "needTime" : 1, "needFetch" : 0, "saveState" : 0, "restoreState" : 0, "isEOF" : 1, "invalidates" : 0, "direction" : "forward", "docsExamined" : 1 }, "ts" : ISODate("2018-07-09T05:15:30.581Z"), "client" : "127.0.0.1", "allUsers" : [ ], "user" : "" }
op:是操作类型
ns:是查询的命名空间,数据库.集合名.内容
query:查询条件
ntoreturn:查询的数据数目
ntoskip:跳过的数据数目
nscanned:扫描的索引的数目,通常大于nscannedObjects
nscannedObjects:扫描的实体数目
如果扫描了索引,没有确定数据不是我需要的,才会去扫描实体
建议:上线后是不推荐使用profile的,在测试或者是上线的前一段时间使用,后续要关闭,因为会占用性能。
54 日志的安全性
1-5个v,v越多代表日志越详细
55 用来查看这个查询的详细配置explain
56 安全机制
1 最安全的物理隔离
2 网络隔离
3 防火墙隔离,只允许部分IP访问我的mongodb
4 用户名密码
57 vim的时候,可以使用/斜杠来搜索
db.createUser(,]})
read,readWrite,dbAdmin,dbOwner,userAdmin
db.createUser(]})
经历过的都是修为!
——那些年java攻城狮踩过的坑
领取专属 10元无门槛券
私享最新 技术干货