MongoDB tailable Cursors

Source: Internet
Author: User


MongoDB tailable Cursors

MongoDB has a feature called tailable cursors, which resembles the tail-f command, you perform a query operation on a capped collection, and when the operation is complete, you can not close the returned data cursor, and continue to read the newly added data from it.

On high-write capped collection, the Tailable Cursors can be used when the index is not available. For example, MongoDB replication uses tailable cursors to get Primary's tail oplog logs.

Consider the following behavior related to tailable cursors:

    • Tailable cursors does not use indexes and returns documents in a natural sort.

    • Because tailable cursors does not use an index, the initial scan of the query is very performance-intensive, but after the cursor is initialized, the newly added document that is subsequently obtained is very fast.

    • Tailable Cursors If you encounter one of the following conditions, it will be zombie or invalid:

      • The query has no matching results.

      • The cursor returns the document at the end of the collection, and then the application deletes the document.

The zombie cursor ID is 0.


DBQuery.Option.awaitData

When using Tailablecursor, this parameter blocks a short period of time before the data is read, and then reads and returns.

Example of tracking Oplog

Use Localvar cursor = Db.oplog.rs.find ({"Op": "U", "ns": "Mydb.product"},{"ts": 1, "o2._id": 1}). AddOption (Dbquery.optio    n.tailable). AddOption (DBQuery.Option.awaitData), while (Cursor.hasnext ()) {var doc = Cursor.next (); Printjson (doc);};


Cursor Methods for version 2.6:
Cursor.addoption ()
https://docs.mongodb.com/v2.6/reference/method/cursor.addOption/

Cursor Methods for version 3.2:
Cursor.tailable ()
https://docs.mongodb.com/manual/reference/method/cursor.tailable/


This article is from the SQL Server deep dives blog, so be sure to keep this source http://ultrasql.blog.51cto.com/9591438/1789038

MongoDB tailable Cursors

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.