1、固定集合

一般的集合都是动态的,可以自动增长以容纳越来越多的数据。

但MONGODB 还有另外一种集合:固定集合。集合大小创建时指定。如果没有空间了,就会自动删除最老的文档,以释放空间。(类似以一个循环队列)

数据插入是按顺序进行                                              当队列已占满,最老的数据被覆盖

固定集合 :

它在碟式磁盘上写入速度非常快,尤其是有专用磁盘时(不会被其它集合的一些随机

性写操作而打扰)

示例:

创建:Size:存储空间为: 10,000 kb    Max: 记录数

> db.createCollection("capped_t1",{"capped":true,"size":100000,"max":100});
{ "ok" : 1 }
> for (i = 1; i <= 101; i++){ db.capped_t1.insert({_id:i,name:i+' name'}) }
> db.capped_t1.count()
100> db.capped_t1.find().sort({_id:-1}).limit(5)
{ "_id" : 101, "name" : "101 name" }
{ "_id" : 100, "name" : "100 name" }
{ "_id" : 99, "name" : "99 name" }
{ "_id" : 98, "name" : "98 name" }
{ "_id" : 97, "name" : "97 name" }
> db.capped_t1.find().sort({_id:1}).limit(5)
{ "_id" : 2, "name" : "2 name" }
{ "_id" : 3, "name" : "3 name" }
{ "_id" : 4, "name" : "4 name" }
{ "_id" : 5, "name" : "5 name" }
{ "_id" : 6, "name" : "6 name" }
>
我们来检查一下集合的物理排序顺序 : (mongodb里叫自然排序的方法 )

这种方法看到的顺序,就是文档在磁盘上的顺序。

> db.capped_t1.find().sort({$natural:1}).limit(10)
{ "_id" : 2, "name" : "2 name" }
{ "_id" : 3, "name" : "3 name" }
{ "_id" : 4, "name" : "4 name" }
{ "_id" : 5, "name" : "5 name" }
{ "_id" : 6, "name" : "6 name" }
{ "_id" : 7, "name" : "7 name" }
{ "_id" : 8, "name" : "8 name" }
{ "_id" : 9, "name" : "9 name" }
{ "_id" : 10, "name" : "10 name" }
{ "_id" : 11, "name" : "11 name" }
>

这里 插播一内容

上一讲我们讲到一个[稀疏索引],如果我建立了索引后,按索引走是查询不到没有此字段的数据。

这里我们也可以使用{$natural:1}

指定不用索引来强制全表或其它索引搜索。

代码如下:

> db.foo.find({x:{$ne:2}}).hint({$natural:1})
{ "_id" : 0 }
{ "_id" : 1, "x" : 1 }
{ "_id" : 3, "x" : 3 }
>

2.循环游标

循环游标类似于 tail –f 命令 ,当循环游标数据被取光后,游标不会被关闭,当有数据插入后,游标还可以取到结果。

$cursor = $collection->find()->tailable();
while (true) {if (!$cursor->hasNext()) {if ($cursor->dead()) {break;}sleep(1);}else {while ($cursor->hasNext()) {do_stuff($cursor->getNext());}}
}(不能在mongodb shell 中使用循环游标)

循环游标如果超过10分钟没有新的数据插入,游标被释放。

3.没有_id 字段??

我们现在使用的集合中,都有个默认的_id字段。它使用:唯一值,可以在分布数据库中保持数据文档的唯一性。
但如果我数据不修改,只插入,也不管它是不是唯一
指定:  {autoIndexId:false}可以建立无_id集合。
优点:性能有稍许提升;
缺点:集合无法复制;

4.TTL索引

使用TTL 可以设置一个时间范围,如果到了此范围,文档被删除。(开始我理解错误,一直以为TTL 是使用在固定集合中的一个索引,并测试一直发现索引无效,经多次测试,才发现,自己理解错误)

