本文整理汇总了C++中DBClientConnection::query方法的典型用法代码示例。如果您正苦于以下问题:C++ DBClientConnection::query方法的具体用法?C++ DBClientConnection::query怎么用?C++ DBClientConnection::query使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DBClientConnection
的用法示例。
在下文中一共展示了DBClientConnection::query方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: getCollFields
// 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;
}
示例2: run
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;
}
}
示例3: updateDoc
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
}
示例4: dprintf
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());
}
}
示例5: run
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);
}
示例6: main
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 ) );
}
示例7: printIfAge
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;
}
}
示例8: queryincrement
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);
}
示例9: mongoSelect
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;
}
示例10: main
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 );
}
示例11: dumpCollection
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!");
}
}
示例12: addMinToDB
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));
}
}
示例13: outputToFile
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';
}
}
示例14: scrub
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);
}
}
}
示例15: printIfAge
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;
}