mongod命令

这篇博客详细介绍了MongoDB的基本数据操作,如查询、插入和更新,以及索引的创建和管理,包括不同类型的索引如单键、复合、过期和全文索引。还提及了地理位置索引的使用和查询方式。此外,讨论了MongoDB的权限认证机制,包括auth和keyfile的启用以及用户和角色的创建。

db.imooc_collection.find();

db.imooc_collection.insert({x:2,_id:1})
db.imooc_collection.find({x:1})
for(i=3;i<100;i++) db.imooc_collection.insert({x:i})
db.imooc_collection.find().count()

db.imooc_collection.skip(3).limit(2).sort({x:1})

db.imooc_collection.update({x:1},{x:999}) 将x:1的记录更新为x:999
db.imooc_collection.insert({x:100,y:100,z:100}) 
db.imooc_collection.update({z:100},{$set:{y:99}}) 只是将上条记录的y更改

索引的种类:
    id索引:db.imooc_2.getIndexs() 得到imooc_2集合上的所有索引
    单键索引:db.imooc_2.ensuerIndex({x:1}) 创建单键索引:在x字段上建立索引,1表示排序的方向,还可以是-1
    复合索引:db.imooc_2.ensuerIndex({x:1,y:1})
    过期索引:
        索引过期后,相应的数据被删除。
        db.imooc_2.ensuerIndex({time:1},{expireAfterSeconds:30})  在time字段建立索引,30秒后过期删除
        db.imooc_2.insert({time:new Date()})
        索引字段的值必须是指定的时间类型(ISODate或其数组)
        如果字段值是数组,只要数组中最近的时间到了,就删除
        过期索引不能是复合索引
        删除时间不精确,有误差
    全文索引:
        db.imooc_2.ensuerIndex({key:"text"}) 在key字段上建立全文索引
        db.imooc_2.ensuerIndex({"$**":"text"}) 在所有字段上建立索引
        db.imooc_2.find({$text:{$search:"arg0 arg1 -arg2..."}}) 查找含有arg0或arg1但是不包含arg2的记录
        db.imooc_2.find({$text:{$search:"arg0" "arg1"}}) 及含有arg0又含有arg1的记录
        db.imooc_2.find({$text:{$search:"values"}},{score:{$mata:"textScore"}}).sort()对"values"进行查找,并计算与结果的相似度,然后排序
        
        全文索引的限制:
            每次查询,只能指定一个$text查询
            $text查询不能出现在$nor查询中
            查询中如果包含了$text, hint不再起作用
    地理位置索引:
        2d索引,存储查找平面上的点(纬度:[-90,90];经度:[-180,180])
        2dsphere索引,存储查找球面上的点
        
        查找方式:
            1.查找距离某个点一定距离的点
            2.查找包含在某区域的点
        db.localtion.ensuerIndex({w:"2d"}) 在w字段上创建地理位置索引,****与全文索引的创建方式类似
        db.localtion.find({w:{$near:[1,1],$maxDistance:10}})查找与点(1,1)距离为10的点
        $box:矩形 {$box:[[x1,y1],[x2,y2]]}左上方点,右下方点
        $center:圆形 {$center:[[x1,y2],r]}
        $polygon:多边形 {$polygon:[[x1,y1],[x2,y2]...]}
        db.localtion.find({w:{$geoWithin:{$box:[[x1,y1],[x2,y2]]}}})
        
        geoNear查询:
            geoNear使用runCommand命令进行使用
            db.runCommand({geoNear:<collection>,
                                    near:[x1,y1],
                                    minDistance:(对2d索引无效),
                                    maxDistance:数字,
                                    num:返回数目
                        })
        db.localtion.ensuerIndex({w:"2dsphere"})
        位置描述使用GeoJSON

索引的属性:
    名字,name指定
        db.collection.ensuerIndex({字段名,排序方向},{name:""})
    唯一性,unique指定
        db.collection.ensuerIndex({},{unique:true/false})
    稀疏性,sparse指定:不必为不存在的字段创建索引,减少磁盘占用,提高插入速度
        db.collection.ensuerIndex({},{sparse:true/false})
    是否定时删除,expireAfterSeconds指定
        TTL,过期索引
        
        
        
索引构建情况分析
    mongostat工具 mongostat -h IP:PORT 查看帮助文档,了解输入参数及其意义
    profile集合  在mongo中建表监视操作轨迹
    日志
    explain分析 在命令后加 .explain() 分析
    
    
MongoDB开启权限认证有两种方法
    auth开启 在mongod.conf 中加入一行 auth=true 
    keyfile开启
    
    MongoDB创建用户
        创建语法: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:"imooc",pwd:"imooc", roles:[{role:"userAdmin",db:"admin"},{role:"read",db:"test"}]})
            
        角色详解:数据库角色(内建类型)、集群角色、备份角色、其他特殊权限。其他的一般不开放
        createRole函数可以创建权限

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值