db.log_events.ensureIndex( { "createdAt": 1 }, { expireAfterSeconds: 3600 } )> db.log_events.find({},{createdAt:1,_id:0})
{ "createdAt" : ISODate("2014-07-28T03:59:46.824Z") }
{ "createdAt" : ISODate("2014-07-28T04:01:36.816Z") }
> db.log_events.insert( {    "createdAt": new Date('2014,07,28'),    "logEvent": 2,    "logMessage": "Success!" } )
> db.log_events.find({},{createdAt:1,_id:0})
{ "createdAt" : ISODate("2014-07-28T03:59:46.824Z") }
{ "createdAt" : ISODate("2014-07-28T04:01:36.816Z") }
{ "createdAt" : ISODate("2014-07-27T16:00:00Z") }> db.log_events.find({},{createdAt:1,_id:0})
{ "createdAt" : ISODate("2014-07-28T03:59:46.824Z") }
{ "createdAt" : ISODate("2014-07-28T04:01:36.816Z") }
> > new Date()
ISODate("2014-07-28T05:33:15.398Z")
> db.log_events.find({},{createdAt:1,_id:0})
>

建立索引后,插入数据,并测试查看经过一段时间后,数据已清空。

注意,开始照:MONGODB 权威指南书里的实验来做,简写成:{expireAfterSecs:3600} 是无效的。
实验如下:

