int main() {
    
    mongo conn[1];
    bson b;
    const char *sock_path = "/tmp/mongodb-27017.sock";
    const char *ns = "test.c.unix_socket";
    const char *col = "c.unix_socket";

    ASSERT( mongo_client( conn, sock_path, -1 ) == MONGO_OK );

    mongo_cmd_drop_collection( conn, "test", col, NULL );

    bson_init( &b );
    bson_append_new_oid( &b, "_id" );
    bson_append_string( &b, "foo", "bar" );
    bson_append_int( &b, "x", 1);
    bson_finish( &b );

    ASSERT( mongo_insert( conn, ns, &b, NULL ) == MONGO_OK );

    mongo_cmd_drop_collection( conn, "test", col, NULL );

    bson_destroy( &b );
    mongo_destroy( conn );

    return 0;
}
Esempio n. 2
0
/* Test getaddrinfo() by successfully connecting to 'localhost'. */
int test_getaddrinfo( void ) {
    mongo conn[1];
    bson b[1];
    const char *ns = "test.foo";
    const char *errmsg = "getaddrinfo failed";

    if( mongo_client( conn, "badhost.example.com", 27017 ) == MONGO_OK ) {
        printf( "connected to bad host!\n" );
        exit( 1 );
    } else {
	ASSERT( strncmp( errmsg, conn->errstr, strlen( errmsg ) ) == 0 );
    }


    if( mongo_client( conn, "localhost", 27017 ) != MONGO_OK ) {
        printf( "failed to connect\n" );
        exit( 1 );
    }

    mongo_cmd_drop_collection( conn, "test", "foo", NULL );

    bson_init( b );
    bson_append_int( b, "foo", 17 );
    bson_finish( b );

    mongo_insert( conn , ns , b, NULL );

    ASSERT( mongo_count( conn, "test", "foo", NULL ) == 1 );

    bson_destroy( b );
    mongo_destroy( conn );


    return 0;
}
void MongodbClient::DropCollection()
{
    int32 status = mongo_cmd_drop_collection(clientData->connection, database.c_str(), collection.c_str(), NULL);
    if(MONGO_OK != status)
    {
        LogError(String("DropCollection"), clientData->connection->err);
    }
}
int main() {
    mongo conn[1];
    bson cmd[1];
    bson out[1];
    bson_iterator it[1];
    char version[10];

    const char *db = "test";
    const char *col = "c.capped";

    INIT_SOCKETS_FOR_WINDOWS;

    if ( mongo_connect( conn , TEST_SERVER , 27017 ) ) {
        printf( "failed to connect\n" );
        exit( 1 );
    }

    mongo_cmd_drop_collection( conn, db, col, NULL );

    ASSERT( mongo_create_capped_collection( conn, db, col,
          1024, 100, NULL ) == MONGO_OK );

    bson_init( cmd );
    bson_append_string( cmd, "collstats", col );
    bson_finish( cmd );

    ASSERT( mongo_run_command( conn, db, cmd, out ) == MONGO_OK );

    if( mongo_get_server_version( version ) != -1 ){
        if( version[0] == '2' && version[2] >= '1' )
            ASSERT( bson_find( it, out, "capped" ) == BSON_BOOL );
        else ASSERT( bson_find( it, out, "capped" ) == BSON_INT );
    }

    ASSERT( bson_find( it, out, "max" ) == BSON_INT );

    bson_destroy( cmd );
    bson_destroy( out );

    mongo_cmd_drop_collection( conn, "test", col, NULL );
    mongo_cmd_drop_db( conn, db );

    mongo_destroy( conn );
    return 0;
}
void test_batch_insert_with_continue( mongo *conn ) {
    bson *objs[5];
    bson *objs2[5];
    bson empty;
    int i;

    mongo_cmd_drop_collection( conn, TEST_DB, TEST_COL, NULL );
    mongo_create_simple_index( conn, TEST_NS, "n", MONGO_INDEX_UNIQUE, NULL );

    for( i=0; i<5; i++ ) {
        objs[i] = bson_malloc( sizeof( bson ) );
        bson_init( objs[i] );
        bson_append_int( objs[i], "n", i );
        bson_finish( objs[i] );
    }

    ASSERT( mongo_insert_batch( conn, TEST_NS, (const bson **)objs, 5,
        NULL, 0 ) == MONGO_OK );

    ASSERT( mongo_count( conn, TEST_DB, TEST_COL,
          bson_empty( &empty ) ) == 5 );

    /* Add one duplicate value for n. */
    objs2[0] = bson_malloc( sizeof( bson ) );
    bson_init( objs2[0] );
    bson_append_int( objs2[0], "n", 1 );
    bson_finish( objs2[0] );

    /* Add n for 6 - 9. */
    for( i = 1; i < 5; i++ ) {
        objs2[i] = bson_malloc( sizeof( bson ) );
        bson_init( objs2[i] );
        bson_append_int( objs2[i], "n", i + 5 );
        bson_finish( objs2[i] );
    }

    /* Without continue on error, will fail immediately. */
    ASSERT( mongo_insert_batch( conn, TEST_NS, (const bson **)objs2, 5,
        NULL, 0 ) == MONGO_OK );
    ASSERT( mongo_count( conn, TEST_DB, TEST_COL,
          bson_empty( &empty ) ) == 5 );

    /* With continue on error, will insert four documents. */
    ASSERT( mongo_insert_batch( conn, TEST_NS, (const bson **)objs2, 5,
        NULL, MONGO_CONTINUE_ON_ERROR ) == MONGO_OK );
    ASSERT( mongo_count( conn, TEST_DB, TEST_COL,
          bson_empty( &empty ) ) == 9 );

    for( i=0; i<5; i++ ) {
        bson_destroy( objs2[i] );
        bson_free( objs2[i] );

        bson_destroy( objs[i] );
        bson_free( objs[i] );
    }
}
Esempio n. 6
0
SEXP mongo_drop(SEXP mongo_conn, SEXP ns) {
    mongo* conn = _checkMongo(mongo_conn);
    const char* _ns = CHAR(STRING_ELT(ns, 0));
    char* p = strchr((char*)_ns, '.');
    if (!p)
        error("Expected a '.' in the namespace.");
    int len = p - (char*)_ns;
    char* db = Calloc(len+1, char);
    strncpy(db, _ns, len);
    db[len] = '\0';
    SEXP ret;
    PROTECT(ret = allocVector(LGLSXP, 1));
    LOGICAL(ret)[0] = (mongo_cmd_drop_collection(conn, db, p+1, NULL) == MONGO_OK);
    Free(db);
    UNPROTECT(1);
    return ret;
}
Esempio n. 7
0
static void test_write_concern_input( mongo *conn ) {
    mongo_write_concern wc[1], wcbad[1];
    bson b[1];

    mongo_cmd_drop_collection( conn, TEST_DB, TEST_COL, NULL );

    bson_init( b );
    bson_append_new_oid( b, "_id" );
    bson_finish( b );

    mongo_write_concern_init( wc );    
    mongo_write_concern_set_w( wc, 1 );

    /* Failure to finish write concern object. */
    ASSERT( mongo_insert( conn, TEST_NS, b, wc ) != MONGO_OK );
    ASSERT( conn->err == MONGO_WRITE_CONCERN_INVALID );
    ASSERT_EQUAL_STRINGS( conn->errstr,
        "Must call mongo_write_concern_finish() before using *write_concern." );

    mongo_write_concern_finish( wc );

    /* Use a bad write concern. */
    mongo_clear_errors( conn );
    mongo_write_concern_init( wcbad );    
    mongo_write_concern_set_w( wcbad, 2 );
    mongo_write_concern_finish( wcbad );
    mongo_set_write_concern( conn, wcbad );
    ASSERT( mongo_insert( conn, TEST_NS, b, NULL ) != MONGO_OK );
    ASSERT( conn->err == MONGO_WRITE_ERROR );
    ASSERT_EQUAL_STRINGS( conn->lasterrstr, "norepl" );

    /* Ensure that supplied write concern overrides default. */
    mongo_clear_errors( conn );
    ASSERT( mongo_insert( conn, TEST_NS, b, wc ) != MONGO_OK );
    ASSERT( conn->err == MONGO_WRITE_ERROR );
    ASSERT_EQUAL_STRINGS( conn->errstr, "See conn->lasterrstr for details." );
    ASSERT_EQUAL_STRINGS( conn->lasterrstr, "E11000 duplicate key error index" );
    ASSERT( conn->lasterrcode == 11000 );

    conn->write_concern = NULL;
    mongo_write_concern_destroy( wc );
    mongo_write_concern_destroy( wcbad );
    bson_destroy( b );
}
Esempio n. 8
0
int main(){
    mongo_connection conn[1];
    bson_buffer bb;
    bson obj;
    bson cond;
    int i;
    bson_oid_t oid;
    const char* col = "c.update_test";
    const char* ns = "test.c.update_test";

    INIT_SOCKETS_FOR_WINDOWS;

    if (mongo_connect( conn , TEST_SERVER, 27017 )){
        printf("failed to connect\n");
        exit(1);
    }

    /* if the collection doesn't exist dropping it will fail */
    if ( mongo_cmd_drop_collection(conn, "test", col, NULL) == MONGO_OK
          && mongo_find_one(conn, ns, bson_empty(&obj), bson_empty(&obj), NULL) != MONGO_OK ){
        printf("failed to drop collection\n");
        exit(1);
    }

    bson_oid_gen(&oid);

    { /* insert */
        bson_buffer_init(&bb);
        bson_append_oid(&bb, "_id", &oid);
        bson_append_int(&bb, "a", 3 );
        bson_from_buffer(&obj, &bb);
        mongo_insert(conn, ns, &obj);
        bson_destroy(&obj);
    }

    { /* insert */
        bson op;

        bson_buffer_init(&bb);
        bson_append_oid(&bb, "_id", &oid);
        bson_from_buffer(&cond, &bb);

        bson_buffer_init(&bb);
        {
            bson_append_start_object(&bb, "$inc");
                bson_append_int(&bb, "a", 2 );
            bson_append_finish_object(&bb);
        }
        {
            bson_append_start_object(&bb, "$set");
                bson_append_double(&bb, "b", -1.5 );
            bson_append_finish_object(&bb);
        }
        bson_from_buffer(&op, &bb);

        for (i=0; i<5; i++)
            mongo_update(conn, ns, &cond, &op, 0);

        /* cond is used later */
        bson_destroy(&op);
    }

    if( mongo_find_one(conn, ns, &cond, 0, &obj) != MONGO_OK ){
        printf("Failed to find object\n");
        exit(1);
    } else {
        int fields = 0;
        bson_iterator it;
        bson_iterator_init(&it, obj.data);

        bson_destroy(&cond);

        while(bson_iterator_next(&it)){
            switch(bson_iterator_key(&it)[0]){
                case '_': /* id */
                    ASSERT(bson_iterator_type(&it) == BSON_OID);
                    ASSERT(!memcmp(bson_iterator_oid(&it)->bytes, oid.bytes, 12));
                    fields++;
                    break;
                case 'a':
                    ASSERT(bson_iterator_type(&it) == BSON_INT);
                    ASSERT(bson_iterator_int(&it) == 3 + 5*2);
                    fields++;
                    break;
                case 'b':
                    ASSERT(bson_iterator_type(&it) == BSON_DOUBLE);
                    ASSERT(bson_iterator_double(&it) == -1.5);
                    fields++;
                    break;
            }
        }

        ASSERT(fields == 3);
    }

    bson_destroy(&obj);

    mongo_cmd_drop_db(conn, "test");
    mongo_destroy(conn);
    return 0;
}
Esempio n. 9
0
int main() {
    bson b, sub, out;
    bson_iterator it;
    mongo conn;
    mongo_cursor cursor;
    int result;

    /* Create a rich document like this one:
    *
    * { _id: ObjectId("4d95ea712b752328eb2fc2cc"),
    * user_id: ObjectId("4d95ea712b752328eb2fc2cd"),
    *
    * items: [
    * { sku: "col-123",
    * name: "John Coltrane: Impressions",
    * price: 1099,
    * },
    *
    * { sku: "young-456",
    * name: "Larry Young: Unity",
    * price: 1199
    * }
    * ],
    *
    * address: {
    * street: "59 18th St.",
    * zip: 10010
    * },
    *
    * total: 2298
    * }
    */
    bson_init( &b );
    bson_append_new_oid( &b, "_id" );
    bson_append_new_oid( &b, "user_id" );

    bson_append_start_array( &b, "items" );
    bson_append_start_object( &b, "0" );
    bson_append_string( &b, "name", "John Coltrane: Impressions" );
    bson_append_int( &b, "price", 1099 );
    bson_append_finish_object( &b );

    bson_append_start_object( &b, "1" );
    bson_append_string( &b, "name", "Larry Young: Unity" );
    bson_append_int( &b, "price", 1199 );
    bson_append_finish_object( &b );
    bson_append_finish_object( &b );

    bson_append_start_object( &b, "address" );
    bson_append_string( &b, "street", "59 18th St." );
    bson_append_int( &b, "zip", 10010 );
    bson_append_finish_object( &b );

    bson_append_int( &b, "total", 2298 );

    /* Finish the BSON obj. */
    bson_finish( &b );
    printf("Here's the whole BSON object:\n");
    bson_print( &b );

    /* Advance to the 'items' array */
    bson_find( &it, &b, "items" );

    /* Get the subobject representing items */
    bson_iterator_subobject_init( &it, &sub, 0 );

    /* Now iterate that object */
    printf("And here's the inner sub-object by itself.\n");
    bson_print( &sub );
    bson_destroy( &sub );

    /* Now make a connection to MongoDB. */
    if( mongo_client( &conn, "127.0.0.1", 27017 ) != MONGO_OK ) {
      switch( conn.err ) {
        case MONGO_CONN_SUCCESS:
          break;
        case MONGO_CONN_NO_SOCKET:
          printf( "FAIL: Could not create a socket!\n" );
          break;
        case MONGO_CONN_FAIL:
          printf( "FAIL: Could not connect to mongod. Make sure it's listening at 127.0.0.1:27017.\n" );
          break;
        default:
          printf( "MongoDB connection error number %d.\n", conn.err );
          break;
      }

      exit( 1 );
    }

    /* Insert the sample BSON document. */
    if( mongo_insert( &conn, "test.records", &b, NULL ) != MONGO_OK ) {
      printf( "FAIL: Failed to insert document with error %d\n", conn.err );
      exit( 1 );
    }

    /* Query for the sample document. */
    mongo_cursor_init( &cursor, &conn, "test.records" );
    mongo_cursor_set_query( &cursor, bson_shared_empty() );
    if( mongo_cursor_next( &cursor ) != MONGO_OK ) {
      printf( "FAIL: Failed to find inserted document." );
      exit( 1 );
    }

    printf( "Found saved BSON object:\n" );
    bson_print( (bson *)mongo_cursor_bson( &cursor ) );

    mongo_cmd_drop_collection( &conn, "test", "records", NULL );
    mongo_cursor_destroy( &cursor );
    bson_destroy( &b );
    mongo_destroy( &conn );

    return 0;
}
Esempio n. 10
0
/* TODO remove and add mongo_create_collection to the public API. */
static void create_capped_collection( mongo *conn ) {
    mongo_cmd_drop_collection( conn, "test", "wc", NULL );
    mongo_create_capped_collection( conn, "test", "wc", 1000000, 0, NULL );
}
Esempio n. 11
0
static void test_insert( mongo *conn ) {
    mongo_write_concern wc0[1], wc1[1];
    bson b[1], b2[1], b3[1], b4[1];
    bson *objs[2];

    mongo_cmd_drop_collection( conn, TEST_DB, TEST_COL, NULL );

    mongo_write_concern_init( wc0 );    
    mongo_write_concern_set_w( wc0, 0 );
    mongo_write_concern_finish( wc0 );
    mongo_write_concern_init( wc1 );    
    mongo_write_concern_set_w( wc1, 1 );
    mongo_write_concern_finish( wc1 );

    bson_init( b4 );
    bson_append_string( b4, "foo", "bar" );
    bson_finish( b4 );

    ASSERT( mongo_insert( conn, TEST_NS, b4, wc1 ) == MONGO_OK );

    ASSERT( mongo_remove( conn, TEST_NS, bson_shared_empty( ), wc1 ) == MONGO_OK );

    bson_init( b );
    bson_append_new_oid( b, "_id" );
    bson_finish( b );

    ASSERT( mongo_insert( conn, TEST_NS, b, NULL ) == MONGO_OK );

    /* This fails but returns OK with write concern w = 0 */
    ASSERT( mongo_insert( conn, TEST_NS, b, wc0 ) == MONGO_OK ); /* no getLastError request */

    ASSERT( mongo_insert( conn, TEST_NS, b, wc1 ) == MONGO_ERROR );
    ASSERT( conn->err == MONGO_WRITE_ERROR );
    ASSERT_EQUAL_STRINGS( conn->errstr, "See conn->lasterrstr for details." );
    ASSERT_EQUAL_STRINGS( conn->lasterrstr, "E11000 duplicate key error index" );
    ASSERT( conn->lasterrcode == 11000 );
    mongo_clear_errors( conn );

    /* Still fails but returns OK with write concern w = 0 */
    ASSERT( mongo_insert( conn, TEST_NS, b, wc0 ) == MONGO_OK );

    /* But not when we set a default write concern on the conn. */
    mongo_set_write_concern( conn, wc1 );
    ASSERT( mongo_insert( conn, TEST_NS, b, NULL ) != MONGO_OK );
    ASSERT( conn->err == MONGO_WRITE_ERROR );
    ASSERT_EQUAL_STRINGS( conn->errstr, "See conn->lasterrstr for details." );
    ASSERT_EQUAL_STRINGS( conn->lasterrstr, "E11000 duplicate key error index" );
    ASSERT( conn->lasterrcode == 11000 );

    /* Now test batch insert. */
    bson_init( b2 );
    bson_append_new_oid( b2, "_id" );
    bson_finish( b2 );

    bson_init( b3 );
    bson_append_new_oid( b3, "_id" );
    bson_finish( b3 );

    objs[0] = b2;
    objs[1] = b3;

    /* Insert two new documents by insert_batch. */
    conn->write_concern = NULL;
    ASSERT( mongo_count( conn, TEST_DB, TEST_COL, bson_shared_empty( ) ) == 1 );
    ASSERT( mongo_insert_batch( conn, TEST_NS, (const bson **)objs, 2, NULL, 0 ) == MONGO_OK );
    ASSERT( mongo_count( conn, TEST_DB, TEST_COL, bson_shared_empty( ) ) == 3 );

    /* This should definitely fail if we try again with write concern. */
    mongo_clear_errors( conn );
    ASSERT( mongo_insert_batch( conn, TEST_NS, (const bson **)objs, 2, wc1, 0 ) == MONGO_ERROR );
    ASSERT( conn->err == MONGO_WRITE_ERROR );
    ASSERT_EQUAL_STRINGS( conn->errstr, "See conn->lasterrstr for details." );
    ASSERT_EQUAL_STRINGS( conn->lasterrstr, "E11000 duplicate key error index" );
    ASSERT( conn->lasterrcode == 11000 );

    /* But it will succeed without the write concern set. */
    ASSERT( mongo_insert_batch( conn, TEST_NS, (const bson **)objs, 2, NULL, 0 ) == MONGO_OK );

    bson_destroy( b );
    bson_destroy( b2 );
    bson_destroy( b3 );
    bson_destroy( b4 );
    mongo_write_concern_destroy( wc0 );
    mongo_write_concern_destroy( wc1 );
}
Esempio n. 12
0
int main(){
    mongo_connection conn[1];
    mongo_connection_options opts;
    bson_buffer bb;
    bson b;
    mongo_cursor * cursor;
    int i;
    char hex_oid[25];

    const char * col = "c.simple";
    const char * ns = "test.c.simple";

    INIT_SOCKETS_FOR_WINDOWS;
    
    strncpy(opts.host, TEST_SERVER, 255);
    opts.host[254] = '\0';
    opts.port = 27017;

    if (mongo_connect( conn , &opts )){
        printf("failed to connect\n");
        exit(1);
    }

    /* if the collection doesn't exist dropping it will fail */
    if (!mongo_cmd_drop_collection(conn, "test", col, NULL)
          && mongo_find_one(conn, ns, bson_empty(&b), bson_empty(&b), NULL)){
        printf("failed to drop collection\n");
        exit(1);
    }

    for(i=0; i< 5; i++){
        bson_buffer_init( & bb );

        bson_append_new_oid( &bb, "_id" );
        bson_append_double( &bb , "a" , 17 );
        bson_append_int( &bb , "b" , 17 );
        bson_append_string( &bb , "c" , "17" );

        {
            bson_buffer * sub = bson_append_start_object(  &bb , "d" );
            bson_append_int( sub, "i", 71 );
            bson_append_finish_object(sub);
        }
        {
            bson_buffer * arr = bson_append_start_array(  &bb , "e" );
            bson_append_int( arr, "0", 71 );
            bson_append_string( arr, "1", "71" );
            bson_append_finish_object(arr);
        }

        bson_from_buffer(&b, &bb);
        mongo_insert( conn , ns , &b );
        bson_destroy(&b);
    }
    
    cursor = mongo_find( conn , ns , bson_empty(&b) , 0 , 0 , 0 , 0 );

    while (mongo_cursor_next(cursor)){
        bson_iterator it;
        bson_iterator_init(&it, cursor->current.data);
        while(bson_iterator_next(&it)){
            fprintf(stderr, "  %s: ", bson_iterator_key(&it));

            switch(bson_iterator_type(&it)){
                case bson_double:
                    fprintf(stderr, "(double) %e\n", bson_iterator_double(&it));
                    break;
                case bson_int:
                    fprintf(stderr, "(int) %d\n", bson_iterator_int(&it));
                    break;
                case bson_string:
                    fprintf(stderr, "(string) \"%s\"\n", bson_iterator_string(&it));
                    break;
                case bson_oid:
                    bson_oid_to_string(bson_iterator_oid(&it), hex_oid);
                    fprintf(stderr, "(oid) \"%s\"\n", hex_oid);
                    break;
                case bson_object:
                    fprintf(stderr, "(subobject) {...}\n");
                    break;
                case bson_array:
                    fprintf(stderr, "(array) [...]\n");
                    break;
                default:
                    fprintf(stderr, "(type %d)\n", bson_iterator_type(&it));
                    break;
            }
        }
        fprintf(stderr, "\n");
    }

    mongo_cursor_destroy(cursor);
    mongo_cmd_drop_db(conn, "test");
    mongo_destroy( conn );
    return 0;
}
Esempio n. 13
0
void remove_sample_data( mongo *conn ) {
    mongo_cmd_drop_collection( conn, "test", "cursors", NULL );
}