Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

MongoDB Tailable Cursors

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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/

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report