Example #1
0
SEXP R_mongo_collection_update(SEXP ptr_col, SEXP ptr_selector, SEXP ptr_update, SEXP ptr_filters, SEXP upsert, SEXP multiple, SEXP replace){
  mongoc_collection_t *col = r2col(ptr_col);
  bson_t *selector = r2bson(ptr_selector);
  bson_t *update = r2bson(ptr_update);

  bool success;
  bson_t opts;
  bson_init (&opts);
  BSON_APPEND_BOOL (&opts, "upsert", Rf_asLogical(upsert));
  if(!Rf_isNull(ptr_filters))
    BSON_APPEND_ARRAY (&opts, "arrayFilters",  r2bson(ptr_filters));

  bson_error_t err;
  bson_t reply;
  if(Rf_asLogical(replace)){
    success = mongoc_collection_replace_one(col, selector, update, &opts, &reply, &err);
  } else {
    success = Rf_asLogical(multiple) ?
      mongoc_collection_update_many(col, selector, update, &opts, &reply, &err) :
      mongoc_collection_update_one(col, selector, update, &opts, &reply, &err);
  }

  if(!success)
    stop(err.message);

  return bson2list(&reply);
}
Example #2
0
SEXP R_mongo_collection_find(SEXP ptr_col, SEXP ptr_query, SEXP ptr_opts) {
  mongoc_collection_t *col = r2col(ptr_col);
  bson_t *query = r2bson(ptr_query);
  bson_t *opts = r2bson(ptr_opts);
  mongoc_cursor_t *c = mongoc_collection_find_with_opts(col, query, opts, NULL);
  return cursor2r(c, ptr_col);
}
Example #3
0
SEXP R_mongo_collection_aggregate(SEXP ptr_col, SEXP ptr_pipeline, SEXP ptr_options, SEXP no_timeout) {
  mongoc_collection_t *col = r2col(ptr_col);
  bson_t *pipeline = r2bson(ptr_pipeline);
  bson_t *options = r2bson(ptr_options);

  mongoc_query_flags_t flags = MONGOC_QUERY_NONE;
  if(Rf_asLogical(no_timeout))
    flags += MONGOC_QUERY_NO_CURSOR_TIMEOUT;

  mongoc_cursor_t *c = mongoc_collection_aggregate (col, flags, pipeline, options, NULL);
  if(!c)
    stop("Error executing pipeline.");
  return cursor2r(c, ptr_col);
}
Example #4
0
SEXP R_mongo_collection_find(SEXP ptr_col, SEXP ptr_query, SEXP ptr_fields, SEXP skip, SEXP limit, SEXP no_timeout) {
  mongoc_collection_t *col = r2col(ptr_col);
  bson_t *query = r2bson(ptr_query);
  bson_t *fields = r2bson(ptr_fields);

  mongoc_query_flags_t flags = MONGOC_QUERY_NONE;
  if(asLogical(no_timeout))
    flags += MONGOC_QUERY_NO_CURSOR_TIMEOUT;

  mongoc_cursor_t *c = mongoc_collection_find(col, flags, asInteger(skip), asInteger(limit),
    0, query, fields, NULL);

  return cursor2r(c);
}
Example #5
0
SEXP R_mongo_collection_update(SEXP ptr_col, SEXP ptr_selector, SEXP ptr_update, SEXP upsert, SEXP multiple){
  mongoc_collection_t *col = r2col(ptr_col);
  bson_t *selector = r2bson(ptr_selector);
  bson_t *update = r2bson(ptr_update);

  //set update flags
  mongoc_update_flags_t flags = MONGOC_UPDATE_NONE;
  if(asLogical(upsert))
    flags = flags + MONGOC_UPDATE_UPSERT;
  if(asLogical(multiple))
    flags = flags + MONGOC_UPDATE_MULTI_UPDATE;

  bson_error_t err;
  if(!mongoc_collection_update(col, flags, selector, update, NULL, &err))
    stop(err.message);

  return ScalarLogical(1);
}
Example #6
0
SEXP R_mongo_collection_insert_bson(SEXP ptr_col, SEXP ptr_bson, SEXP stop_on_error){
  mongoc_collection_t *col = r2col(ptr_col);
  bson_t *b = r2bson(ptr_bson);
  mongoc_insert_flags_t flags = Rf_asLogical(stop_on_error) ? MONGOC_INSERT_NONE : MONGOC_INSERT_CONTINUE_ON_ERROR;
  bson_error_t err;

  if(!mongoc_collection_insert(col, flags, b, NULL, &err))
    stop(err.message);

  return Rf_ScalarLogical(1);
}
Example #7
0
SEXP R_mongo_collection_count (SEXP ptr, SEXP ptr_filter){
  mongoc_collection_t *col = r2col(ptr);
  bson_t *filter = r2bson(ptr_filter);
  bson_error_t err;
  int64_t count = mongoc_collection_count_documents (col, filter, NULL, NULL, NULL, &err);
  if (count < 0)
    stop(err.message);

  //R does not support int64
  return Rf_ScalarReal((double) count);
}
Example #8
0
SEXP R_mongo_collection_remove(SEXP ptr_col, SEXP ptr_bson, SEXP just_one){
  mongoc_collection_t *col = r2col(ptr_col);
  bson_t *b = r2bson(ptr_bson);
  bson_error_t err;
  mongoc_remove_flags_t flags = Rf_asLogical(just_one) ? MONGOC_REMOVE_SINGLE_REMOVE : MONGOC_REMOVE_NONE;

  if(!mongoc_collection_remove(col, flags, b, NULL, &err))
    stop(err.message);

  return Rf_ScalarLogical(1);
}
Example #9
0
SEXP R_mongo_collection_create_index(SEXP ptr_col, SEXP ptr_bson) {
  mongoc_collection_t *col = r2col(ptr_col);
  bson_t *b = r2bson(ptr_bson);
  const mongoc_index_opt_t *options = mongoc_index_opt_get_default();
  bson_error_t err;

  if(!mongoc_collection_create_index(col, b, options, &err))
    stop(err.message);

  return ScalarLogical(1);
}
Example #10
0
SEXP R_mongo_collection_command_simple(SEXP ptr_col, SEXP command){
  mongoc_collection_t *col = r2col(ptr_col);
  bson_t *cmd = r2bson(command);
  bson_t reply;
  bson_error_t err;
  if(!mongoc_collection_command_simple(col, cmd, NULL, &reply, &err))
    stop(err.message);

  SEXP out = PROTECT(bson2list(&reply));
  bson_destroy (&reply);
  UNPROTECT(1);
  return out;
}
Example #11
0
SEXP R_mongo_collection_command(SEXP ptr_col, SEXP ptr_cmd, SEXP no_timeout){
  mongoc_collection_t *col = r2col(ptr_col);
  bson_t *cmd = r2bson(ptr_cmd);

  mongoc_query_flags_t flags = MONGOC_QUERY_NONE;
  if(Rf_asLogical(no_timeout))
    flags += MONGOC_QUERY_NO_CURSOR_TIMEOUT;

  mongoc_cursor_t *c = mongoc_collection_command(col, flags, 0, 0, 0, cmd, NULL, NULL);
  if(!c)
    stop("Error executing command.");
  return cursor2r(c, ptr_col);
}
Example #12
0
SEXP R_mongo_collection_create_index(SEXP ptr_col, SEXP ptr_bson) {
  mongoc_collection_t *col = r2col(ptr_col);
  bson_t *keys = r2bson(ptr_bson);
  const char * collection_name = mongoc_collection_get_name(col);
  char * index_name = mongoc_collection_keys_to_index_string (keys);
  bson_error_t err;

  //From: https://s3.amazonaws.com/mciuploads/mongo-c-driver/docs/latest/create-indexes.html
  bson_t * command = BCON_NEW ("createIndexes", BCON_UTF8 (collection_name), "indexes",
    "[", "{", "key", BCON_DOCUMENT (keys), "name", BCON_UTF8 (index_name), "}", "]");

  if(!mongoc_collection_write_command_with_opts(col, command, NULL, NULL, &err))
    stop(err.message);

  return Rf_ScalarLogical(1);
}
Example #13
0
SEXP R_mongo_collection_count (SEXP ptr, SEXP ptr_query, SEXP no_timeout){
  mongoc_collection_t *col = r2col(ptr);
  bson_t *query = r2bson(ptr_query);

  bson_error_t err;
  mongoc_query_flags_t flags = MONGOC_QUERY_NONE;
  if(asLogical(no_timeout))
    flags += MONGOC_QUERY_NO_CURSOR_TIMEOUT;

  int64_t count = mongoc_collection_count (col, flags, query, 0, 0, NULL, &err);
  if (count < 0)
    stop(err.message);

  //R does not support int64
  return ScalarReal((double) count);
}
Example #14
0
File: bson.c Project: CDC/mongolite
SEXP R_bson_to_list(SEXP ptr) {
  bson_t *b = r2bson(ptr);
  return bson2list(b);
}
Example #15
0
File: bson.c Project: CDC/mongolite
SEXP R_bson_to_raw(SEXP ptr){
  bson_t *b = r2bson(ptr);
  const uint8_t *buf = bson_get_data(b);
  return mkRaw(buf, b->len);
}
Example #16
0
File: bson.c Project: CDC/mongolite
SEXP R_bson_to_json(SEXP ptr){
  return mkStringUTF8(bson_as_json (r2bson(ptr), NULL));
}