// finds the names of the document with the most fields in a collection std::set<string> getCollFields(DBClientConnection& c, string db, string collection){ int longest; // mongo fieldnames = sql column names set<string> fieldnames; // get the list of ODBC supported fields (columns) from collection.meta collection // collection.meta should only contain one document std::auto_ptr<mongo::DBClientCursor> cursor = c.query(db+"."+collection+".meta"); BSONObj d = cursor->next(); if (d.nFields() != 0){ longest = d.nFields(); d.getFieldNames(fieldnames); } // if no meta collection find collection with most fields if (longest == 0) { cursor = c.query(db+"."+collection); while( cursor->more() ) { // get next doc/row/tuple BSONObj doc = cursor->next(); if(longest < doc.nFields()){ longest = doc.nFields(); doc.getFieldNames(fieldnames); } } } return fieldnames; }
void run() { DBClientConnection c; c.connect("localhost"); cout << "connected" << endl; //insert BSONObj p = BSON( "name" << "ken" << "age" << 20 ); c.insert("test.persons", p); //query cout << "count: " << c.count("test.persons") << endl; { auto_ptr<DBClientCursor> cursor = c.query("test.persons", {}); while(cursor->more()) cout << cursor->next().toString() << endl; } { auto_ptr<DBClientCursor> cursor = c.query("test.persons", QUERY("age" << 20)); while(cursor->more()) cout << cursor->next().toString() << endl; } }
void FXtoBSON::updateDoc(const char &t, DBClientConnection &c){ switch(t){ case 'd': { BSONObj findHour = find(time0, 'h'); BSONObj findDay = find(time0, 'd'); auto_ptr<DBClientCursor> curH = c.query(dbH, findHour, 0, 0, &projId); if(curH->more()){ c.update(dbD, findDay, BSON("$setOnInsert" << emptyDoc('d')), true); c.update(dbD, findDay, BSON("$set" << BSON(to_string(time0.tm_hour) << curH->next().getField("_id")))); } } break; case 'm': { BSONObj findDay = find(time0, 'd'); BSONObj findMonth = find(time0, 'm'); auto_ptr<DBClientCursor> curD = c.query(dbD, findDay, 0, 0, &projId); if(curD->more()){ c.update(dbM, findMonth, BSON("$setOnInsert" << emptyDoc('m')), true); c.update(dbM, findMonth, BSON("$set" << BSON(to_string(time0.tm_mday) << curD->next().getField("_id")))); } } break; case 'y': { BSONObj findMonth = find(time0, 'm'); BSONObj findYear = find(time0, 'y'); auto_ptr<DBClientCursor> curM = c.query(dbM, findMonth, 0, 0, &projId); if(curM->more()){ c.update(dbY, findYear, BSON("$setOnInsert" << emptyDoc('y')), true); c.update(dbY, findYear, BSON("$set" << BSON(to_string(time0.tm_mon) << curM->next().getField("_id")))); } } break; } // switch end }
void plumage::stats::processMachineStats(ODSMongodbOps* ops, Date_t& ts) { dprintf(D_FULLDEBUG, "ODSCollectorPlugin::processMachineStats() called...\n"); DBClientConnection* conn = ops->m_db_conn; conn->ensureIndex(DB_RAW_ADS, BSON( ATTR_MY_TYPE << 1 )); auto_ptr<DBClientCursor> cursor = conn->query(DB_RAW_ADS, QUERY( ATTR_MY_TYPE << "Machine" ) ); conn->ensureIndex(DB_STATS_SAMPLES_MACH, BSON( "ts" << -1 )); conn->ensureIndex(DB_STATS_SAMPLES_MACH, BSON( "m" << 1 )); conn->ensureIndex(DB_STATS_SAMPLES_MACH, BSON( "n" << 1 )); while( cursor->more() ) { BSONObj p = cursor->next(); // write record to machine samples BSONObjBuilder bob; DATE(ts,ts); STRING(m,ATTR_MACHINE); STRING(n,ATTR_NAME); STRING(ar,ATTR_ARCH); STRING(os,ATTR_OPSYS); STRING(req,ATTR_REQUIREMENTS); INTEGER(ki,ATTR_KEYBOARD_IDLE); DOUBLE(la,ATTR_LOAD_AVG); STRING(st,ATTR_STATE); INTEGER(cpu,ATTR_CPUS); INTEGER(mem,ATTR_MEMORY); // TODO: these might be moved to another collection // STRING(gjid,ATTR_GLOBAL_JOB_ID); // STRING(ru,ATTR_REMOTE_USER); // STRING(ag,ATTR_ACCOUNTING_GROUP); conn->insert(DB_STATS_SAMPLES_MACH,bob.obj()); } }
void plumage::stats::processSchedulerStats(ODSMongodbOps* ops, Date_t& ts) { dprintf(D_FULLDEBUG, "ODSCollectorPlugin::processSchedulerStats() called...\n"); DBClientConnection* conn = ops->m_db_conn; conn->ensureIndex(DB_RAW_ADS, BSON( ATTR_MY_TYPE << 1 )); auto_ptr<DBClientCursor> cursor = conn->query(DB_RAW_ADS, QUERY( ATTR_MY_TYPE << "Scheduler" ) ); conn->ensureIndex(DB_STATS_SAMPLES_SCHED, BSON( "ts" << -1 )); conn->ensureIndex(DB_STATS_SAMPLES_SCHED, BSON( "n" << 1 )); while( cursor->more() ) { BSONObj p = cursor->next(); // write record to scheduler samples BSONObjBuilder bob; DATE(ts,ts); STRING(n,ATTR_NAME); INTEGER(mjr,ATTR_MAX_JOBS_RUNNING); INTEGER(nu,ATTR_NUM_USERS); INTEGER(tja,ATTR_TOTAL_JOB_ADS); INTEGER(trun,ATTR_TOTAL_RUNNING_JOBS); INTEGER(thj,ATTR_TOTAL_HELD_JOBS); INTEGER(tij,ATTR_TOTAL_IDLE_JOBS); INTEGER(trem,ATTR_TOTAL_REMOVED_JOBS); INTEGER(tsr,ATTR_TOTAL_SCHEDULER_RUNNING_JOBS); INTEGER(tsi,ATTR_TOTAL_SCHEDULER_IDLE_JOBS); INTEGER(tlr,ATTR_TOTAL_LOCAL_RUNNING_JOBS); INTEGER(tli,ATTR_TOTAL_LOCAL_IDLE_JOBS); INTEGER(tfj,ATTR_TOTAL_FLOCKED_JOBS); conn->insert(DB_STATS_SAMPLES_SCHED,bob.obj()); } }
void run() { DBClientConnection c; c.connect("localhost"); //"192.168.58.1"); cout << "connected ok" << endl; BSONObj p = BSON( "name" << "Joe" << "age" << 33 ); c.insert("tutorial.persons", p); p = BSON( "name" << "Jane" << "age" << 40 ); c.insert("tutorial.persons", p); p = BSON( "name" << "Abe" << "age" << 33 ); c.insert("tutorial.persons", p); p = BSON( "name" << "Samantha" << "age" << 21 << "city" << "Los Angeles" << "state" << "CA" ); c.insert("tutorial.persons", p); c.ensureIndex("tutorial.persons", fromjson("{age:1}")); cout << "count:" << c.count("tutorial.persons") << endl; auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", BSONObj()); while( cursor->more() ) { cout << cursor->next().toString() << endl; } cout << "\nprintifage:\n"; printIfAge(c, 33); }
int main( int argc, const char **argv ) { const char *port = "27017"; if ( argc != 1 ) { if ( argc != 3 ) throw -12; port = argv[ 2 ]; } DBClientConnection conn; string errmsg; if ( ! conn.connect( string( "127.0.0.1:" ) + port , errmsg ) ) { cout << "couldn't connect : " << errmsg << endl; throw -11; } const char * ns = "test.second"; conn.remove( ns , BSONObj() ); conn.insert( ns , BSON( "name" << "eliot" << "num" << 17 ) ); conn.insert( ns , BSON( "name" << "sara" << "num" << 24 ) ); auto_ptr<DBClientCursor> cursor = conn.query( ns , BSONObj() ); cout << "using cursor" << endl; while ( cursor->more() ) { BSONObj obj = cursor->next(); cout << "\t" << obj.jsonString() << endl; } conn.ensureIndex( ns , BSON( "name" << 1 << "num" << -1 ) ); }
void printIfAge(DBClientConnection& c, int age) { auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", QUERY( "age" << age ).sort("name") ); while( cursor->more() ) { BSONObj p = cursor->next(); cout << p.getStringField("name") << endl; } }
void em_mongodb::queryincrement(std::string dbcoll,BSONElement last) { // int ret = MDB_FAIL_QUERY; DBClientConnection* pconn = getConn(); if(!pconn) return ; mongo::Query cond = mongo::Query().sort("$natural"); // BSONElement last;// = minKey.firstElement(); while(1) { std::auto_ptr<mongo::DBClientCursor> cursor = pconn->query(dbcoll,cond,0,0,0,QueryOption_CursorTailable|QueryOption_AwaitData); while(1) { if(!cursor->more()) { if(cursor->isDead()) { break; } continue; } BSONObj obj = cursor->next(); last = obj["_id"]; //do something here... incrementfunc(obj); } // cond = mongo::Query("_id"<<BSON("$gt"<<last)).sort("$natural"); } boost::mutex::scoped_lock(m_iomux); m_connpool[pconn] = false; sem_post(&m_jobsem); }
std::auto_ptr<mongo::DBClientCursor> mongoSelect(DBClientConnection& c, string db, string collection, string fields, BSONObj whereCond){ // cout << "Hello mongoSelect" << endl; std::auto_ptr<mongo::DBClientCursor> cursor = c.query(db+"."+collection, whereCond); return cursor; }
int main( int argc, const char **argv ) { const char *port = "27017"; if ( argc != 1 ) { if ( argc != 3 ) throw -12; port = argv[ 2 ]; } DBClientConnection conn; string errmsg; if ( ! conn.connect( string( "127.0.0.1:" ) + port , errmsg ) ) { cout << "couldn't connect : " << errmsg << endl; throw -11; } const char * ns = "test.where"; conn.remove( ns , BSONObj() ); conn.insert( ns , BSON( "name" << "eliot" << "num" << 17 ) ); conn.insert( ns , BSON( "name" << "sara" << "num" << 24 ) ); auto_ptr<DBClientCursor> cursor = conn.query( ns , BSONObj() ); 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" )); cursor = conn.query( ns , q ); int num = 0; while ( cursor->more() ) { BSONObj obj = cursor->next(); cout << "\t" << obj.jsonString() << endl; num++; } MONGO_verify( num == 1 ); }
void printIfAge(DBClientConnection& c, string dbName, int age) { auto_ptr<DBClientCursor> cursor = c.query(dbName, QUERY( "age" << LT << age ).sort("age") ); int resultCounter = 0; while( cursor->more() ) { resultCounter++; BSONObj p = cursor->next(); ofLogVerbose(MongoModel::logName) << "resultCounter: " << resultCounter << " name: " << p["name"] << " age: " << p["age"]; } }
void dumpCollection(Parameters& params) { DBClientConnection c; string hostPort(params.getHost()); if (hostPort.find(':') == string::npos) { hostPort += ":"; hostPort += to_string(params.getPort()); } c.connect(hostPort); time_t t; time(&t); int documentCount = c.count(params.getDbCollection()); if (params.isDebug()) { cout << "{ " << params.getDbCollection() << ".count: " << documentCount << " }\n"; } string docPrefixString(params.getDbCollection()); // docIndex += "{"; // docIndex += to_string(i++); // docIndex += "}"; unique_ptr<DBClientCursor> cursor = c.query(params.getDbCollection(), BSONObj()); unique_ptr<IBSONRenderer> renderer; switch (params.getStyle()) { case STYLE_DOTTED: renderer = unique_ptr<IBSONRenderer>(new BSONDotNotationDump(params, docPrefixString)); break; case STYLE_TREE: renderer = unique_ptr<IBSONRenderer>(new BSONObjectTypeDump(params, docPrefixString)); break; case STYLE_JSON: case STYLE_JSONPACKED: renderer = unique_ptr<IBSONRenderer>(new JSONDump(params, " ")); break; default: throw std::logic_error("ISE: Undefined STYLE!"); break; } if (renderer) { renderer->setOutputStream(cout); renderer->begin(NULL); int documentIndex = 0; while (cursor->more()) { const BSONObj& o = cursor->next(); // Get the BSON Object renderer->render(o, documentIndex++, documentCount); } renderer->end(NULL); } else { throw std::logic_error("ISE: Undefined renderer!"); } }
void FXtoBSON::addMinToDB(const BSONObj & document, DBClientConnection & c){ BSONObj FINDhour = find(time1, 'h'); auto_ptr<DBClientCursor> cursor = c.query(dbH, FINDhour); if(cursor->more()){ c.update(dbH , FINDhour, BSON("$set" << document)); } else { c.update(dbH, FINDhour, BSON("$set" << emptyDoc('h')), true); c.update(dbH, FINDhour, BSON("$set" << document)); } }
void outputToFile(DBClientConnection& c){ ofstream signal; ofstream noise; signal.open("signal.txt"); noise.open("noise.txt"); auto_ptr<DBClientCursor> noiseCursor =c.query("hw3.noise", Query()); auto_ptr<DBClientCursor> signalCursor =c.query("hw3.signal", Query()); //output to file while (noiseCursor->more()){ BSONObj single = noiseCursor->next(); noise<<single.getStringField("date")<<","<<single.getField("value").Double() <<","<<single.getField("volume").Int()<<'\n'; } while (signalCursor->more()){ BSONObj single = signalCursor->next(); signal<<single.getStringField("date")<<","<<single.getField("value").Double() <<","<<single.getField("volume").Int()<<'\n'; } }
void scrub(DBClientConnection& c){ //DES: finds and moves noise documents found in the database //IN: connection to the mongo database auto_ptr<DBClientCursor> noiseCursor[6]; //look for negative volume noiseCursor[0] = c.query("hw3.signal", BSON("volume"<<LTE<<0.0)); //find excessive price (>5 dollars) noiseCursor[1] = c.query("hw3.signal", BSON("value"<<GTE<<5.0)); //find excessive negative price (<-5 dollars) noiseCursor[2] = c.query("hw3.signal", BSON("value"<<LTE<<-5.0)); //find weekend dates noiseCursor[3] = c.query("hw3.signal", Query("{date: /[0-9]{7}2./i }")); //find 9 am trades noiseCursor[4] = c.query("hw3.signal", Query("{date: /[0-9]{8}:09./i }")); //find 5 pm trades noiseCursor[5] = c.query("hw3.signal", Query("{date: /[0-9]{8}:17./i }")); //loop through each noise type and move the errors from the signal collection to the //noise collection for (int i=0;i<6;i++){ while (noiseCursor[i]->more()){ BSONObj singleNoise = noiseCursor[i]->next(); //add to noise db c.insert("hw3.noise", singleNoise); //remove from signal db c.remove("hw3.signal",singleNoise); } } }
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; }
/*执行写一个打印名称的函数(惟一的)的所有students在收集他们的年龄是一个给定的值*/ int printIfXuehao(DBClientConnection& c, int xuehao) { std::auto_ptr<DBClientCursor> cursor = c.query("mydb.testData", MONGO_QUERY( "xuehao" << xuehao ).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 dealCheck(string cmd, DBClientConnection& conn) { int index = 6; BSONObj p; if(cmd[index] == ' ') index ++; //cout << cmd << endl; string code = cmd.substr(index, 9); //cout << code << endl; auto_ptr<DBClientCursor> cursor = conn.query("HXTBBH.coupon",BSON("code" << code)); if(cursor->more()) p = cursor->next(); cout << p.getStringField("status") << endl; }
int main() { // BSONObj p = BSONObjBuilder().genOID().append("name","Joe").append("age",33).obj(); DBClientConnection c; c.connect("127.0.0.1"); cout << "count:" << c.count("test.uefa") << "\n"; std::auto_ptr<DBClientCursor> cursor = c.query("test.uefa", BSONObj()); while (cursor->more()) cout << cursor->next().toString() << "\n"; // string mongo::DBClientWithCommands::getLastError(); // Empty string if no error // cout << return 0; }
string GetPaymentJson(string id) { auto_ptr<DBClientCursor> cursor = db.query(PAYMENTS_COLLECTION_NAMESPASE, MONGO_QUERY("_id" << OID(id))); if (cursor->more()) { BSONObj payment = cursor->next(); string paymentJson = payment.jsonString(); return paymentJson; //Json::Value paymentJson; //Json::Reader reader; //reader.parse(payment.jsonString(), paymentJson); //return video.toStyledString(); } return ""; }
int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); client::initialize(); try { DBClientConnection c; c.connect("localhost"); std::cout << "connected ok" << std::endl; c.remove("deuda.bonosletras", BSONObj()); std::cout << "reg. in deuda.bonosletras before=" << c.count("deuda.bonosletras") << std::endl; for (int i = 0; i < 100000; ++i) { BSONObj p = BSONObjBuilder().append("name", "Joe").append("age", i).obj(); c.insert("deuda.bonosletras", p); } std::cout << "reg. in deuda.bonosletras after =" << c.count("deuda.bonosletras") << std::endl; std::auto_ptr<DBClientCursor> cursor = c.query("deuda.bonosletras", QUERY("age" << 100)); while (cursor->more()) { BSONObj p = cursor->next(); std::cout << p.getStringField("name") << std::endl; } } catch( const DBException &e ) { std::cout << "caught " << e.what() << std::endl; } return EXIT_SUCCESS; return a.exec(); }
int em_mongodb::query(std::string dbcoll,mongo::Query cond,std::vector<BSONObj>& out,int count) { int ret = 0; DBClientConnection* pconn = getConn(); if(!pconn) return ret; auto_ptr<DBClientCursor> cursor = pconn->query(dbcoll,cond,count); // std::cout<<"result for query,return BSONObjs"<<std::endl; while(cursor->more()) { out.push_back(cursor->next()); ret++; } boost::mutex::scoped_lock lock(m_iomux); m_connpool[pconn] = false; sem_post(&m_jobsem); return ret; }
time_t MongoGliderDataExporter::getLastTime(){ DBClientConnection* timeConn = new DBClientConnection(); string errMsg; try{ timeConn->connect(hostString,errMsg); } catch (DBException &e){ fprintf(stderr,"Database connection exception: %s. Message: %s\r\n",e.what(),errMsg.c_str()); } Query qu = BSONObj(); auto_ptr<DBClientCursor> cursor = timeConn->query("GDAM."+gliderName+".processed_files",qu.sort("modify_time",-1)); time_t retVal = 0; while(cursor->more()){ BSONObj p = cursor->next(); retVal = p.getField("modify_time").Date().toTimeT(); break; // Only want first result } delete timeConn; return retVal; }
void* run(void *args) { int cnt = *(static_cast<int*>(args)); int *found = new int; *found = 0; DBClientConnection c; c.connect("localhost"); for(int i = 0; i < cnt; i++) { int id = get_rand_value(); auto_ptr<DBClientCursor> cursor = c.query(USER_CATE_COLLECTION, QUERY("id" << id)); if(cursor->itcount() > 0) ++(*found); } pthread_exit(found); return NULL; }
/** 0 - namespace 1 - query 2 - fields 3 - limit 4 - skip */ Handle<Value> mongoFind(const Arguments& args){ jsassert( args.Length() == 5 , "find needs 5 args" ); jsassert( args[1]->IsObject() , "needs to be an object" ); DBClientConnection * conn = getConnection( args ); GETNS; BSONObj q = v8ToMongo( args[1]->ToObject() ); DDD( "query:" << q ); BSONObj fields; bool haveFields = args[2]->IsObject() && args[2]->ToObject()->GetPropertyNames()->Length() > 0; if ( haveFields ) fields = v8ToMongo( args[2]->ToObject() ); Local<v8::Object> mongo = args.This(); Local<v8::Value> slaveOkVal = mongo->Get( String::New( "slaveOk" ) ); jsassert( slaveOkVal->IsBoolean(), "slaveOk member invalid" ); bool slaveOk = slaveOkVal->BooleanValue(); try { auto_ptr<mongo::DBClientCursor> cursor; int nToReturn = (int)(args[3]->ToNumber()->Value()); int nToSkip = (int)(args[4]->ToNumber()->Value()); { v8::Unlocker u; cursor = conn->query( ns, q , nToReturn , nToSkip , haveFields ? &fields : 0, slaveOk ? Option_SlaveOk : 0 ); } v8::Function * cons = (v8::Function*)( *( mongo->Get( String::New( "internalCursor" ) ) ) ); Local<v8::Object> c = cons->NewInstance(); // NOTE I don't believe the cursor object will ever be freed. c->Set( v8::String::New( "cursor" ) , External::New( cursor.release() ) ); return c; } catch ( ... ){ return v8::ThrowException( v8::String::New( "socket error on query" ) ); } }
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; BSONObj p = BSON( "name" << "Joe" << "age" << 33 ); c.insert("tutorial.persons", p); p = BSON( "name" << "Jane" << "age" << 40 ); c.insert("tutorial.persons", p); p = BSON( "name" << "Abe" << "age" << 33 ); c.insert("tutorial.persons", p); p = BSON( "name" << "Methuselah" << "age" << BSONNULL); c.insert("tutorial.persons", p); p = BSON( "name" << "Samantha" << "age" << 21 << "city" << "Los Angeles" << "state" << "CA" ); c.insert("tutorial.persons", p); c.ensureIndex("tutorial.persons", fromjson("{age:1}")); cout << "count:" << c.count("tutorial.persons") << endl; 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"; return printIfAge(c, 33); }
void simCount(DBClientConnection& c, string dbcoll, BSONObj bobject){ cout<<"\n simCount \n"<< endl; // BSONObj cx = BSONObjBuilder().append(fromjson("{age:12}")).obj(); mongo::BSONObj cx = fromjson("{age: {$gt : 12, $lt : 40}}"); mongo::BSONObj dx = fromjson("{age: {$gt : 12}, age:{$lt : 40}}"); mongo::Query query = QUERY("age"<< NE << 12); auto_ptr<mongo::DBClientCursor> cursor = c.query(dbcoll,dx); set<string> fieldnames; while( cursor->more() ) { BSONObj p = cursor->next(); cout<< p.nFields() <<endl; cout << p.getField("name").toString() << endl; cout << p.getFieldNames(fieldnames) << endl; cout << fieldnames.size() << endl; } }
void plumage::stats::processSubmitterStats(ODSMongodbOps* ops, Date_t& ts) { dprintf(D_FULLDEBUG, "ODSCollectorPlugin::processSubmitterStats called...\n"); DBClientConnection* conn = ops->m_db_conn; conn->ensureIndex(DB_RAW_ADS, BSON( ATTR_MY_TYPE << 1 )); auto_ptr<DBClientCursor> cursor = conn->query(DB_RAW_ADS, QUERY( ATTR_MY_TYPE << "Submitter" ) ); conn->ensureIndex(DB_STATS_SAMPLES_SUB, BSON( "ts" << -1 )); conn->ensureIndex(DB_STATS_SAMPLES_SUB, BSON( "sn" << 1 )); while( cursor->more() ) { BSONObj p = cursor->next(); // write record to submitter samples BSONObjBuilder bob; DATE(ts,ts); STRING(sn,ATTR_NAME); STRING(ma,ATTR_MACHINE); INTEGER(jr,ATTR_RUNNING_JOBS); // TODO: weird...HeldJobs isn't always there in the raw submitter ad int h = p.getIntField(ATTR_HELD_JOBS); h = (h>0) ? h : 0; bob.append("jh",h); INTEGER(ji,ATTR_IDLE_JOBS); conn->insert(DB_STATS_SAMPLES_SUB,bob.obj()); } }
int main( int argc, const char **argv ) { const char *port = "27017"; if ( argc != 1 ) { if ( argc != 3 ) throw -12; port = argv[ 2 ]; } DBClientConnection conn; string errmsg; if ( ! conn.connect( string( "127.0.0.1:" ) + port , errmsg ) ) { cout << "couldn't connect : " << errmsg << endl; throw -11; } const char * ns = "test.test1"; conn.dropCollection(ns); // clean up old data from any previous tests conn.remove( ns, BSONObj() ); assert( conn.findOne( ns , BSONObj() ).isEmpty() ); // test insert conn.insert( ns ,BSON( "name" << "eliot" << "num" << 1 ) ); assert( ! conn.findOne( ns , BSONObj() ).isEmpty() ); // test remove conn.remove( ns, BSONObj() ); assert( conn.findOne( ns , BSONObj() ).isEmpty() ); // insert, findOne testing conn.insert( ns , BSON( "name" << "eliot" << "num" << 1 ) ); { BSONObj res = conn.findOne( ns , BSONObj() ); assert( strstr( res.getStringField( "name" ) , "eliot" ) ); assert( ! strstr( res.getStringField( "name2" ) , "eliot" ) ); assert( 1 == res.getIntField( "num" ) ); } // cursor conn.insert( ns ,BSON( "name" << "sara" << "num" << 2 ) ); { auto_ptr<DBClientCursor> cursor = conn.query( ns , BSONObj() ); int count = 0; while ( cursor->more() ) { count++; BSONObj obj = cursor->next(); } assert( count == 2 ); } { auto_ptr<DBClientCursor> cursor = conn.query( ns , BSON( "num" << 1 ) ); int count = 0; while ( cursor->more() ) { count++; BSONObj obj = cursor->next(); } assert( count == 1 ); } { auto_ptr<DBClientCursor> cursor = conn.query( ns , BSON( "num" << 3 ) ); int count = 0; while ( cursor->more() ) { count++; BSONObj obj = cursor->next(); } assert( count == 0 ); } // update { BSONObj res = conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot" ).obj() ); assert( ! strstr( res.getStringField( "name2" ) , "eliot" ) ); BSONObj after = BSONObjBuilder().appendElements( res ).append( "name2" , "h" ).obj(); conn.update( ns , BSONObjBuilder().append( "name" , "eliot2" ).obj() , after ); res = conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot" ).obj() ); assert( ! strstr( res.getStringField( "name2" ) , "eliot" ) ); assert( conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot2" ).obj() ).isEmpty() ); conn.update( ns , BSONObjBuilder().append( "name" , "eliot" ).obj() , after ); res = conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot" ).obj() ); assert( strstr( res.getStringField( "name" ) , "eliot" ) ); assert( strstr( res.getStringField( "name2" ) , "h" ) ); assert( conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot2" ).obj() ).isEmpty() ); // upsert conn.update( ns , BSONObjBuilder().append( "name" , "eliot2" ).obj() , after , 1 ); assert( ! conn.findOne( ns , BSONObjBuilder().append( "name" , "eliot" ).obj() ).isEmpty() ); } { // ensure index assert( conn.ensureIndex( ns , BSON( "name" << 1 ) ) ); assert( ! conn.ensureIndex( ns , BSON( "name" << 1 ) ) ); } { // hint related tests assert( conn.findOne(ns, "{}")["name"].str() == "sara" ); assert( conn.findOne(ns, "{ name : 'eliot' }")["name"].str() == "eliot" ); assert( conn.getLastError() == "" ); // nonexistent index test bool asserted = false; try { conn.findOne(ns, Query("{name:\"eliot\"}").hint("{foo:1}")); } catch ( ... ){ asserted = true; } assert( asserted ); //existing index assert( conn.findOne(ns, Query("{name:'eliot'}").hint("{name:1}")).hasElement("name") ); // run validate assert( conn.validate( ns ) ); } { // timestamp test const char * tsns = "test.tstest1"; conn.dropCollection( tsns ); { mongo::BSONObjBuilder b; b.appendTimestamp( "ts" ); conn.insert( tsns , b.obj() ); } mongo::BSONObj out = conn.findOne( tsns , mongo::BSONObj() ); Date_t oldTime = out["ts"].timestampTime(); unsigned int oldInc = out["ts"].timestampInc(); { mongo::BSONObjBuilder b1; b1.append( out["_id"] ); mongo::BSONObjBuilder b2; b2.append( out["_id"] ); b2.appendTimestamp( "ts" ); conn.update( tsns , b1.obj() , b2.obj() ); } BSONObj found = conn.findOne( tsns , mongo::BSONObj() ); cout << "old: " << out << "\nnew: " << found << endl; assert( ( oldTime < found["ts"].timestampTime() ) || ( oldTime == found["ts"].timestampTime() && oldInc < found["ts"].timestampInc() ) ); } { // check that killcursors doesn't affect last error assert( conn.getLastError().empty() ); BufBuilder b; b.appendNum( (int)0 ); // reserved b.appendNum( (int)-1 ); // invalid # of cursors triggers exception b.appendNum( (int)-1 ); // bogus cursor id Message m; m.setData( dbKillCursors, b.buf(), b.len() ); // say() is protected in DBClientConnection, so get superclass static_cast< DBConnector* >( &conn )->say( m ); assert( conn.getLastError().empty() ); } { list<string> l = conn.getDatabaseNames(); for ( list<string>::iterator i = l.begin(); i != l.end(); i++ ){ cout << "db name : " << *i << endl; } l = conn.getCollectionNames( "test" ); for ( list<string>::iterator i = l.begin(); i != l.end(); i++ ){ cout << "coll name : " << *i << endl; } } cout << "client test finished!" << endl; }