Advertisement
Guest User

Untitled

a guest
Oct 17th, 2013
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 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. /*
  37. insert( conn, "a", 1 );
  38. insert( conn, "a", 2 );
  39. insert( conn, "a", 3 );
  40. insert( conn, "a", 4 );
  41. insert( conn, "a", 5 );
  42. insert( conn, "a", 6 );
  43. insert( conn, "a", 7 );
  44. insert( conn, "a", 8 );
  45. insert( conn, "a", 9 );
  46. insert( conn, "a", 10 );
  47. */
  48.  
  49. int test[2];
  50. test[0] = 5;
  51. test[1] = 1;
  52. test[2] = 3;
  53.  
  54. int test2[2];
  55.  
  56. mongo::BSONObjBuilder obj;
  57. obj.append( "name" , "wut");
  58. obj.appendBinData( "binTest",sizeof(test), mongo::BinDataGeneral, test);
  59. conn.insert( "test.people" , obj.obj() );
  60.  
  61. {
  62. mongo::BSONObj query = BSON( "num" << BSON( "$nin" << BSON_ARRAY(1 << 5 << 10) ) );
  63. cout << query << endl;
  64.  
  65. auto_ptr<mongo::DBClientCursor> cursor = conn.query( "test.people" , query );
  66. cout << "using cursor" << endl;
  67. while ( cursor->more() ) {
  68. mongo::BSONObj obj = cursor->next();
  69. cout << "\t" << obj.jsonString() << endl;
  70. mongo::BSONElement array = obj["binTest"];
  71. //test = mongo::
  72. }
  73.  
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement