Files
mongo/client/examples/whereExample.cpp

47 lines
1.0 KiB
C++
Raw Normal View History

2009-01-29 10:30:25 -05:00
// whereExample.cpp
#include <iostream>
2009-01-29 18:38:35 -05:00
#include "client/dbclient.h"
2009-01-29 10:30:25 -05:00
using namespace std;
using namespace mongo;
int main() {
DBClientConnection conn;
string errmsg;
if ( ! conn.connect( "127.0.0.1" , errmsg ) ) {
cout << "couldn't connect : " << errmsg << endl;
throw -11;
}
2009-01-29 10:30:25 -05:00
const char * ns = "test.where";
conn.remove( ns , emptyObj );
2009-01-29 18:38:35 -05:00
conn.insert( ns , BSON( "name" << "eliot" << "num" << 17 ) );
conn.insert( ns , BSON( "name" << "sara" << "num" << 24 ) );
2009-01-29 10:30:25 -05:00
auto_ptr<DBClientCursor> cursor = conn.query( ns , emptyObj );
while ( cursor->more() ) {
BSONObj obj = cursor->next();
cout << "\t" << obj.jsonString() << endl;
}
cout << "now using $where" << endl;
Query q = Query("{}").where("this.name == name" , BSON( "name" << "sara" ));
2009-01-29 10:30:25 -05:00
cursor = conn.query( ns , q );
2009-01-29 10:30:25 -05:00
int num = 0;
while ( cursor->more() ) {
BSONObj obj = cursor->next();
cout << "\t" << obj.jsonString() << endl;
num++;
}
assert( num == 1 );
}