Get the App
SLTechnology News&Howtos  ›  Database  › 

MongoDB Tailable Cursors

Shulou Source: shulou.com Published: 2022-06-01 19:39:34 10月03日 Update

MongoDB Tailable Cursors

MongoDB has a feature called Tailable Cursors, which is similar to the tail-f command. You perform a query operation on a Capped Collection. When the operation is completed, you can not close the returned data Cursor and continue to read the newly added data from it.

On highly written Capped Collection, Tailable Cursors can be used when the index is not available. For example, MongoDB replication uses Tailable Cursors to get the tail oplog log of Primary.

Consider the following Tailable Cursors-related behaviors:

Tailable Cursors does not use an index and returns the document in a natural sort.

Because Tailable Cursors does not use indexes, the initial scan of the query consumes performance; however, after the cursor is initialized, the newly added documents are obtained very quickly.

Tailable Cursors will be dead or invalid if one of the following situations occurs:

The query has no matching results.

The cursor returns the document at the end of the collection, which is then deleted by the application.

The dead cursor id is 0.

DBQuery.Option.awaitData

When using TailableCursor, this parameter blocks for a short period of time when the data is read out, and then reads it again 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.Option.tailable) .addOption (DBQuery.Option.awaitData); while (cursor.hasNext ()) {var doc = cursor.next (); printjson (doc);}

Version 2.6 of the cursor method:

Cursor.addOption ()

Https://docs.mongodb.com/v2.6/reference/method/cursor.addOption/

Vernier method version 3.2:

Cursor.tailable ()

Https://docs.mongodb.com/manual/reference/method/cursor.tailable/

Tags: Cursors data documents indexes queries methods from parameters commands tails applications performance conditions logs time properties examples programs results natural Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Xiaomi Docker Shulou Technology macOS