Skip to content

DOCS-6720 remove legacy C++ driver example #2439

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 0 additions & 116 deletions source/tutorial/create-tailable-cursor.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,119 +55,3 @@ Consider the following behaviors related to tailable cursors:

See your :doc:`driver documentation </applications/drivers>` for the
driver-specific method to specify the tailable cursor.

C++ Example
-----------

The ``tail`` function uses a tailable cursor to output the results from
a query to a capped collection:

- The function handles the case of the dead cursor by having the query
be inside a loop.

- To periodically check for new data, the ``cursor->more()`` statement
is also inside a loop.

.. code-block:: cpp

#include "client/dbclient.h"

using namespace mongo;

/*
* Example of a tailable cursor.
* The function "tails" the capped collection (ns) and output elements as they are added.
* The function also handles the possibility of a dead cursor by tracking the field 'insertDate'.
* New documents are added with increasing values of 'insertDate'.
*/

void tail(DBClientBase& conn, const char *ns) {

BSONElement lastValue = minKey.firstElement();

Query query = Query().hint( BSON( "$natural" << 1 ) );

while ( 1 ) {
auto_ptr<DBClientCursor> c =
conn.query(ns, query, 0, 0, 0,
QueryOption_CursorTailable | QueryOption_AwaitData );

while ( 1 ) {
if ( !c->more() ) {

if ( c->isDead() ) {
break;
}

continue;
}

BSONObj o = c->next();
lastValue = o["insertDate"];
cout << o.toString() << endl;
}

query = QUERY( "insertDate" << GT << lastValue ).hint( BSON( "$natural" << 1 ) );
}
}

The ``tail`` function performs the following actions:

- Initialize the ``lastValue`` variable, which tracks the last
accessed value. The function will use the ``lastValue`` if the
cursor becomes *invalid* and ``tail`` needs to restart the
query. Use :method:`~cursor.hint()` to ensure that the query uses
the :operator:`$natural` order.

- In an outer ``while(1)`` loop,

- Query the capped collection and return a tailable cursor that
blocks for several seconds waiting for new documents

.. code-block:: cpp

auto_ptr<DBClientCursor> c =
conn.query(ns, query, 0, 0, 0,
QueryOption_CursorTailable | QueryOption_AwaitData );

- Specify the capped collection using ``ns`` as an argument
to the function.

- Set the ``QueryOption_CursorTailable`` option to create a
tailable cursor.

- Set the ``QueryOption_AwaitData`` option so that the returned
cursor blocks for a few seconds to wait for data.

- In an inner ``while (1)`` loop, read the documents from the cursor:

- If the cursor has no more documents and is not invalid, loop the
inner ``while`` loop to recheck for more documents.

- If the cursor has no more documents and is dead, break the inner
``while`` loop.

- If the cursor has documents:

- output the document,

- update the ``lastValue`` value,

- and loop the inner ``while (1)`` loop to recheck for more
documents.

- If the logic breaks out of the inner ``while (1)`` loop and the
cursor is invalid:

- Use the ``lastValue`` value to create a new query condition that
matches documents added after the ``lastValue``. Explicitly
ensure ``$natural`` order with the ``hint()`` method:

.. code-block:: cpp

query = QUERY( "insertDate" << GT << lastValue ).hint( BSON( "$natural" << 1 ) );

- Loop through the outer ``while (1)`` loop to re-query with the new query
condition and repeat.

.. seealso:: `Detailed blog post on tailable cursor <http://shtylman.com/post/the-tail-of-mongodb>`_