#include #include #include #include #ifdef _WIN32 #define sleep(_n) Sleep((_n) * 1000) #endif static void print_bson (const bson_t *b) { char *str; str = bson_as_json(b, NULL); fprintf(stdout, "%s\n", str); bson_free(str); } static mongoc_cursor_t * query_collection (mongoc_collection_t *collection) { mongoc_cursor_t *cursor; bson_t query; bson_t gt; int fflags = (MONGOC_QUERY_TAILABLE_CURSOR | MONGOC_QUERY_SLAVE_OK); BSON_ASSERT(collection); bson_init(&query); bson_append_document_begin(&query, "ts", 2, >); bson_append_timestamp(>, "$gt", 3, 0, 0); bson_append_document_end(&query, >); cursor = mongoc_collection_find(collection, (mongoc_query_flags_t)fflags, 0, 0, 0, &query, NULL, NULL); bson_destroy(&query); return cursor; } static void tail_collection (mongoc_collection_t *collection) { mongoc_cursor_t *cursor; uint32_t last_time; const bson_t *doc; bson_error_t error; bson_iter_t iter; BSON_ASSERT(collection); cursor = query_collection(collection); while (true) { while (true) { if (!mongoc_cursor_error(cursor, &error) && mongoc_cursor_more(cursor) && mongoc_cursor_next(cursor, &doc)) { print_bson(doc); } else { break; } } if (mongoc_cursor_error(cursor, &error)) { fprintf(stderr, "%s\n", error.message); exit(1); } fprintf(stdout, "%s\n", "another iteration"); sleep(1); } mongoc_cursor_destroy(cursor); } int main (int argc, char *argv[]) { mongoc_collection_t *collection; mongoc_client_t *client; if (argc != 2) { fprintf(stderr, "usage: %s MONGO_URI\n", argv[0]); return EXIT_FAILURE; } mongoc_init(); client = mongoc_client_new(argv[1]); if (!client) { fprintf(stderr, "Invalid URI: \"%s\"\n", argv[1]); return EXIT_FAILURE; } mongoc_client_set_error_api (client, 2); collection = mongoc_client_get_collection(client, "local", "oplog.rs"); tail_collection(collection); mongoc_collection_destroy(collection); mongoc_client_destroy(client); return 0; }