> db.bid.insert({_id:1,create_dt:new Date('2014,08,01')})
> db.bid.insert({_id:2,create_dt:new Date('2014,11,01')})
> db.bid.ensureIndex({create_dt:1},{"expireAfterSecs":60*60*24*14})
> db.bid.getIndexes()
[{"v" : 1,"key" : {"_id" : 1},"ns" : "test.bid","name" : "_id_"},{"v" : 1,"key" : {"create_dt" : 1},"ns" : "test.bid","name" : "create_dt_1","expireAfterSecs" : 1209600}
]
> db.bid.find()
{ "_id" : 1, "create_dt" : ISODate("2014-07-31T16:00:00Z") }
{ "_id" : 2, "create_dt" : ISODate("2014-10-31T16:00:00Z") }
> db.bid.find()
{ "_id" : 1, "create_dt" : ISODate("2014-07-31T16:00:00Z") }
{ "_id" : 2, "create_dt" : ISODate("2014-10-31T16:00:00Z") }
> db.bid.find()
{ "_id" : 1, "create_dt" : ISODate("2014-07-31T16:00:00Z") }
{ "_id" : 2, "create_dt" : ISODate("2014-10-31T16:00:00Z") }
> db.bid.find()
{ "_id" : 1, "create_dt" : ISODate("2014-07-31T16:00:00Z") }
{ "_id" : 2, "create_dt" : ISODate("2014-10-31T16:00:00Z") }
> db.bid.find()
{ "_id" : 1, "create_dt" : ISODate("2014-07-31T16:00:00Z") }
{ "_id" : 2, "create_dt" : ISODate("2014-10-31T16:00:00Z") }
> db.bid.find()
{ "_id" : 1, "create_dt" : ISODate("2014-07-31T16:00:00Z") }
{ "_id" : 2, "create_dt" : ISODate("2014-10-31T16:00:00Z") }
> db.bid.find()
{ "_id" : 1, "create_dt" : ISODate("2014-07-31T16:00:00Z") }
{ "_id" : 2, "create_dt" : ISODate("2014-10-31T16:00:00Z") }
> db.runCommand({"collMod":"someapp.cache","expireAfterSecs":60})
{ "ok" : 0, "errmsg" : "ns does not exist" }
> db.runCommand({"collMod":"someapp.cache","expireAfterSecs":60})
{ "ok" : 0, "errmsg" : "ns does not exist" }
> db.bid.find()
{ "_id" : 1, "create_dt" : ISODate("2014-07-31T16:00:00Z") }
{ "_id" : 2, "create_dt" : ISODate("2014-10-31T16:00:00Z") }
> db.bid.find()
{ "_id" : 1, "create_dt" : ISODate("2014-07-31T16:00:00Z") }
{ "_id" : 2, "create_dt" : ISODate("2014-10-31T16:00:00Z") }
> db.bid.find()
{ "_id" : 1, "create_dt" : ISODate("2014-07-31T16:00:00Z") }
{ "_id" : 2, "create_dt" : ISODate("2014-10-31T16:00:00Z") }
> db.bid.find()
{ "_id" : 1, "create_dt" : ISODate("2014-07-31T16:00:00Z") }
{ "_id" : 2, "create_dt" : ISODate("2014-10-31T16:00:00Z") }
> db.bid.find()
{ "_id" : 1, "create_dt" : ISODate("2014-07-31T16:00:00Z") }
{ "_id" : 2, "create_dt" : ISODate("2014-10-31T16:00:00Z") }
> db.bid.find()
{ "_id" : 1, "create_dt" : ISODate("2014-07-31T16:00:00Z") }
{ "_id" : 2, "create_dt" : ISODate("2014-10-31T16:00:00Z") }
> db.bid.find()
{ "_id" : 1, "create_dt" : ISODate("2014-07-31T16:00:00Z") }
{ "_id" : 2, "create_dt" : ISODate("2014-10-31T16:00:00Z") }
> db.bid.getIndexes()
[{"v" : 1,"key" : {"_id" : 1},"ns" : "test.bid","name" : "_id_"},{"v" : 1,"key" : {"create_dt" : 1},"ns" : "test.bid","name" : "create_dt_1","expireAfterSecs" : 1209600}
]
> db.bid.dropIndex("create_dt_1")
{ "nIndexesWas" : 2, "ok" : 1 }
> db.bid.getIndexes()
[{"v" : 1,"key" : {"_id" : 1},"ns" : "test.bid","name" : "_id_"}
]
> db.log_events.ensureIndex( { "createdAt": 1 }, { expireAfterSeconds: 3600 } )
> db.bid.ensureIndex( { "create_dt": 1 }, { expireAfterSeconds: 3600 } )
> db.bid.find()
{ "_id" : 1, "create_dt" : ISODate("2014-07-31T16:00:00Z") }
{ "_id" : 2, "create_dt" : ISODate("2014-10-31T16:00:00Z") }
> db.bid.find()
{ "_id" : 1, "create_dt" : ISODate("2014-07-31T16:00:00Z") }
{ "_id" : 2, "create_dt" : ISODate("2014-10-31T16:00:00Z") }
> db.bid.find()
{ "_id" : 1, "create_dt" : ISODate("2014-07-31T16:00:00Z") }
{ "_id" : 2, "create_dt" : ISODate("2014-10-31T16:00:00Z") }
> db.bid.getIndexes()
[{"v" : 1,"key" : {"_id" : 1},"ns" : "test.bid","name" : "_id_"},{"v" : 1,"key" : {"create_dt" : 1},"ns" : "test.bid","name" : "create_dt_1","expireAfterSeconds" : 3600}
]
> db.bid.getIndexes()
[{"v" : 1,"key" : {"_id" : 1},"ns" : "test.bid","name" : "_id_"},{"v" : 1,"key" : {"create_dt" : 1},"ns" : "test.bid","name" : "create_dt_1","expireAfterSeconds" : 3600}
]
> db.bid.find()
{ "_id" : 1, "create_dt" : ISODate("2014-07-31T16:00:00Z") }
{ "_id" : 2, "create_dt" : ISODate("2014-10-31T16:00:00Z") }
> new Date()
ISODate("2014-11-05T03:10:34.828Z")
> db.bid.find()
{ "_id" : 1, "create_dt" : ISODate("2014-07-31T16:00:00Z") }
{ "_id" : 2, "create_dt" : ISODate("2014-10-31T16:00:00Z") }
> db.bid.find()
> db.bid.find()
> db.bid.dropIndex("create_dt_1")
{ "nIndexesWas" : 2, "ok" : 1 }
> db.bid.ensureIndex( { "create_dt": 1 }, { expireAfterSeconds: 3600*14 } )
> db.bid.insert({_id:1,create_dt:new Date('2014,08,01')})
> db.bid.insert({_id:1,create_dt:new Date('2014,01,01')})
E11000 duplicate key error index: test.bid.$_id_  dup key: { : 1.0 }
> db.bid.insert({_id:2,create_dt:new Date('2014,01,01')})
> db.bid.insert({_id:3,create_dt:new Date('2014,11,01')})
> db.bid.getIndexes()
[{"v" : 1,"key" : {"_id" : 1},"ns" : "test.bid","name" : "_id_"},{"v" : 1,"key" : {"create_dt" : 1},"ns" : "test.bid","name" : "create_dt_1","expireAfterSeconds" : 50400}
]
> db.bid.dropIndex("create_dt_1")
{ "nIndexesWas" : 2, "ok" : 1 }
> db.bid.ensureIndex( { "create_dt": 1 }, { expireAfterSeconds: 3600*24*14 } )
> db.bid.getIndexes()
[{"v" : 1,"key" : {"_id" : 1},"ns" : "test.bid","name" : "_id_"},{"v" : 1,"key" : {"create_dt" : 1},"ns" : "test.bid","name" : "create_dt_1","expireAfterSeconds" : 1209600}
]
> db.bid.find()
> db.bid.find()
> db.bid.insert({_id:3,create_dt:new Date('2014,11,01')})
> db.bid.find()
{ "_id" : 3, "create_dt" : ISODate("2014-10-31T16:00:00Z") }
> db.bid.insert({_id:2,create_dt:new Date('2014,01,01')})
> db.bid.find()
{ "_id" : 3, "create_dt" : ISODate("2014-10-31T16:00:00Z") }
{ "_id" : 2, "create_dt" : ISODate("2013-12-31T16:00:00Z") }
> db.bid.find()
{ "_id" : 3, "create_dt" : ISODate("2014-10-31T16:00:00Z") }
{ "_id" : 2, "create_dt" : ISODate("2013-12-31T16:00:00Z") }
> db.bid.find()
{ "_id" : 3, "create_dt" : ISODate("2014-10-31T16:00:00Z") }
{ "_id" : 2, "create_dt" : ISODate("2013-12-31T16:00:00Z") }
> db.bid.find()
{ "_id" : 3, "create_dt" : ISODate("2014-10-31T16:00:00Z") }
{ "_id" : 2, "create_dt" : ISODate("2013-12-31T16:00:00Z") }
> db.bid.find()
{ "_id" : 3, "create_dt" : ISODate("2014-10-31T16:00:00Z") }
{ "_id" : 2, "create_dt" : ISODate("2013-12-31T16:00:00Z") }
> db.bid.find()
{ "_id" : 3, "create_dt" : ISODate("2014-10-31T16:00:00Z") }
{ "_id" : 2, "create_dt" : ISODate("2013-12-31T16:00:00Z") }
> db.bid.find()
{ "_id" : 3, "create_dt" : ISODate("2014-10-31T16:00:00Z") }
> db.bid.find()
{ "_id" : 3, "create_dt" : ISODate("2014-10-31T16:00:00Z") }
> 

