3
/* Copyright 2009 10gen Inc.
5
* Licensed under the Apache License, Version 2.0 (the "License");
6
* you may not use this file except in compliance with the License.
7
* You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
/* example of using a tailable cursor */
20
#include "../../client/dbclient.h"
21
#include "../../util/goodies.h"
23
using namespace mongo;
27
/* "tail" the specified namespace, outputting elements as they are added.
28
_id values must be inserted in increasing order for this to work. (Some other
29
field could also be used.)
31
Note: one could use a capped collection and $natural order to do something
32
similar, using sort({$natural:1}), and then not need to worry about
35
void tail(DBClientBase& conn, const char *ns) {
36
conn.ensureIndex(ns, fromjson("{_id:1}"));
38
Query query = Query().sort("_id");
40
auto_ptr<DBClientCursor> c = conn.query(ns, query, 0, 0, 0, Option_CursorTailable);
49
BSONObj o = c->next();
51
cout << o.toString() << endl;
53
query = QUERY( "_id" << GT << lastId ).sort("_id");