2009-01-14 15:40:54 +01:00
|
|
|
// second.cpp
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include "mongo/client/dbclient.h"
|
|
|
|
|
|
|
|
using namespace std;
|
2009-01-14 23:09:51 +01:00
|
|
|
using namespace mongo;
|
2009-01-14 15:40:54 +01:00
|
|
|
|
2009-01-14 23:17:24 +01:00
|
|
|
int main() {
|
2009-01-14 15:40:54 +01:00
|
|
|
|
|
|
|
DBClientConnection conn;
|
|
|
|
string errmsg;
|
2009-01-14 23:17:24 +01:00
|
|
|
if ( ! conn.connect( "127.0.0.1" , errmsg ) ) {
|
2009-01-14 15:40:54 +01:00
|
|
|
cout << "couldn't connect : " << errmsg << endl;
|
|
|
|
throw -11;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * ns = "test.second";
|
|
|
|
|
|
|
|
conn.remove( ns , emptyObj );
|
2009-01-14 23:17:24 +01:00
|
|
|
|
2009-01-14 15:40:54 +01:00
|
|
|
conn.insert( ns , BUILDOBJ( "name" << "eliot" << "num" << 17 ) );
|
|
|
|
conn.insert( ns , BUILDOBJ( "name" << "sara" << "num" << 24 ) );
|
|
|
|
|
|
|
|
auto_ptr<DBClientCursor> cursor = conn.query( ns , emptyObj );
|
|
|
|
cout << "using cursor" << endl;
|
2009-01-14 23:17:24 +01:00
|
|
|
while ( cursor->more() ) {
|
2009-01-14 15:40:54 +01:00
|
|
|
BSONObj obj = cursor->next();
|
|
|
|
cout << "\t" << obj.jsonString() << endl;
|
|
|
|
}
|
2009-01-14 16:40:15 +01:00
|
|
|
|
2009-01-14 16:44:25 +01:00
|
|
|
conn.ensureIndex( ns , BUILDOBJ( "name" << 1 << "num" << -1 ) );
|
2009-01-14 15:40:54 +01:00
|
|
|
}
|