本文整理汇总了C++中DBClientConnection::eval方法的典型用法代码示例。如果您正苦于以下问题:C++ DBClientConnection::eval方法的具体用法?C++ DBClientConnection::eval怎么用?C++ DBClientConnection::eval使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DBClientConnection
的用法示例。
在下文中一共展示了DBClientConnection::eval方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: testDbEval
/* TODO: unit tests should run this? */
void testDbEval() {
DBClientConnection c;
string err;
if ( !c.connect("localhost", err) ) {
out() << "can't connect to server " << err << endl;
return;
}
if( !c.auth("dwight", "u", "p", err) ) {
out() << "can't authenticate " << err << endl;
return;
}
BSONObj info;
BSONElement retValue;
BSONObjBuilder b;
b.append("0", 99);
BSONObj args = b.done();
bool ok = c.eval("dwight", "function() { return args[0]; }", info, retValue, &args);
out() << "eval ok=" << ok << endl;
out() << "retvalue=" << retValue.toString() << endl;
out() << "info=" << info.toString() << endl;
out() << endl;
int x = 3;
assert( c.eval("dwight", "function() { return 3; }", x) );
out() << "***\n";
BSONObj foo = fromjson("{\"x\":7}");
out() << foo.toString() << endl;
int res=0;
ok = c.eval("dwight", "function(parm1) { return parm1.x; }", foo, res);
out() << ok << " retval:" << res << endl;
}
示例2: main
//.........这里部分代码省略.........
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;
verify( ( oldTime < found["ts"].timestampTime() ) ||
( oldTime == found["ts"].timestampTime() && oldInc < found["ts"].timestampInc() ) );
}
{
// check that killcursors doesn't affect last error
verify( 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 );
verify( 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;
}
}
{
//Map Reduce (this mostly just tests that it compiles with all output types)
const string ns = "test.mr";
conn.insert(ns, BSON("a" << 1));
conn.insert(ns, BSON("a" << 1));
const char* map = "function() { emit(this.a, 1); }";
const char* reduce = "function(key, values) { return Array.sum(values); }";
const string outcoll = ns + ".out";
BSONObj out;
out = conn.mapreduce(ns, map, reduce, BSONObj()); // default to inline
//MONGO_PRINT(out);
out = conn.mapreduce(ns, map, reduce, BSONObj(), outcoll);
//MONGO_PRINT(out);
out = conn.mapreduce(ns, map, reduce, BSONObj(), outcoll.c_str());
//MONGO_PRINT(out);
out = conn.mapreduce(ns, map, reduce, BSONObj(), BSON("reduce" << outcoll));
//MONGO_PRINT(out);
}
{
// test timeouts
DBClientConnection conn( true , 0 , 2 );
if ( ! conn.connect( string( "127.0.0.1:" ) + port , errmsg ) ) {
cout << "couldn't connect : " << errmsg << endl;
throw -11;
}
conn.insert( "test.totest" , BSON( "x" << 1 ) );
BSONObj res;
bool gotError = false;
verify( conn.eval( "test" , "return db.totest.findOne().x" , res ) );
try {
conn.eval( "test" , "sleep(5000); return db.totest.findOne().x" , res );
}
catch ( std::exception& e ) {
gotError = true;
log() << e.what() << endl;
}
verify( gotError );
// sleep so the server isn't locked anymore
sleepsecs( 4 );
verify( conn.eval( "test" , "return db.totest.findOne().x" , res ) );
}
cout << "client test finished!" << endl;
return EXIT_SUCCESS;
}