Example #1
0
SEXP rmongo_find(SEXP mongo_conn, SEXP ns, SEXP query, SEXP sort, SEXP fields, SEXP limit, SEXP skip, SEXP options) {
    mongo* conn = _checkMongo(mongo_conn);
    const char* _ns = CHAR(STRING_ELT(ns, 0));
    bson* _query = _checkBSON(query);
    bson* _sort = _checkBSON(sort);

    bson* q = _query;
    bson sorted_query;
    if (_sort != NULL && bson_size(_sort) > 5) {
        q = &sorted_query;
        bson_init(q);
        bson_append_bson(q, "$query", _query);
        bson_append_bson(q, "$orderby", _sort);
        bson_finish(q);
    }

    bson* _fields = _checkBSON(fields);
    int _limit = asInteger(limit);
    int _skip = asInteger(skip);
    int _options = 0;
    int i;
    int len = LENGTH(options);
    for (i = 0; i < len; i++)
        _options |= INTEGER(options)[i];

    mongo_cursor* cursor = mongo_find(conn, _ns, q, _fields, _limit, _skip, _options);

    if (q == &sorted_query)
        bson_destroy(&sorted_query);

    return _mongo_cursor_create(cursor);
}
Example #2
0
SEXP rmongo_find_one(SEXP mongo_conn, SEXP ns, SEXP query, SEXP fields) {
    mongo* conn = _checkMongo(mongo_conn);
    const char* _ns = CHAR(STRING_ELT(ns, 0));
    bson* _query = _checkBSON(query);
    bson* _fields = _checkBSON(fields);
    bson out;
    if (mongo_find_one(conn, _ns, _query, _fields, &out) != MONGO_OK) {
        return R_NilValue;
    }
    SEXP ret = _mongo_bson_create(&out);
    bson_destroy(&out);
    UNPROTECT(3);
    return ret;
}
Example #3
0
SEXP mongo_index_create(SEXP mongo_conn, SEXP ns, SEXP key, SEXP options) {
    mongo* conn = _checkMongo(mongo_conn);
    const char* _ns = CHAR(STRING_ELT(ns, 0));
    int _options = 0;
    int i;
    int len = LENGTH(options);
    for (i = 0; i < len; i++)
        _options |= INTEGER(options)[i];
    bson* _key;
    bson b;
    int keyIsBSON = _isBSON(key);
    if (keyIsBSON)
        _key = _checkBSON(key);
    else {
        _key = &b;
        len = LENGTH(key);
        bson_init(&b);
        for (i = 0; i < len; i++)
            bson_append_int(&b, CHAR(STRING_ELT(key, i)), 1);
        bson_finish(&b);
    }
    bson out;
    int success = mongo_create_index(conn, _ns, _key, _options, &out);
    if (!keyIsBSON)
        bson_destroy(&b);
    if (success == MONGO_OK) {
        bson_destroy(&out);
        return R_NilValue;
    }
    SEXP ret = _mongo_bson_create(&out);
    bson_destroy(&out);
    UNPROTECT(3);
    return ret;
}
Example #4
0
SEXP rmongo_update(SEXP mongo_conn, SEXP ns, SEXP cond, SEXP op, SEXP flags) {
    mongo* conn = _checkMongo(mongo_conn);
    const char* _ns = CHAR(STRING_ELT(ns, 0));
    bson* _cond = _checkBSON(cond);
    bson* _op = _checkBSON(op);
    int _flags = 0;
    int i;
    int len = LENGTH(flags);
    for (i = 0; i < len; i++)
        _flags |= INTEGER(flags)[i];
    SEXP ret;
    PROTECT(ret = allocVector(LGLSXP, 1));
    LOGICAL(ret)[0] = (mongo_update(conn, _ns, _cond, _op, _flags) == MONGO_OK);
    UNPROTECT(1);
    return ret;
}
Example #5
0
SEXP rmongo_insert_batch(SEXP mongo_conn, SEXP ns, SEXP b) {
    mongo* conn = _checkMongo(mongo_conn);
    const char* _ns = CHAR(STRING_ELT(ns, 0));
    SEXP ret;
    PROTECT(ret = allocVector(LGLSXP, 1));
    if (TYPEOF(b) != VECSXP)
        error("Expected a list of mongo.bson class objects");
    int len = LENGTH(b);
    bson** blist = Calloc(len, bson*);
    int i;
    int success = 1;
    for (i = 0; i < len && success; i++) {
        SEXP _b = VECTOR_ELT(b, i);
        if (!_isBSON(_b))
            success = 0;
        else
            blist[i] = _checkBSON(_b);
    }
    if (success)
        LOGICAL(ret)[0] = (mongo_insert_batch(conn, _ns, blist, len) == MONGO_OK);
    Free(blist);
    if (!success)
        error("Expected list of mongo.bson class objects");
    UNPROTECT(1);
    return ret;
}
Example #6
0
SEXP rmongo_remove(SEXP mongo_conn, SEXP ns, SEXP cond) {
    mongo* conn = _checkMongo(mongo_conn);
    const char* _ns = CHAR(STRING_ELT(ns, 0));
    bson* _cond = _checkBSON(cond);
    SEXP ret;
    PROTECT(ret = allocVector(LGLSXP, 1));
    LOGICAL(ret)[0] = (mongo_remove(conn, _ns, _cond) == MONGO_OK);
    UNPROTECT(1);
    return ret;
}
Example #7
0
SEXP mongo_gridfs_find(SEXP gfs, SEXP query) {
    gridfs* _gfs = _checkGridfs(gfs);
    gridfile* gfile = Calloc(1, gridfile);
    int result;
    if (_isBSON(query)) {
        bson* _query = _checkBSON(query);
        result = gridfs_find_query(_gfs, _query, gfile);
    } else
        result = gridfs_find_filename(_gfs, CHAR(STRING_ELT(query, 0)), gfile);
    if (result != MONGO_OK)
        return R_NilValue;
    return _mongo_gridfile_create(gfs, gfile);
}
Example #8
0
SEXP mongo_command(SEXP mongo_conn, SEXP db, SEXP command) {
    mongo* conn = _checkMongo(mongo_conn);
    const char* _db = CHAR(STRING_ELT(db, 0));
    bson* _command = _checkBSON(command);
    bson out;
    if (mongo_run_command(conn, _db, _command, &out) != MONGO_OK) {
        return R_NilValue;
    }
    SEXP ret = _mongo_bson_create(&out);
    bson_destroy(&out);
    UNPROTECT(3);
    return ret;
}
Example #9
0
SEXP rmongo_count(SEXP mongo_conn, SEXP ns, SEXP query) {
    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';
    bson* _query = _checkBSON(query);
    SEXP ret;
    PROTECT(ret = allocVector(REALSXP, 1));
    REAL(ret)[0] = mongo_count(conn, db, p+1, _query);
    Free(db);
    UNPROTECT(1);
    return ret;
}