建立文件夹data,log,conf,bin,将编译好的mongod放入bin,在conf中新建mongod.conf文件
port = 12345
dbpath = data
logpath = log/mongod.log
fork = true # linux下表示启动fork进程,windows下无效
启动mongodb: bin/mongod -f conf/mongod.conf
上述安装方法同时适用windows和linux.mongodb的安装包解压后bin目录包含已编译好的可执行程序.
使用mongdb客户端:mongo 127.0.0.1:12345/test
关闭服务:db.shutdownServer()
使用update语句,至少接受2个参数
- db.imooc_collection.insert({x:100, y:100, z:100})
- db.imooc_collection.update({z:100}, {$set:{y:99}})
set是部分更新操作符,只有y改为99,x和z不变
存在的字段会更新,不存在的字段会保持原样
- 试图更新一条不存在的数据时db.imooc_collection.update({y:100}, {y:999})
返回结果WriteResult({“nMatched”: 0, “nUpserted”: 0, “nModified”: 0}) 没有做任何操作,也不报错
- db.imooc_collection.find({y: 999})
无返回,表明刚才的update并没有新建文档
- db.imooc_collection.update({y:100}, {y:999}, true)
true表示如果数据不存在,就写入新的数据
- db.imooc_collection.find({y:999})
返回一条数据
默认情况下,update语句只更新一条找到的数据,这样设计是为了防止误操作
- db.imooc_collection.update({c:1}, {$set:{c:2}}, false, true)
会将所有c:1的文档改为c:2,且其他元素不变
在数据量较小是,不使用索引查询数据也很快,但数据量大时,使用索引会加快查询速度
use imooc
db.imooc_collection.getIndexes() 显示集合的索引
db.imooc_collection.ensureIndex({x:1})
x为1代表正向排序,-1表示倒向排序
如果文档较多,创建索引消耗时间多,如果系统负载较大,且有很多存在的文档,不能直接使用这个命令,否则严重影响数据库的性能
{author:"", title:"", article:""}
db.articles.ensureIndex({key:"text"})
db.articles.ensureIndex({key_1:"text", key_2:"text"})
db.articles.ensureIndex({"$**":"text"}) 表示对集合中所有字段创建大的全文索引
注意: 一个数据集合只允许创建一个全文索引
使用全文索引:
db.articles.find({$text: {$search: "coffee"}})
db.articles.find({$text: {$search: "aa bb cc"}}) 查找多个关键词,用空格隔开 (或查询)
db.articles.find({$text: {$search: "aa bb -cc"}}) 包含aa或bb, 不包含cc
db.articles.find({$text: {$search: "\"aa\" \"bb\" \"cc\""}}) 包含aa且包含bb且cc
全文索引相似度查询:
$meta操作符: {score:{$meta: "textScore"}}写在查询条件后面可以返回 返回结果 的相似度.
与sort一起使用,可以达到很好的实用效果
db.articles.find({$text: {$search: "aa bb cc"}}, {score:{$meta:"textScore"}})
返回结果中会多了一个score字段, 字段的值越大,相似度越高
db.articles.find({$text: {$search: "aa bb cc"}}, {score:{$meta:"textScore"}}).sort({score:{$meta:"textScore"}})
全文索引的使用限制:
每次查询,只能指定一个$text查询
$text查询不能出现在$nor查询中
查询中如果包含了$text,hint不再起作用 (hint的作用是指定使用的索引)
很可惜,mongodb全文索引还不支持中文
索引名字长度限制:125字节.
db.imooc_2.ensureIndex({x:1,y:1,z:1}, {name:”normal_index”}) 自己给索引命名
db.imooc_2.dropIndex({“normal_index”}) 删除索引可以使用名字
唯一性,unique指定:db.collection.ensureIndex({},{unique:true/false})
例如,db.imooc_2.ensureIndex({m:1,n:1}, {unique:true})
db.imooc_2.insert({m:1,n:2})
db.imooc_2.insert({m:1,n:2}) 报错,提示key冲突
稀疏性, sparse指定: 默认不稀疏
db.collection.ensureIndex({},{sparse:true/false})
如果为一个字段创建了索引,但是插入了一条数据没有该字段,如果指定了sparse为true,则不会为这条记录创建索引.
稀疏性的好处是,节省磁盘空间,提高插入速度
是否定时删除,expireAfterSeconds指定:TTL,过期索引
2D索引: 平面地理位置索引
创建方式db.collection.ensureIndex({w:"2d"})
例如db.location.ensureIndex({"w":"2d"})
位置表示方式:经纬度[经度,纬度]
取值范围:经度[-180,180] 纬度[-90,90]
db.location.insert(w:[1,1])
db.location.insert(w:[100, 100]) # 纬度超过范围,但不会报错,但查询时会有错误
db.location.find({w:{$near:[1,1], $maxDistance:10}})
near会默认返回100个, maxDistance规定最大距离是10
$geoWithin查询: 查询某个形状内的点.
1. $box: 矩形,使用
{$box:[[, ],[, ]]} 第一个坐标表示左边界,第二个坐标表示右边界.
2. $center: 圆形,使用
{$center:[[,],r]}表示 x1和y1表示圆心位置, r表示半径
3. $polygon: 多边形,使用{$polygon:[[,],[,],[,]]}
使用geoWithin查询
db.location.find({w:{$geoWithin:{$box:[[0,0],[3,3]]}}}) box查询矩形中的点
db.location.find({w:{$geoWithin:{#center:[[0,0],5]}}})
db.location.find({w:{$geoWithin:{$polygon:[[0,0],[0,1],[2,5],[6,1]]}}})
使用geoNear查询:near查询操作符的进化版,geoNear使用runCommand命令进行使用,常用使用如下:
db.runCommand({
geoNear:,
near:[x,y],
minDistance:(对2D索引无效),
maxDistance:
num:
...})
2Dsphere索引: 球面地理位置索引
port=12345
dbpath=data
logpath=log/mongod.log
bind_ip=127.0.0.1
verbose=vvvvv
fork=true
auth=true
并且重启mongod服务
角色类型: 内建类型(read,readWrite,dbAdmin,dbOwner,userAdmin)
keyfile开启