当前位置: 首页>>代码示例>>C++>>正文


C++ DBClientConnection::insert方法代码示例

本文整理汇总了C++中DBClientConnection::insert方法的典型用法代码示例。如果您正苦于以下问题:C++ DBClientConnection::insert方法的具体用法?C++ DBClientConnection::insert怎么用?C++ DBClientConnection::insert使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在DBClientConnection的用法示例。


在下文中一共展示了DBClientConnection::insert方法的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 ) );
}
开发者ID:agiamas,项目名称:mongo,代码行数:32,代码来源:second.cpp

示例2: 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);
}
开发者ID:BendustiK,项目名称:mongo,代码行数:25,代码来源:tutorial.cpp

示例3: main

int main() {
    try {
        cout << "connecting to localhost..." << endl;
        DBClientConnection c;
        c.connect("localhost");
        cout << "connected ok" << endl;
        unsigned long long count = c.count("test.foo");
        cout << "count of exiting documents in collection test.foo : " << count << endl;

        bo o = BSON( "hello" << "world" );
        c.insert("test.foo", o);

        string e = c.getLastError();
        if( !e.empty() ) { 
            cout << "insert #1 failed: " << e << endl;
        }

        // make an index with a unique key constraint
        c.ensureIndex("test.foo", BSON("hello"<<1), /*unique*/true);

        c.insert("test.foo", o); // will cause a dup key error on "hello" field
        cout << "we expect a dup key error here:" << endl;
        cout << "  " << c.getLastErrorDetailed().toString() << endl;
    } 
    catch(DBException& e) { 
        cout << "caught DBException " << e.toString() << endl;
        return 1;
    }

    return 0;
}
开发者ID:10genReviews,项目名称:mongo,代码行数:31,代码来源:simple_client_demo.cpp

示例4: dprintf

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());
    }
}
开发者ID:AlainRoy,项目名称:htcondor,代码行数:29,代码来源:ODSStatsProcessors.cpp

示例5: prepareDatabase

/* ****************************************************************************
*
* prepareDatabase -
*/
static void prepareDatabase(std::string id, std::string type)
{
  /* Set database */
  setupDatabase();

  DBClientConnection* connection = getMongoConnection();

  /* We create one entity:
   *
   * - 'id', 'type' with four attributes
   *     A1: X
   *     A1: Y
   *     A2: Z
   *     A3: W
   */

  BSONObj en1 = BSON("_id" << BSON("id" << id << "type" << type) <<
                     "attrs" << BSON_ARRAY(
                        BSON("name" << "A1" << "type" << "TA1" << "value" << "X") <<
                        BSON("name" << "A1" << "type" << "TA1bis" << "value" << "Y") <<
                        BSON("name" << "A2" << "type" << "TA2" << "value" << "Z") <<
                        BSON("name" << "A3" << "type" << "TA3" << "value" << "W")
                        )
     );

  connection->insert(ENTITIES_COLL, en1);
}
开发者ID:B-Rich,项目名称:fiware-orion,代码行数:31,代码来源:mapGetIndividualContextEntityAttributes_test.cpp

示例6: 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];
}
开发者ID:tanfulai,项目名称:mongo,代码行数:26,代码来源:MongoJS.cpp

示例7: main

int main() {
    try {
        cout << "connecting to localhost..." << endl;
        DBClientConnection c;
        c.connect("localhost");
        cout << "connected ok" << endl;

        bo o = BSON( "hello" << "world" );

	cout << "inserting..." << endl;

	time_t start = time(0);
	for( unsigned i = 0; i < 1000000; i++ ) {
	  c.insert("test.foo", o);
	}

	// wait until all operations applied
	cout << "getlasterror returns: \"" << c.getLastError() << '"' << endl;

	time_t done = time(0);
	time_t dt = done-start;
	cout << dt << " seconds " << 1000000/dt << " per second" << endl;
    } 
    catch(DBException& e) { 
        cout << "caught DBException " << e.toString() << endl;
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}
开发者ID:328500920,项目名称:mongo,代码行数:30,代码来源:insert_demo.cpp

示例8: run

void run(long time,short spo2,short pr,const char *deviceid) 
{
	DBClientConnection c;
	c.connect(host);
	BSONObj p = BSONObjBuilder().append(TIMESTAMP,(int)time).append(SPo2,spo2).append(PULSERATE,pr).append(DEVICEID,deviceid).obj();
	c.insert(tablename, p);
}
开发者ID:netspective,项目名称:fluent,代码行数:7,代码来源:pulseOximeter-persist.cpp

示例9: 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);
		}
	}
}
开发者ID:jpalonso1,项目名称:BigData,代码行数:30,代码来源:mainhw3.cpp

示例10: 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;
    }
}
开发者ID:upwell,项目名称:study,代码行数:28,代码来源:insert.cpp

示例11: toolMain

int toolMain( int argc, char* argv[], char* envp[] ) {
    mongo::runGlobalInitializersOrDie(argc, argv, envp);
    if( parseCmdLineOptions( argc, argv) )
        return 1;

    BSONObj nestedDoc =  BSON("Firstname" << "David" <<
                              "Lastname" << "Smith" <<
                              "Address" << BSON( "Street" << "5th Av" <<
                                                 "City" << "New York" )
                              );
    std::vector<std::string> list;
    list.push_back("mongo new york city");
    list.push_back("mongo rome");
    list.push_back("mongo dublin");
    list.push_back("mongo seoul");
    list.push_back("mongo barcelona");
    list.push_back("mongo madrid");
    list.push_back("mongo chicago");
    list.push_back("mongo amsterdam");
    list.push_back("mongo delhi");
    list.push_back("mongo beijing");

    BSONObj args = BSONObjBuilder()
                   .append( "_id", 0 )
                   .append( "blob", "MongoDB is an open source document-oriented database "
                                    "system designed with scalability and developer." )
                   .append( "nestedDoc", nestedDoc )
                   .append( "list", list )
                   .append( "counter", 0 ).obj();

    const int numDocsPerDB =
            static_cast<int>( globalDocGenOption.dbSize * 1024 * 1024 / args.objsize() );
    cout << "numDocsPerDB:" << numDocsPerDB << endl;
    try {
        DBClientConnection conn;
        conn.connect( globalDocGenOption.hostname );
        cout << "successfully connected to the host" << endl;
        for( int i=0; i < globalDocGenOption.numdbs; ++i ) {
            scoped_ptr<DocumentGenerator> docGen( DocumentGenerator::makeDocumentGenerator(args) );
            cout << "populating database " << globalDocGenOption.prefix << i  << endl;
            long long j = 0;
            string ns = mongoutils::str::stream() << globalDocGenOption.prefix << i << ".sampledata";
            while( j != numDocsPerDB ) {
                BSONObj doc = docGen->createDocument();
                conn.insert( ns, doc );
                ++j;
            }
            BSONObj blobIndex = BSON("blob" << 1);
            conn.ensureIndex(ns, blobIndex);
            BSONObj listIndex = BSON("list" << 1);
            conn.ensureIndex(ns, listIndex);
        }
    } catch( DBException &e ) {
        cout << "caught " << e.what() << endl;
    }
    return 0;
}
开发者ID:10genReviews,项目名称:mongo,代码行数:57,代码来源:docgeneratormain.cpp

示例12: 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 );
}
开发者ID:10genReviews,项目名称:mongo,代码行数:44,代码来源:whereExample.cpp

示例13: main

int main() { 
  cout << "connecting to localhost..." << endl;
  DBClientConnection c;
  c.connect("localhost");
  cout << "connected ok" << endl;
  unsigned long long count = c.count("test.foo");
  cout << "count of exiting documents in collection test.foo : " << count << endl;

  bo o = BSON( "hello" << "world" );
  c.insert("test.foo", o);

  return 0;
}
开发者ID:jit,项目名称:mongo,代码行数:13,代码来源:simple_client_demo.cpp

示例14: run

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);
}
开发者ID:504com,项目名称:mongo,代码行数:39,代码来源:tutorial.cpp

示例15: set

	void set(const char * key, int statusCode){
	
		string collection= _Ctx->outDb.dbname + string(".leads");
		
		BSONObjBuilder b;

		const time_t log_time = time(0);
		b.append("time", (int)log_time);
		b.append("key", key);
		b.append("IP", *(_Ctx->ppa));
		b.append("status", statusCode);
				
		conn->insert(collection, b.obj());
	}	
开发者ID:Nord001,项目名称:click-server,代码行数:14,代码来源:outputMongoStorage.cpp


注:本文中的DBClientConnection::insert方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。