diff options
Diffstat (limited to 'src/mongo/client/examples/tutorial.cpp')
| -rw-r--r-- | src/mongo/client/examples/tutorial.cpp | 34 |
1 files changed, 27 insertions, 7 deletions
diff --git a/src/mongo/client/examples/tutorial.cpp b/src/mongo/client/examples/tutorial.cpp index 12c5c7e71a9..7b5e3bf10a9 100644 --- a/src/mongo/client/examples/tutorial.cpp +++ b/src/mongo/client/examples/tutorial.cpp @@ -23,15 +23,28 @@ using namespace mongo; -void printIfAge(DBClientConnection& c, int age) { - auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", QUERY( "age" << age ).sort("name") ); +int printIfAge(DBClientConnection& c, int age) { + std::auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", QUERY( "age" << age ).sort("name") ); + if (!cursor.get()) { + cout << "query failure" << endl; + return EXIT_FAILURE; + } + while( cursor->more() ) { BSONObj p = cursor->next(); cout << p.getStringField("name") << endl; } + return EXIT_SUCCESS; } -void run() { +int run() { + + Status status = client::initialize(); + if ( !status.isOK() ) { + std::cout << "failed to initialize the client driver: " << status.toString() << endl; + return EXIT_FAILURE; + } + DBClientConnection c; c.connect("localhost"); //"192.168.58.1"); cout << "connected ok" << endl; @@ -50,21 +63,28 @@ void run() { cout << "count:" << c.count("tutorial.persons") << endl; - auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", BSONObj()); + std::auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", BSONObj()); + if (!cursor.get()) { + cout << "query failure" << endl; + return EXIT_FAILURE; + } + while( cursor->more() ) { cout << cursor->next().toString() << endl; } cout << "\nprintifage:\n"; - printIfAge(c, 33); + return printIfAge(c, 33); } int main() { + int ret = EXIT_SUCCESS; try { - run(); + ret = run(); } catch( DBException &e ) { cout << "caught " << e.what() << endl; + ret = EXIT_FAILURE; } - return 0; + return ret; } |
