本文整理汇总了C++中BSONObj::jsonString方法的典型用法代码示例。如果您正苦于以下问题:C++ BSONObj::jsonString方法的具体用法?C++ BSONObj::jsonString怎么用?C++ BSONObj::jsonString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BSONObj
的用法示例。
在下文中一共展示了BSONObj::jsonString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: 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 ) );
}
示例2: buildAnIndex
// throws DBException
void buildAnIndex(const std::string& ns,
NamespaceDetails* d,
IndexDetails& idx,
bool mayInterrupt) {
BSONObj idxInfo = idx.info.obj();
MONGO_TLOG(0) << "build index on: " << ns << " properties: " << idxInfo.jsonString() << endl;
Timer t;
unsigned long long n;
verify( Lock::isWriteLocked(ns) );
if( inDBRepair || !idxInfo["background"].trueValue() ) {
int idxNo = IndexBuildsInProgress::get(ns.c_str(), idx.info.obj()["name"].valuestr());
n = BtreeBasedBuilder::fastBuildIndex(ns.c_str(), d, idx, mayInterrupt, idxNo);
verify( !idx.head.isNull() );
}
else {
BackgroundIndexBuildJob j(ns.c_str());
n = j.go(ns, d, idx);
}
MONGO_TLOG(0) << "build index done. scanned " << n << " total records. " << t.millis() / 1000.0 << " secs" << endl;
}
示例3: findOne
BSONObj SyncClusterConnection::findOne(const string &ns, Query query, const BSONObj *fieldsToReturn, int queryOptions) {
if ( ns.find( ".$cmd" ) != string::npos ){
string cmdName = query.obj.firstElement().fieldName();
int lockType = _lockType( cmdName );
if ( lockType > 0 ){ // write $cmd
string errmsg;
if ( ! prepare( errmsg ) )
throw UserException( 13104 , (string)"SyncClusterConnection::insert prepare failed: " + errmsg );
vector<BSONObj> all;
for ( size_t i=0; i<_conns.size(); i++ ){
all.push_back( _conns[i]->findOne( ns , query , 0 , queryOptions ).getOwned() );
}
_checkLast();
for ( size_t i=0; i<all.size(); i++ ){
BSONObj temp = all[i];
if ( isOk( temp ) )
continue;
stringstream ss;
ss << "write $cmd failed on a shard: " << temp.jsonString();
ss << " " << _conns[i]->toString();
throw UserException( 13105 , ss.str() );
}
return all[0];
}
}
return DBClientBase::findOne( ns , query , fieldsToReturn , queryOptions );
}
示例4: buildAnIndex
// throws DBException
void buildAnIndex( Collection* collection,
IndexDetails& idx,
bool mayInterrupt ) {
string ns = collection->ns().ns(); // our copy
BSONObj idxInfo = idx.info.obj();
MONGO_TLOG(0) << "build index on: " << ns
<< " properties: " << idxInfo.jsonString() << endl;
audit::logCreateIndex( currentClient.get(), &idxInfo, idx.indexName(), ns );
Timer t;
unsigned long long n;
verify( Lock::isWriteLocked( ns ) );
if( inDBRepair || !idxInfo["background"].trueValue() ) {
int idxNo = collection->details()->findIndexByName( idx.info.obj()["name"].valuestr(),
true );
verify( idxNo >= 0 );
n = BtreeBasedBuilder::fastBuildIndex( ns.c_str(), collection->details(),
idx, mayInterrupt, idxNo );
verify( !idx.head.isNull() );
}
else {
BackgroundIndexBuildJob j( ns );
n = j.go( collection, idx );
}
MONGO_TLOG(0) << "build index done. scanned " << n << " total records. " << t.millis() / 1000.0 << " secs" << endl;
}
示例5: mongoInsert
v8::Handle<v8::Value> mongoInsert(const v8::Arguments& args){
jsassert( args.Length() == 2 , "insert needs 2 args" );
jsassert( args[1]->IsObject() , "have to insert an object" );
DBClientConnection * conn = getConnection( args );
GETNS;
v8::Handle<v8::Object> in = args[1]->ToObject();
if ( ! in->Has( String::New( "_id" ) ) ){
v8::Handle<v8::Value> argv[1];
in->Set( String::New( "_id" ) , getObjectIdCons()->NewInstance( 0 , argv ) );
}
BSONObj o = v8ToMongo( in );
DDD( "want to save : " << o.jsonString() );
try {
conn->insert( ns , o );
}
catch ( ... ){
return v8::ThrowException( v8::String::New( "socket error on insert" ) );
}
return args[1];
}
示例6: insert
void SyncClusterConnection::insert( const string &ns, const vector< BSONObj >& v , int flags) {
if (v.size() == 1){
insert(ns, v[0], flags);
return;
}
for (vector<BSONObj>::const_iterator it = v.begin(); it != v.end(); ++it ) {
BSONObj obj = *it;
if ( obj["_id"].type() == EOO ) {
string assertMsg = "SyncClusterConnection::insert (batched) obj misses an _id: ";
uasserted( 16743, assertMsg + obj.jsonString() );
}
}
// fsync all connections before starting the batch.
string errmsg;
if ( ! prepare( errmsg ) ) {
string assertMsg = "SyncClusterConnection::insert (batched) prepare failed: ";
throw UserException( 16744, assertMsg + errmsg );
}
// We still want one getlasterror per document, even if they're batched.
for ( size_t i=0; i<_conns.size(); i++ ) {
for ( vector<BSONObj>::const_iterator it = v.begin(); it != v.end(); ++it ) {
_conns[i]->insert( ns, *it, flags );
_conns[i]->getLastErrorDetailed();
}
}
// We issue a final getlasterror, but this time with an fsync.
_checkLast();
}
示例7: test_second
inline int test_second(){
boost::scoped_ptr<DBClientBase> conn(mongo_wrapper::instance().connect());
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));
std::auto_ptr<DBClientCursor> cursor = conn->query(ns, BSONObj());
if (!cursor.get()) {
cout << "query failure" << endl;
return EXIT_FAILURE;
}
cout << "using cursor" << endl;
while (cursor->more()) {
BSONObj obj = cursor->next();
cout << "\t" << obj.jsonString() << endl;
}
conn->createIndex(ns, BSON("name" << 1 << "num" << -1));
///////////////////////////////////////////////
cout << "now using $where" << endl;
Query q = Query("{}").where("this.name == name", BSON("name" << "sara"));
cursor = conn->query(ns, q);
if (!cursor.get()) {
cout << "query failure" << endl;
return EXIT_FAILURE;
}
int num = 0;
while (cursor->more()) {
BSONObj obj = cursor->next();
cout << "\t" << obj.jsonString() << endl;
num++;
}
verify(num == 1);
return EXIT_SUCCESS;
}
示例8: 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 );
}
示例9: insertGeoData
void insertGeoData(DBClientBase* conn) {
Point p1(BSON("type" << "Point" << "coordinates" << BSON_ARRAY(-5.0 << -5.0)));
Point p2(BSON("type" << "Point" << "coordinates" << BSON_ARRAY(100.0 << 0.0)));
Point p3(BSON("type" << "Point" << "coordinates" << BSON_ARRAY(20.0 << 30.0)));
Point p4(BSON("type" << "Point" << "coordinates" << BSON_ARRAY(50.0 << 50.0)));
cout << p4.toBSON().jsonString() << endl;
BSONObj lineBson = BSON("type" << "LineString" << "coordinates" <<
BSON_ARRAY(BSON_ARRAY(0.0 << 10.0) << BSON_ARRAY(100.0 << 10.0)));
LineString line(lineBson);
std::vector<LineString> lineStrings;
lineStrings.push_back(line);
lineStrings.push_back(line);
MultiLineString mls(lineStrings);
geo::coords2dgeographic::Polygon poly(lineStrings);
BSONObj mpBson = BSON("type" << "MultiPolygon" << "coordinates" << BSON_ARRAY(
BSON_ARRAY(
BSON_ARRAY(
BSON_ARRAY(0.0 << 10.0) <<
BSON_ARRAY(100.0 << 10.0) <<
BSON_ARRAY(5.0 << 5.0) <<
BSON_ARRAY(0.0 << 10.0)))));
cout << "MULTIPOLYGON BSON:" << endl;
cout << mpBson.jsonString() << endl;
MultiPolygon mp(mpBson);
cout << mp.toBSON().jsonString() << endl;
BSONObj gcolBson = BSON("type" << "GeometryCollection" << "geometries" <<
BSON_ARRAY(p1.toBSON() << p2.toBSON() << line.toBSON()));
GeometryCollection gcol(gcolBson);
cout << "GEO COLLECTION BSON:" << endl;
cout << gcol.toBSON() << endl;
const vector<const GeoObj*>& geoms = gcol.getGeometries();
for (size_t i = 0; i < geoms.size(); ++i) {
cout << geoms[i]->getType() << endl;
}
conn->insert(kDbCollectionName, BSON(kLocField << p1.toBSON()));
conn->insert(kDbCollectionName, BSON(kLocField << p2.toBSON()));
conn->insert(kDbCollectionName, BSON(kLocField << p3.toBSON()));
conn->insert(kDbCollectionName, BSON(kLocField << p4.toBSON()));
conn->insert(kDbCollectionName, BSON(kLocField << line.toBSON()));
conn->insert(kDbCollectionName, BSON(kLocField << mls.toBSON()));
conn->insert(kDbCollectionName, BSON(kLocField << mp.toBSON()));
conn->insert(kDbCollectionName, BSON(kLocField << gcol.toBSON()));
conn->createIndex(kDbCollectionName, fromjson("{loc:\"2dsphere\"}"));
cout << "Coordinates p1 toBSON().toString():" << endl;
cout << p1.getCoordinates().toBSON().toString() << endl;
cout << "MultiLineString mls toBSON().jsonString():" << endl;
cout << mls.toBSON().jsonString() << endl << endl;
}
示例10: checkShardVersion
void checkShardVersion( DBClientBase& conn , const string& ns , bool authoritative ){
// TODO: cache, optimize, etc...
WriteBackListener::init( conn );
DBConfigPtr conf = grid.getDBConfig( ns );
if ( ! conf )
return;
ShardChunkVersion version = 0;
unsigned long long officialSequenceNumber = 0;
ChunkManagerPtr manager;
const bool isSharded = conf->isSharded( ns );
if ( isSharded ){
manager = conf->getChunkManager( ns , authoritative );
officialSequenceNumber = manager->getSequenceNumber();
}
unsigned long long & sequenceNumber = checkShardVersionLastSequence[ make_pair(&conn,ns) ];
if ( sequenceNumber == officialSequenceNumber )
return;
if ( isSharded ){
version = manager->getVersion( Shard::make( conn.getServerAddress() ) );
}
log(2) << " have to set shard version for conn: " << &conn << " ns:" << ns
<< " my last seq: " << sequenceNumber << " current: " << officialSequenceNumber
<< " version: " << version << " manager: " << manager.get()
<< endl;
BSONObj result;
if ( setShardVersion( conn , ns , version , authoritative , result ) ){
// success!
log(1) << " setShardVersion success!" << endl;
sequenceNumber = officialSequenceNumber;
dassert( sequenceNumber == checkShardVersionLastSequence[ make_pair(&conn,ns) ] );
return;
}
log(1) << " setShardVersion failed!\n" << result << endl;
if ( result.getBoolField( "need_authoritative" ) )
massert( 10428 , "need_authoritative set but in authoritative mode already" , ! authoritative );
if ( ! authoritative ){
checkShardVersion( conn , ns , 1 );
return;
}
log() << " setShardVersion failed: " << result << endl;
massert( 10429 , (string)"setShardVersion failed! " + result.jsonString() , 0 );
}
示例11: run
void run() {
ASSERT( db.createCollection(ns()) );
BSONObjBuilder cmd;
cmd.appendSymbol("dropIndexes", nsColl()); // Use Symbol for SERVER-16260
cmd.append("index", "*");
BSONObj result;
bool ok = db.runCommand(nsDb(), cmd.obj(), result);
log() << result.jsonString();
ASSERT(ok);
}
示例12: gotObject
virtual void gotObject( const BSONObj& o ) {
switch ( _type ) {
case JSON:
cout << o.jsonString( TenGen ) << endl;
break;
case DEBUG:
debug(o);
break;
default:
cerr << "bad type? : " << _type << endl;
}
}
示例13: createCollectionWithOptions
void createCollectionWithOptions(BSONObj cmdObj) {
// Create a new cmdObj to skip undefined fields and fix collection name
BSONObjBuilder bo;
// Add a "create" field if it doesn't exist
if (!cmdObj.hasField("create")) {
bo.append("create", _curcoll);
}
BSONObjIterator i(cmdObj);
while ( i.more() ) {
BSONElement e = i.next();
// Replace the "create" field with the name of the collection we are actually creating
if (strcmp(e.fieldName(), "create") == 0) {
bo.append("create", _curcoll);
}
else {
if (e.type() == Undefined) {
log() << _curns << ": skipping undefined field: " << e.fieldName() << endl;
}
else {
bo.append(e);
}
}
}
cmdObj = bo.obj();
BSONObj fields = BSON("options" << 1);
scoped_ptr<DBClientCursor> cursor(conn().query(_curdb + ".system.namespaces", Query(BSON("name" << _curns)), 0, 0, &fields));
bool createColl = true;
if (cursor->more()) {
createColl = false;
BSONObj obj = cursor->next();
if (!obj.hasField("options") || !optionsSame(cmdObj, obj["options"].Obj())) {
log() << "WARNING: collection " << _curns << " exists with different options than are in the metadata.json file and not using --drop. Options in the metadata file will be ignored." << endl;
}
}
if (!createColl) {
return;
}
BSONObj info;
if (!conn().runCommand(_curdb, cmdObj, info)) {
uasserted(15936, "Creating collection " + _curns + " failed. Errmsg: " + info["errmsg"].String());
} else {
log() << "\tCreated collection " << _curns << " with options: " << cmdObj.jsonString() << endl;
}
}
示例14: createCollectionWithOptions
void createCollectionWithOptions(BSONObj obj) {
BSONObjIterator i(obj);
// Rebuild obj as a command object for the "create" command.
// - {create: <name>} comes first, where <name> is the new name for the collection
// - elements with type Undefined get skipped over
BSONObjBuilder bo;
bo.append("create", _curcoll);
while (i.more()) {
BSONElement e = i.next();
if (strcmp(e.fieldName(), "create") == 0) {
continue;
}
if (e.type() == Undefined) {
log() << _curns << ": skipping undefined field: " << e.fieldName() << endl;
continue;
}
bo.append(e);
}
obj = bo.obj();
BSONObj fields = BSON("options" << 1);
scoped_ptr<DBClientCursor> cursor(conn().query(_curdb + ".system.namespaces", Query(BSON("name" << _curns)), 0, 0, &fields));
bool createColl = true;
if (cursor->more()) {
createColl = false;
BSONObj nsObj = cursor->next();
if (!nsObj.hasField("options") || !optionsSame(obj, nsObj["options"].Obj())) {
log() << "WARNING: collection " << _curns << " exists with different options than are in the metadata.json file and not using --drop. Options in the metadata file will be ignored." << endl;
}
}
if (!createColl) {
return;
}
BSONObj info;
if (!conn().runCommand(_curdb, obj, info)) {
uasserted(15936, "Creating collection " + _curns + " failed. Errmsg: " + info["errmsg"].String());
} else {
log() << "\tCreated collection " << _curns << " with options: " << obj.jsonString() << endl;
}
}
示例15: main
int main( int argc, const char **argv ) {
const char *port = "27017";
if ( argc != 1 ) {
if ( argc != 3 ) {
cout << "need to pass port as second param" << endl;
return EXIT_FAILURE;
}
port = argv[ 2 ];
}
Status status = client::initialize();
if ( !status.isOK() ) {
std::cout << "failed to initialize the client driver: " << status.toString() << endl;
return EXIT_FAILURE;
}
ScopedDbConnection conn(string( "127.0.0.1:" ) + port);
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 ) );
std::auto_ptr<DBClientCursor> cursor = conn->query( ns , BSONObj() );
if (!cursor.get()) {
cout << "query failure" << endl;
return EXIT_FAILURE;
}
cout << "using cursor" << endl;
while ( cursor->more() ) {
BSONObj obj = cursor->next();
cout << "\t" << obj.jsonString() << endl;
}
conn->ensureIndex( ns , BSON( "name" << 1 << "num" << -1 ) );
conn.done();
return EXIT_SUCCESS;
}