Advertisement
Guest User

Untitled

a guest
Oct 17th, 2013
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.81 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. #include "mongo/client/dbclient.h"
  4.  
  5. using namespace std;
  6.  
  7. void insert( mongo::DBClientConnection & conn , const char * name , int num ) {
  8.     mongo::BSONObjBuilder obj;
  9.     obj.append( "name" , name );
  10.     obj.append( "num" , num );
  11.     conn.insert( "test.people" , obj.obj() );
  12. }
  13.  
  14. int main( int argc, const char **argv ) {
  15.  
  16.     const char *port = "27017";
  17.     if ( argc != 1 ) {
  18.         if ( argc != 3 )
  19.             throw -12;
  20.         port = argv[ 2 ];
  21.     }
  22.  
  23.     mongo::DBClientConnection conn;
  24.     string errmsg;
  25.     if ( ! conn.connect( string( "127.0.0.1:" ) + port , errmsg ) ) {
  26.         cout << "couldn't connect : " << errmsg << endl;
  27.         throw -11;
  28.     }
  29.  
  30.     {
  31.         // clean up old data from any previous tests
  32.         mongo::BSONObjBuilder query;
  33.         conn.remove( "test.people" , query.obj() );
  34.     }
  35.  
  36.     insert( conn, "a", 1 );
  37.     insert( conn, "a", 2 );
  38.     insert( conn, "a", 3 );
  39.     insert( conn, "a", 4 );
  40.     insert( conn, "a", 5 );
  41.     insert( conn, "a", 6 );
  42.     insert( conn, "a", 7 );
  43.     insert( conn, "a", 8 );
  44.     insert( conn, "a", 9 );
  45.     insert( conn, "a", 10 );
  46.  
  47.     int test[2];
  48.     test[0] = 5;
  49.     test[1] = 1;
  50.     test[2] = 3;
  51.  
  52.     mongo::BSONObjBuilder obj;
  53.     obj.append( "name" , "wut");
  54.     obj.appendBinData( "binTest",sizeof(test), BinDataGeneral, test);
  55.     conn.insert( "test.people" , obj.obj() );
  56.  
  57.     {
  58.         mongo::BSONObj query = BSON( "num" << BSON( "$nin" << BSON_ARRAY(1 << 5 << 10) ) );
  59.         cout << query << endl;
  60.  
  61.         auto_ptr<mongo::DBClientCursor> cursor = conn.query( "test.people" , query );
  62.         cout << "using cursor" << endl;
  63.         while ( cursor->more() ) {
  64.             mongo::BSONObj obj = cursor->next();
  65.             cout << "\t" << obj.jsonString() << endl;
  66.         }
  67.  
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement