mongoDb-学习笔记3
2015-08-07 17:54
525 查看
1 固定集合,必须显示创建
创建固定大小和数量的集合db.createCollection("mycol", { size : 100000, capped : true, max : 100 } )
将非固定集合转换为固定集合 db.runCommand({"convertToCapped":"test","size":100000,"max":100})
db.mycol2.find().sort({$natural:1}) 自然排序,按照磁盘排列顺序返回文档
循环游标:循环游标是一种特殊的游标,当循环游标的结果集被取光之后游标不会被关闭,当有新文档插入时,循环游标会继续取到结果,只能用在固定集合
,超过10分钟没有结果,循环游标会自动关闭。
$autoIndexId 设值为false,可以创建没有_id索引的固定集合
2 ttl索引
time-to-live 可以为集合指定一个日期字段为ttl索引,expireAfterSecs
db.dol.ensureIndex({"updateDate":1},{"expireAfterSecs":60*60*60})
使用collMod命令修改expireAfterSecs的值
db.runCommand({collmod:"dol.update","expireAfterSecs":60*60*60})
3 全文本索引
性能比较低下,尽量不使用,一个集合只能有一个全文本索引,一个全文本索引可以包含多个字段
A 开启 db.adminCommand({"setParameter":1,"textSearchEnabled":true})
B 创建 db.dol.ensureIndex({"updateDate":"text"})
C 需要显示使用 db.runCommand({"text":"dol","search":"ask ,hn"})
4地理空间索引
2dsphere(用于地球表面)和2d(用于平面地图)
5 GridFS
mongofiles --help
mongofiles put fol.txt 上传
mongofiles list
mongofiles get fol.txt 下载
6 聚合框架
aggregate()
管道符
$match db.users.aggregate({$match:{"name":"xiao"}})
$project db.users.aggregate({$match:{"name":"xiao"}},{$project:{"name":1,"_id":0}})
更改字段名 db.users.aggregate({$match:{"name":"xiao"}},{$project:{"username":"$name","_id":0}})
数学表达式:$add $subtract $multiply $divide $mod
日期表达式:$year $month $week $dayOfMonth $dayOfWeek $dayOfYear $hour $minute $second
字符串表达式:$substr $concat $toLower $toUpper
逻辑表达式:$cmp =返回0 <返回-1 >返回1
$strcasecmp $eq $ne $gt $gte $lt $lte $and $or $not
两个控制语句:$cond $ifNull
$group {$group:{_id:"$grade"}}
db.users.aggregate({$project:{"name":1,"_id":0}},{$group:{"_id":"$name","count":{$sum:1}}})
db.users.aggregate({$group:{"_id":"$name","avgprice":{$avg:"$price"}}})
db.users.aggregate({$group:{"_id":"$name","avgprice":{$min:"$price"}}})
db.users.aggregate({$group:{"_id":"$name","avgprice":{$max:"$price"}}})
db.users.aggregate({$group:{"_id":"$name","avgprice":{$first:"$price"}}})
db.users.aggregate({$group:{"_id":"$name","avgprice":{$last:"$price"}}})
$unwind 可以将数组中的每一个值拆分为单独的文档
$sort $limit $skip
聚合命令
db.users.count()
db.users.distinct("name")
创建固定大小和数量的集合db.createCollection("mycol", { size : 100000, capped : true, max : 100 } )
将非固定集合转换为固定集合 db.runCommand({"convertToCapped":"test","size":100000,"max":100})
db.mycol2.find().sort({$natural:1}) 自然排序,按照磁盘排列顺序返回文档
循环游标:循环游标是一种特殊的游标,当循环游标的结果集被取光之后游标不会被关闭,当有新文档插入时,循环游标会继续取到结果,只能用在固定集合
,超过10分钟没有结果,循环游标会自动关闭。
$autoIndexId 设值为false,可以创建没有_id索引的固定集合
2 ttl索引
time-to-live 可以为集合指定一个日期字段为ttl索引,expireAfterSecs
db.dol.ensureIndex({"updateDate":1},{"expireAfterSecs":60*60*60})
使用collMod命令修改expireAfterSecs的值
db.runCommand({collmod:"dol.update","expireAfterSecs":60*60*60})
3 全文本索引
性能比较低下,尽量不使用,一个集合只能有一个全文本索引,一个全文本索引可以包含多个字段
A 开启 db.adminCommand({"setParameter":1,"textSearchEnabled":true})
B 创建 db.dol.ensureIndex({"updateDate":"text"})
C 需要显示使用 db.runCommand({"text":"dol","search":"ask ,hn"})
4地理空间索引
2dsphere(用于地球表面)和2d(用于平面地图)
5 GridFS
mongofiles --help
mongofiles put fol.txt 上传
mongofiles list
mongofiles get fol.txt 下载
6 聚合框架
aggregate()
管道符
$match db.users.aggregate({$match:{"name":"xiao"}})
$project db.users.aggregate({$match:{"name":"xiao"}},{$project:{"name":1,"_id":0}})
更改字段名 db.users.aggregate({$match:{"name":"xiao"}},{$project:{"username":"$name","_id":0}})
数学表达式:$add $subtract $multiply $divide $mod
日期表达式:$year $month $week $dayOfMonth $dayOfWeek $dayOfYear $hour $minute $second
字符串表达式:$substr $concat $toLower $toUpper
逻辑表达式:$cmp =返回0 <返回-1 >返回1
$strcasecmp $eq $ne $gt $gte $lt $lte $and $or $not
两个控制语句:$cond $ifNull
$group {$group:{_id:"$grade"}}
db.users.aggregate({$project:{"name":1,"_id":0}},{$group:{"_id":"$name","count":{$sum:1}}})
db.users.aggregate({$group:{"_id":"$name","avgprice":{$avg:"$price"}}})
db.users.aggregate({$group:{"_id":"$name","avgprice":{$min:"$price"}}})
db.users.aggregate({$group:{"_id":"$name","avgprice":{$max:"$price"}}})
db.users.aggregate({$group:{"_id":"$name","avgprice":{$first:"$price"}}})
db.users.aggregate({$group:{"_id":"$name","avgprice":{$last:"$price"}}})
$unwind 可以将数组中的每一个值拆分为单独的文档
$sort $limit $skip
聚合命令
db.users.count()
db.users.distinct("name")
相关文章推荐
- Linux下mongodb安装及数据导入导出教程
- MongoDB Shell
- MongoDB-3运行笔记
- Oracle海量数据导入MongoDB 采用JAVA反射-Penghao
- MongoDB基本用法
- MongoDB Mongodb.conf 配置 Auth
- mongodb 常用命令
- MongoDB常用操作对比T-Sql
- MongoDB导出、备份、恢复
- MongoDB常用命令大全
- Mongodb与Spring配合使用的例子
- 启动mongodb报1067错误
- MongoDB分片集群配置
- 【MongoDB】3.0 配置文件相关介绍
- mongoDb的索引
- MongoDB基本命令使用
- mongodb初学-java操作mongodb
- mongodb常见管理命令
- mongodb高可用Replica Set
- mongodb增删改查