MongoDB Tailable Cursors

來源:互聯網
上載者:User

標籤:cursors   tailable   cursor   


MongoDB Tailable Cursors

MongoDB 有一個叫 Tailable Cursors的特性,它類似於tail -f 命令,你在一個Capped Collection上面執行查詢操作,當操作完成後,你可以不關閉返回的資料Cursor,並持續地從中讀出新加入的資料。

在高寫入的Capped Collection上,索引不可用時,可使用Tailable Cursors。例如,MongoDB複製使用了Tailable Cursors來擷取Primary的尾oplog日誌。

考慮以下與Tailable Cursors相關的行為:

  • Tailable Cursors不使用索引,並以自然排序返迴文檔。

  • 因為Tailable Cursors不使用索引,查詢的初始掃描非常耗效能;但是,遊標初始化完後,隨後擷取到的新增加的文檔是很快速的。

  • Tailable Cursors如果遇到以下情況之一將會僵死或無效:

    • 查詢無匹配結果。

    • 遊標在集合尾部返迴文檔,隨後應用程式刪除了該文檔。

僵死的遊標id為0。


DBQuery.Option.awaitData

在使用TailableCursor時,此參數會在資料讀盡時先阻塞一小段時間後再讀取一次並進行返回。

跟蹤oplog的樣本

use localvar cursor = db.oplog.rs.find({"op" : "u", "ns" : "MyDB.Product"},{"ts": 1, "o2._id": 1}).addOption(DBQuery.Option.tailable).addOption(DBQuery.Option.awaitData);while(cursor.hasNext()){    var doc = cursor.next();    printjson(doc);};


2.6版的遊標方法:
cursor.addOption()
https://docs.mongodb.com/v2.6/reference/method/cursor.addOption/

3.2版的遊標方法:
cursor.tailable()
https://docs.mongodb.com/manual/reference/method/cursor.tailable/


本文出自 “SQL Server Deep Dives” 部落格,請務必保留此出處http://ultrasql.blog.51cto.com/9591438/1789038

MongoDB Tailable Cursors

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.