Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds support for creating unique indexes #182

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions R/client.R
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,16 @@ mongo_cursor_more <- function(cursor){
}

#' @useDynLib mongolite R_mongo_collection_create_index
mongo_collection_create_index <- function(col, field = '{}'){
mongo_collection_create_index <- function(col, field = '{}', isUnique=F){
stopifnot(is.character(field))
stopifnot(length(field) == 1)
stopifnot(is.logical(isUnique))
if(!jsonlite::validate(field)){
if(grepl("[{}]", field))
stop("Index is not valid json or field name.")
field <- jsonlite::toJSON(structure(list(1), names = field), auto_unbox = TRUE)
}
.Call(R_mongo_collection_create_index, col, bson_or_json(field))
.Call(R_mongo_collection_create_index, col, bson_or_json(field), isUnique)
}

#' @useDynLib mongolite R_mongo_collection_drop_index
Expand Down
4 changes: 2 additions & 2 deletions R/mongo.R
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,10 @@ mongo_object <- function(col, verbose, orig){
mongo_collection_command_simple(col, command, simplify)
}

index <- function(add = NULL, remove = NULL){
index <- function(add = NULL, remove = NULL, isUnique=F){
check_col()
if(length(add))
mongo_collection_create_index(col, add);
mongo_collection_create_index(col, add, isUnique);

if(length(remove))
mongo_collection_drop_index(col, remove);
Expand Down
5 changes: 3 additions & 2 deletions src/collection.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,17 @@ SEXP R_mongo_collection_insert_page(SEXP ptr_col, SEXP json_vec, SEXP stop_on_er
return out;
}

SEXP R_mongo_collection_create_index(SEXP ptr_col, SEXP ptr_bson) {
SEXP R_mongo_collection_create_index(SEXP ptr_col, SEXP ptr_bson, SEXP ptr_unique) {
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);
bool isUnique = Rf_asLogical(ptr_unique);
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), "}", "]");
"[", "{", "key", BCON_DOCUMENT (keys), "name", BCON_UTF8 (index_name), "unique", BCON_BOOL(isUnique), "}","]");

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