【知识小课堂】mongodb 之 特殊集合及索引相关推荐

  1. 【知识小课堂】 mongodb 之 objectId

    一.OBJECTID 因公司开发人员在使用MONGODB时,总遇到一些小问题.为了增加大家的mongodb 数据库知识. 决定每周进行一.两次的知识小课堂.这里把内容整理出来,上传到博客中.也算是自己 ...

  2. 宝付国际跨境知识小课堂 | 人民币外汇市场是个啥?

    全球化浪潮下,因为各个经济体的比较利益差异,产生了货物.服务.人员或资本之间的流动.各个经济体使用的当地货币不同,为了贸易或资本流动及其价值定价,本国的货币需要兑换成另一国的货币来结算,这个货币兑换的 ...

  3. 【知识小课堂】mongodb 之 查询关键词使用

    查询关键词: $or 多个条件 满足其中一个 即可: Syntax:{ $or: [ { <expression1> }, { <expression2> }, ... , { ...

  4. 【知识小课堂】 mongodb 之字段中的【 数组】、【内嵌文档】

    一.介绍 MONGODB 的表结构 很灵活 .主要还是因为 字段中可以包含 [ 数组].[内嵌文档]. 现在简单介绍一下 字段中的[ 数组].[内嵌文档]相关的一些操作 (为了方便理解,还是以表来理解 ...

  5. 计算机知识小课堂宣传标语,创建高效课堂宣传标语50条

    创建高效课堂宣传标语50条 在日常学习.工作或生活中,大家都接触过很多优秀的标语吧,标语是指文字简练.意义鲜明的宣传.鼓动口号.那什么样的标语才算得上是经典呢?以下是小编精心整理的创建高效课堂宣传标语 ...

  6. 【知识小课堂】 之 聚合函数

    我们先来看几个简单的聚合命令: 1.count > db.foo.count() 4 > db.foo.find({_id:{$gte:2}}) { "_id" : 2 ...

  7. 家电知识小课堂 帮您避过选购的那些坑

    本文来自IT168 大家在选购家电时,是否会或多或少拥有一些困惑呢?面对琳琅满目的众多产品,不知道该如何选择,甚至一不小心就可能陷入某些选购陷阱之中,多花了冤枉钱.下面我们就来为大家普及一些家电选购的 ...

  8. 模具制造设计知识小课堂

    "潇洒模具"--模具制造设计培训讲堂: 一,选择模具钢时什么是最重要的和最具有决定性意义的因素? 成形方法:可从两种基本材料类型中选择. A) 热加工工具钢,它能承受模铸.锻造和挤 ...

  9. 【知识小课堂】4 之 索引

    索引类型 mongoDB 的索引在存储结构都是一样的,但是根据不同的应用需求,还是分成了:唯一索引,稀疏索引,复合索引 1.唯一索引 MONGODB 在默认建立文档时,都会自动添加一个:"_ ...

最新文章

  1. 怎么找思科答案最快_成功闯入思科的面试经历
  2. 关于nginx反向代理产生大量连接问题解决。
  3. 基于人工神经网络的不规则小天体引力场建模
  4. error LNK2001: unresolved external symbol QtCored.lib using staic Qt lib
  5. 山东省计算机春季高考大纲,2017年山东春季高考专业知识考试大纲.doc
  6. error: binding reference of type int to const int discards qualifiers
  7. 手机网页底部广告代码,悬浮底部广告,带关闭开关,复制文字,按钮
  8. 计算机组装中编制配置单的方法,diy电脑配置单2017 电脑组装配置清单及价格
  9. [VOT10](2022CVPR)TCTrack: Temporal Contexts for Aerial Tracking
  10. 北大数学英才班,没有一名新生经历高三
  11. 个人学习(解决)练习ssm框架遇到的问题No qualifying bean of type ‘service.BookTypeService‘ available:
  12. JZ-008-跳台阶
  13. iOS支付宝、微信支付
  14. Xcode10.1安装插件
  15. python输出画图html,python画柱状图并且输出到html文件
  16. PMC Organometallix继续业务扩张
  17. 将word文档转换为html文档,将word文档转化为html(代码)
  18. 马云:初次创业 我对科技和计算机一无所知
  19. React google map
  20. 处理使用node-gpy时遇到的Can't find msbuild.exe错误

热门文章

  1. 第一天开始学习使用git中遇到的问题
  2. Linux查看版本当前操作系统内核信息
  3. hadoop2 5个环境配置文件
  4. javascript动画效果之透明度
  5. 在Windows Server 2008的桌面上显示“我的电脑”“网上邻居”等图标?
  6. 对CSS了解-overflow:hidden
  7. android ProgressBar 自定义进度条颜色
  8. git移除某文件夹的版本控制
  9. [转] 图解Seq2Seq模型、RNN结构、Encoder-Decoder模型 到 Attention
  10. 如何学习Linux性能优化?