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