bool drop_collections (mongoc_gridfs_t *gridfs, bson_error_t *error) { return (mongoc_collection_drop (mongoc_gridfs_get_files (gridfs), error) && mongoc_collection_drop (mongoc_gridfs_get_chunks (gridfs), error)); }
static void test_drop (void) { mongoc_collection_t *collection; mongoc_database_t *database; mongoc_client_t *client; bson_error_t error; bson_t *doc; bool r; client = mongoc_client_new(gTestUri); ASSERT (client); database = get_test_database (client); ASSERT (database); collection = get_test_collection (client, "test_drop"); ASSERT (collection); doc = BCON_NEW("hello", "world"); r = mongoc_collection_insert(collection, MONGOC_INSERT_NONE, doc, NULL, &error); bson_destroy (doc); ASSERT (r); r = mongoc_collection_drop(collection, &error); ASSERT (r == true); r = mongoc_collection_drop(collection, &error); ASSERT (r == false); mongoc_collection_destroy(collection); mongoc_database_destroy(database); mongoc_client_destroy(client); }
static void test_insert (void) { mongoc_database_t *database; mongoc_collection_t *collection; mongoc_client_t *client; bson_context_t *context; bson_error_t error; bool r; bson_oid_t oid; unsigned i; bson_t b; client = mongoc_client_new(gTestUri); ASSERT (client); database = get_test_database (client); ASSERT (database); collection = get_test_collection (client, "test_insert"); ASSERT (collection); mongoc_collection_drop(collection, &error); context = bson_context_new(BSON_CONTEXT_NONE); ASSERT (context); for (i = 0; i < 10; i++) { bson_init(&b); bson_oid_init(&oid, context); bson_append_oid(&b, "_id", 3, &oid); bson_append_utf8(&b, "hello", 5, "/world", 5); r = mongoc_collection_insert(collection, MONGOC_INSERT_NONE, &b, NULL, &error); if (!r) { MONGOC_WARNING("%s\n", error.message); } ASSERT (r); bson_destroy(&b); } bson_init (&b); BSON_APPEND_INT32 (&b, "$hello", 1); r = mongoc_collection_insert (collection, MONGOC_INSERT_NONE, &b, NULL, &error); ASSERT (!r); ASSERT (error.domain == MONGOC_ERROR_BSON); ASSERT (error.code == MONGOC_ERROR_BSON_INVALID); bson_destroy (&b); r = mongoc_collection_drop (collection, &error); ASSERT (r); mongoc_collection_destroy(collection); mongoc_database_destroy(database); bson_context_destroy(context); mongoc_client_destroy(client); }
int main(void) { mongoc_collection_t *collection; mongoc_database_t *database; mongoc_client_t *client; bson_error_t error; bson_t *options; client = mongoc_client_new ("mongodb://localhost:27017/admin"); database = mongoc_client_get_database (client, "databaseName"); options = BCON_NEW ( "validator", "{", "age", "{", "$lte", BCON_INT32 (34), "}", "}", "validationAction", BCON_UTF8 ("error"), "validationLevel", BCON_UTF8 ("moderate")); collection = mongoc_database_create_collection (database, "collectionName", options, &error); if (!collection) { fprintf(stderr, "Got error: \"%s\" on line %d\n", error.message, __LINE__); return 1; } fam_flags (collection); fam_bypass (collection); fam_update (collection); fam_fields (collection); fam_sort (collection); mongoc_collection_drop (collection, NULL); bson_destroy (options); mongoc_database_destroy (database); mongoc_collection_destroy (collection); mongoc_client_destroy (client); return 0; }
static void test_rename (void) { mongoc_collection_t *collection; mongoc_client_t *client; bson_error_t error; bson_t doc = BSON_INITIALIZER; bool r; client = mongoc_client_new (gTestUri); ASSERT (client); collection = get_test_collection (client, "test_rename"); ASSERT (collection); r = mongoc_collection_insert (collection, MONGOC_INSERT_NONE, &doc, NULL, &error); assert (r); r = mongoc_collection_rename (collection, "test", "test_rename_2", false, &error); assert (r); r = mongoc_collection_drop (collection, &error); assert (r); mongoc_collection_destroy (collection); mongoc_client_destroy (client); bson_destroy (&doc); }
static void test_split_insert (void) { mongoc_write_command_t command; mongoc_write_result_t result; mongoc_collection_t *collection; mongoc_client_t *client; bson_oid_t oid; bson_t **docs; bson_t reply = BSON_INITIALIZER; bson_error_t error; int i; bool r; client = test_framework_client_new (NULL); assert (client); collection = get_test_collection (client, "test_split_insert"); assert (collection); docs = bson_malloc (sizeof(bson_t*) * 3000); for (i = 0; i < 3000; i++) { docs [i] = bson_new (); bson_oid_init (&oid, NULL); BSON_APPEND_OID (docs [i], "_id", &oid); } _mongoc_write_result_init (&result); _mongoc_write_command_init_insert (&command, (const bson_t * const *)docs, 3000, true, true); _mongoc_write_command_execute (&command, client, 0, collection->db, collection->collection, NULL, 0, &result); r = _mongoc_write_result_complete (&result, &reply, &error); assert (r); assert (result.nInserted == 3000); _mongoc_write_command_destroy (&command); _mongoc_write_result_destroy (&result); r = mongoc_collection_drop (collection, &error); assert (r); for (i = 0; i < 3000; i++) { bson_destroy (docs [i]); } bson_free (docs); mongoc_collection_destroy (collection); mongoc_client_destroy (client); }
static void test_large_return (void) { mongoc_collection_t *collection; mongoc_client_t *client; mongoc_cursor_t *cursor; bson_error_t error; const bson_t *doc = NULL; bson_oid_t oid; bson_t insert_doc = BSON_INITIALIZER; bson_t query = BSON_INITIALIZER; size_t len; char *str; bool r; client = mongoc_client_new (gTestUri); ASSERT (client); collection = get_test_collection (client, "test_large_return"); ASSERT (collection); len = 1024 * 1024 * 4; str = bson_malloc (len); memset (str, (int)' ', len); str [len - 1] = '\0'; bson_oid_init (&oid, NULL); BSON_APPEND_OID (&insert_doc, "_id", &oid); BSON_APPEND_UTF8 (&insert_doc, "big", str); r = mongoc_collection_insert (collection, MONGOC_INSERT_NONE, &insert_doc, NULL, &error); assert (r); bson_destroy (&insert_doc); BSON_APPEND_OID (&query, "_id", &oid); cursor = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 0, 0, &query, NULL, NULL); assert (cursor); bson_destroy (&query); r = mongoc_cursor_next (cursor, &doc); assert (r); assert (doc); r = mongoc_cursor_next (cursor, &doc); assert (!r); mongoc_cursor_destroy (cursor); r = mongoc_collection_drop (collection, &error); assert (r); mongoc_collection_destroy (collection); mongoc_client_destroy (client); bson_free (str); }
static void test_delete (void) { mongoc_collection_t *collection; mongoc_database_t *database; mongoc_client_t *client; bson_context_t *context; bson_error_t error; bool r; bson_oid_t oid; bson_t b; int i; client = mongoc_client_new(gTestUri); ASSERT (client); database = get_test_database (client); ASSERT (database); collection = get_test_collection (client, "test_delete"); ASSERT (collection); context = bson_context_new(BSON_CONTEXT_NONE); ASSERT (context); for (i = 0; i < 100; i++) { bson_init(&b); bson_oid_init(&oid, context); bson_append_oid(&b, "_id", 3, &oid); bson_append_utf8(&b, "hello", 5, "world", 5); r = mongoc_collection_insert(collection, MONGOC_INSERT_NONE, &b, NULL, &error); if (!r) { MONGOC_WARNING("%s\n", error.message); } ASSERT (r); bson_destroy(&b); bson_init(&b); bson_append_oid(&b, "_id", 3, &oid); r = mongoc_collection_delete(collection, MONGOC_DELETE_NONE, &b, NULL, &error); if (!r) { MONGOC_WARNING("%s\n", error.message); } ASSERT (r); bson_destroy(&b); } r = mongoc_collection_drop (collection, &error); ASSERT (r); mongoc_collection_destroy(collection); mongoc_database_destroy(database); bson_context_destroy(context); mongoc_client_destroy(client); }
SEXP R_mongo_collection_drop (SEXP ptr){ mongoc_collection_t *col = r2col(ptr); bson_error_t err; if(!mongoc_collection_drop(col, &err)) stop(err.message); return ScalarLogical(1); }
SEXP R_mongo_collection_drop (SEXP ptr){ mongoc_collection_t *col = r2col(ptr); bson_error_t err; int res = mongoc_collection_drop(col, &err); if(!res && err.code != 26) stop(err.message); return Rf_ScalarLogical(res); }
int main (int argc, char *argv[]) { mongoc_client_t *client; mongoc_apm_callbacks_t *callbacks; stats_t stats = { 0 }; mongoc_collection_t *collection; const char *uristr = "mongodb://127.0.0.1/?appname=cmd-monitoring-example"; const char *collection_name = "test"; bson_t doc; mongoc_init (); if (argc > 1) { uristr = argv [1]; } client = mongoc_client_new (uristr); if (!client) { fprintf (stderr, "Failed to parse URI.\n"); return EXIT_FAILURE; } mongoc_client_set_error_api (client, 2); callbacks = mongoc_apm_callbacks_new (); mongoc_apm_set_command_started_cb (callbacks, command_started); mongoc_apm_set_command_succeeded_cb (callbacks, command_succeeded ); mongoc_apm_set_command_failed_cb (callbacks, command_failed); mongoc_client_set_apm_callbacks (client, callbacks, (void *) &stats /* context pointer */); bson_init (&doc); BSON_APPEND_INT32 (&doc, "_id", 1); collection = mongoc_client_get_collection (client, "test", collection_name); mongoc_collection_drop (collection, NULL); mongoc_collection_insert (collection, MONGOC_INSERT_NONE, &doc, NULL, NULL); /* duplicate key error on the second insert */ mongoc_collection_insert (collection, MONGOC_INSERT_NONE, &doc, NULL, NULL); printf ("started: %d\nsucceeded: %d\nfailed: %d\n", stats.started, stats.succeeded, stats.failed); bson_destroy (&doc); mongoc_collection_destroy (collection); mongoc_apm_callbacks_destroy (callbacks); mongoc_client_destroy (client); mongoc_cleanup (); return EXIT_SUCCESS; }
bool mongoc_gridfs_drop (mongoc_gridfs_t *gridfs, bson_error_t *error) { bool r; ENTRY; r = mongoc_collection_drop (gridfs->files, error); if (!r) { RETURN (0); } r = mongoc_collection_drop (gridfs->chunks, error); if (!r) { RETURN (0); } RETURN (1); }
static void test_find_and_modify_write_concern_wire_32_failure (void *context) { mongoc_collection_t *collection; mongoc_client_t *client; bson_error_t error; mongoc_find_and_modify_opts_t *opts; bson_t reply; bson_t query = BSON_INITIALIZER; bson_t *update; bool success; mongoc_write_concern_t *wc; client = test_framework_client_new (); collection = get_test_collection (client, "writeFailure"); wc = mongoc_write_concern_new (); mongoc_write_concern_set_w (wc, 42); mongoc_collection_set_write_concern (collection, wc); /* Find Zlatan Ibrahimovic, the striker */ BSON_APPEND_UTF8 (&query, "firstname", "Zlatan"); BSON_APPEND_UTF8 (&query, "lastname", "Ibrahimovic"); BSON_APPEND_UTF8 (&query, "profession", "Football player"); BSON_APPEND_INT32 (&query, "age", 34); BSON_APPEND_INT32 ( &query, "goals", (16 + 35 + 23 + 57 + 16 + 14 + 28 + 84) + (1 + 6 + 62)); /* Add his football position */ update = BCON_NEW ("$set", "{", "position", BCON_UTF8 ("striker"), "}"); opts = mongoc_find_and_modify_opts_new (); mongoc_find_and_modify_opts_set_update (opts, update); /* Create the document if it didn't exist, and return the updated document */ mongoc_find_and_modify_opts_set_flags ( opts, MONGOC_FIND_AND_MODIFY_UPSERT | MONGOC_FIND_AND_MODIFY_RETURN_NEW); success = mongoc_collection_find_and_modify_with_opts ( collection, &query, opts, &reply, &error); ASSERT (!success); ASSERT_ERROR_CONTAINS ( error, MONGOC_ERROR_WRITE_CONCERN, 100, "Write Concern error:"); bson_destroy (&reply); bson_destroy (update); bson_destroy (&query); mongoc_find_and_modify_opts_destroy (opts); mongoc_collection_drop (collection, NULL); mongoc_collection_destroy (collection); mongoc_client_destroy (client); }
static void test_regex (void) { mongoc_collection_t *collection; mongoc_database_t *database; mongoc_write_concern_t *wr; mongoc_client_t *client; bson_error_t error = { 0 }; int64_t count; bson_t q = BSON_INITIALIZER; bson_t *doc; bool r; client = mongoc_client_new (gTestUri); ASSERT (client); database = get_test_database (client); ASSERT (database); collection = get_test_collection (client, "test_regex"); ASSERT (collection); wr = mongoc_write_concern_new (); mongoc_write_concern_set_journal (wr, true); doc = BCON_NEW ("hello", "/world"); r = mongoc_collection_insert (collection, MONGOC_INSERT_NONE, doc, wr, &error); ASSERT (r); BSON_APPEND_REGEX (&q, "hello", "^/wo", "i"); count = mongoc_collection_count (collection, MONGOC_QUERY_NONE, &q, 0, 0, NULL, &error); ASSERT (count > 0); ASSERT (!error.domain); r = mongoc_collection_drop (collection, &error); ASSERT (r); mongoc_write_concern_destroy (wr); bson_destroy (&q); bson_destroy (doc); mongoc_collection_destroy (collection); mongoc_database_destroy(database); mongoc_client_destroy (client); }
static void test_create_collection (void) { mongoc_database_t *database; mongoc_collection_t *collection; mongoc_client_t *client; bson_error_t error = { 0 }; bson_t options; bson_t storage_opts; bson_t wt_opts; char *dbname; char *name; bool r; client = mongoc_client_new (gTestUri); assert (client); dbname = gen_collection_name ("dbtest"); database = mongoc_client_get_database (client, dbname); assert (database); bson_free (dbname); bson_init (&options); BSON_APPEND_INT32 (&options, "size", 1234); BSON_APPEND_INT32 (&options, "max", 4567); BSON_APPEND_BOOL (&options, "capped", true); BSON_APPEND_BOOL (&options, "autoIndexId", true); BSON_APPEND_DOCUMENT_BEGIN(&options, "storage", &storage_opts); BSON_APPEND_DOCUMENT_BEGIN(&storage_opts, "wiredtiger", &wt_opts); BSON_APPEND_UTF8(&wt_opts, "configString", "block_compressor=zlib"); bson_append_document_end(&storage_opts, &wt_opts); bson_append_document_end(&options, &storage_opts); name = gen_collection_name ("create_collection"); collection = mongoc_database_create_collection (database, name, &options, &error); assert (collection); bson_destroy (&options); bson_free (name); r = mongoc_collection_drop (collection, &error); assert (r); r = mongoc_database_drop (database, &error); assert (r); mongoc_collection_destroy (collection); mongoc_database_destroy (database); mongoc_client_destroy (client); }
static void test_drop (void) { mongoc_collection_t *collection; mongoc_client_t *client; bson_error_t error; bool r; client = mongoc_client_new(gTestUri); ASSERT (client); collection = mongoc_client_get_collection(client, "test", "test"); ASSERT (collection); r = mongoc_collection_drop(collection, &error); assert(r == true); r = mongoc_collection_drop(collection, &error); assert(r == false); mongoc_collection_destroy(collection); mongoc_client_destroy(client); }
static void test_validate (void) { mongoc_collection_t *collection; mongoc_client_t *client; bson_iter_t iter; bson_error_t error; bson_t doc = BSON_INITIALIZER; bson_t opts = BSON_INITIALIZER; bson_t reply; bool r; client = mongoc_client_new (gTestUri); ASSERT (client); collection = get_test_collection (client, "test_validate"); ASSERT (collection); r = mongoc_collection_insert(collection, MONGOC_INSERT_NONE, &doc, NULL, &error); assert (r); BSON_APPEND_BOOL (&opts, "full", true); r = mongoc_collection_validate (collection, &opts, &reply, &error); assert (r); assert (bson_iter_init_find (&iter, &reply, "ns")); assert (bson_iter_init_find (&iter, &reply, "valid")); bson_destroy (&reply); bson_reinit (&opts); BSON_APPEND_UTF8 (&opts, "full", "bad_value"); r = mongoc_collection_validate (collection, &opts, &reply, &error); assert (!r); assert (error.domain == MONGOC_ERROR_BSON); assert (error.code == MONGOC_ERROR_BSON_INVALID); r = mongoc_collection_drop (collection, &error); assert (r); mongoc_collection_destroy (collection); mongoc_client_destroy (client); bson_destroy (&doc); bson_destroy (&opts); }
static void test_index (void) { mongoc_collection_t *collection; mongoc_database_t *database; mongoc_client_t *client; mongoc_index_opt_t opt; bson_error_t error; bool r; bson_t keys; mongoc_index_opt_init(&opt); client = mongoc_client_new(gTestUri); ASSERT (client); database = get_test_database (client); ASSERT (database); collection = get_test_collection (client, "test_index"); ASSERT (collection); bson_init(&keys); bson_append_int32(&keys, "hello", -1, 1); r = mongoc_collection_create_index(collection, &keys, &opt, &error); ASSERT (r); r = mongoc_collection_create_index(collection, &keys, &opt, &error); ASSERT (r); r = mongoc_collection_drop_index(collection, "hello_1", &error); ASSERT (r); bson_destroy(&keys); r = mongoc_collection_drop (collection, &error); ASSERT (r); mongoc_collection_destroy(collection); mongoc_database_destroy(database); mongoc_client_destroy(client); }
static void test_save (void) { mongoc_collection_t *collection; mongoc_client_t *client; bson_context_t *context; bson_error_t error; bool r; bson_oid_t oid; unsigned i; bson_t b; client = mongoc_client_new(gTestUri); ASSERT (client); collection = mongoc_client_get_collection(client, "test", "test"); ASSERT (collection); mongoc_collection_drop(collection, &error); context = bson_context_new(BSON_CONTEXT_NONE); ASSERT (context); for (i = 0; i < 10; i++) { bson_init(&b); bson_oid_init(&oid, context); bson_append_oid(&b, "_id", 3, &oid); bson_append_utf8(&b, "hello", 5, "/world", 5); r = mongoc_collection_save(collection, &b, NULL, &error); if (!r) { MONGOC_WARNING("%s\n", error.message); } ASSERT (r); bson_destroy(&b); } bson_destroy (&b); mongoc_collection_destroy(collection); bson_context_destroy(context); mongoc_client_destroy(client); }
static void test_create_collection (void) { mongoc_database_t *database; mongoc_collection_t *collection; mongoc_client_t *client; bson_error_t error = { 0 }; bson_t options; char *dbname; char *name; bool r; client = mongoc_client_new (gTestUri); assert (client); dbname = gen_collection_name ("dbtest"); database = mongoc_client_get_database (client, dbname); assert (database); bson_free (dbname); bson_init (&options); BSON_APPEND_INT32 (&options, "size", 1234); BSON_APPEND_INT32 (&options, "max", 4567); BSON_APPEND_BOOL (&options, "capped", true); BSON_APPEND_BOOL (&options, "autoIndexId", true); name = gen_collection_name ("create_collection"); collection = mongoc_database_create_collection (database, name, &options, &error); assert (collection); bson_free (name); r = mongoc_collection_drop (collection, &error); assert (r); r = mongoc_database_drop (database, &error); assert (r); mongoc_collection_destroy (collection); mongoc_database_destroy (database); mongoc_client_destroy (client); }
int lua_mongo_collection_drop (lua_State *L) { collection_t *collection; bson_error_t error; bool ret; collection = (collection_t *)luaL_checkudata(L, 1, "lua_mongoc_collection"); ret = mongoc_collection_drop(collection->c_collection, &error); if (ret) { lua_pushboolean(L, ret); } else { // Ignore error if dropping collection that does not exist. if (strcmp(error.message, "ns not found") != 0) { luaL_error(L, error.message); } } return 1; }
static void test_load (mongoc_client_t *client, unsigned iterations) { mongoc_collection_t *col; mongoc_database_t *db; bson_error_t error; unsigned i; bson_t b; bson_t q; bson_init(&b); bson_append_int32(&b, "ping", 4, 1); bson_init(&q); db = mongoc_client_get_database(client, "admin"); col = mongoc_client_get_collection(client, "test", "test"); for (i = 0; i < iterations; i++) { ping(db, &b); fetch(col, &q); } if (!mongoc_collection_drop(col, &error)) { MONGOC_WARNING("Failed to drop collection: %s", error.message); } mongoc_database_destroy(db); db = mongoc_client_get_database(client, "test"); if (!mongoc_database_drop(db, &error)) { MONGOC_WARNING("Failed to drop database: %s", error.message); } mongoc_database_destroy(db); mongoc_collection_destroy(col); bson_destroy(&b); }
static void test_stats (void) { mongoc_collection_t *collection; mongoc_client_t *client; bson_error_t error; bson_iter_t iter; bson_t stats; bson_t doc = BSON_INITIALIZER; bool r; client = mongoc_client_new (gTestUri); ASSERT (client); collection = get_test_collection (client, "test_stats"); ASSERT (collection); r = mongoc_collection_insert (collection, MONGOC_INSERT_NONE, &doc, NULL, &error); assert (r); r = mongoc_collection_stats (collection, NULL, &stats, &error); assert (r); assert (bson_iter_init_find (&iter, &stats, "ns")); assert (bson_iter_init_find (&iter, &stats, "count")); assert (bson_iter_as_int64 (&iter) >= 1); bson_destroy (&stats); r = mongoc_collection_drop (collection, &error); assert (r); mongoc_collection_destroy (collection); mongoc_client_destroy (client); bson_destroy (&doc); }
static void test_find_and_modify (void) { mongoc_collection_t *collection; mongoc_client_t *client; bson_error_t error; bson_iter_t iter; bson_iter_t citer; bson_t *update; bson_t doc = BSON_INITIALIZER; bson_t reply; mongoc_find_and_modify_opts_t *opts; client = test_framework_client_new (); ASSERT (client); collection = get_test_collection (client, "test_find_and_modify"); ASSERT (collection); BSON_APPEND_INT32 (&doc, "superduper", 77889); ASSERT_OR_PRINT (mongoc_collection_insert ( collection, MONGOC_INSERT_NONE, &doc, NULL, &error), error); update = BCON_NEW ("$set", "{", "superduper", BCON_INT32 (1234), "}"); opts = mongoc_find_and_modify_opts_new (); mongoc_find_and_modify_opts_set_update (opts, update); mongoc_find_and_modify_opts_set_fields (opts, tmp_bson ("{'superduper': 1}")); mongoc_find_and_modify_opts_set_sort (opts, tmp_bson ("{'superduper': 1}")); mongoc_find_and_modify_opts_set_flags (opts, MONGOC_FIND_AND_MODIFY_RETURN_NEW); ASSERT_OR_PRINT (mongoc_collection_find_and_modify_with_opts ( collection, &doc, opts, &reply, &error), error); assert (bson_iter_init_find (&iter, &reply, "value")); assert (BSON_ITER_HOLDS_DOCUMENT (&iter)); assert (bson_iter_recurse (&iter, &citer)); assert (bson_iter_find (&citer, "superduper")); assert (BSON_ITER_HOLDS_INT32 (&citer)); assert (bson_iter_int32 (&citer) == 1234); assert (bson_iter_init_find (&iter, &reply, "lastErrorObject")); assert (BSON_ITER_HOLDS_DOCUMENT (&iter)); assert (bson_iter_recurse (&iter, &citer)); assert (bson_iter_find (&citer, "updatedExisting")); assert (BSON_ITER_HOLDS_BOOL (&citer)); assert (bson_iter_bool (&citer)); bson_destroy (&reply); bson_destroy (update); ASSERT_OR_PRINT (mongoc_collection_drop (collection, &error), error); mongoc_find_and_modify_opts_destroy (opts); mongoc_collection_destroy (collection); mongoc_client_destroy (client); bson_destroy (&doc); }
static void test_bulk (void) { mongoc_bulk_operation_t *bulk; mongoc_collection_t *collection; mongoc_client_t *client; bson_error_t error; bson_iter_t iter; bson_t reply; bson_t child; bson_t del; bson_t up; bson_t doc = BSON_INITIALIZER; bool r; client = mongoc_client_new (gTestUri); assert (client); collection = get_test_collection (client, "test_bulk"); assert (collection); bulk = mongoc_collection_create_bulk_operation (collection, true, NULL); assert (bulk); mongoc_bulk_operation_insert (bulk, &doc); mongoc_bulk_operation_insert (bulk, &doc); mongoc_bulk_operation_insert (bulk, &doc); mongoc_bulk_operation_insert (bulk, &doc); bson_init (&up); bson_append_document_begin (&up, "$set", -1, &child); bson_append_int32 (&child, "hello", -1, 123); bson_append_document_end (&up, &child); mongoc_bulk_operation_update (bulk, &doc, &up, false); bson_destroy (&up); bson_init (&del); BSON_APPEND_INT32 (&del, "hello", 123); mongoc_bulk_operation_delete (bulk, &del); bson_destroy (&del); r = mongoc_bulk_operation_execute (bulk, &reply, &error); assert (r); assert (bson_iter_init_find (&iter, &reply, "nInserted")); assert (BSON_ITER_HOLDS_INT32 (&iter)); assert (bson_iter_int32 (&iter) == 4); /* * This may be omitted if we talked to a (<= 2.4.x) node, or a mongos * talked to a (<= 2.4.x) node. */ if (bson_iter_init_find (&iter, &reply, "nModified")) { assert (BSON_ITER_HOLDS_INT32 (&iter)); assert (bson_iter_int32 (&iter) == 4); } assert (bson_iter_init_find (&iter, &reply, "nRemoved")); assert (BSON_ITER_HOLDS_INT32 (&iter)); assert (4 == bson_iter_int32 (&iter)); assert (bson_iter_init_find (&iter, &reply, "nMatched")); assert (BSON_ITER_HOLDS_INT32 (&iter)); assert (4 == bson_iter_int32 (&iter)); assert (bson_iter_init_find (&iter, &reply, "nUpserted")); assert (BSON_ITER_HOLDS_INT32 (&iter)); assert (!bson_iter_int32 (&iter)); bson_destroy (&reply); r = mongoc_collection_drop (collection, &error); assert (r); mongoc_bulk_operation_destroy (bulk); mongoc_collection_destroy (collection); mongoc_client_destroy (client); bson_destroy (&doc); }
static void test_split_insert (void) { mongoc_bulk_write_flags_t write_flags = MONGOC_BULK_WRITE_FLAGS_INIT; mongoc_write_command_t command; mongoc_write_result_t result; mongoc_collection_t *collection; mongoc_client_t *client; bson_oid_t oid; bson_t **docs; bson_t reply = BSON_INITIALIZER; bson_error_t error; mongoc_server_stream_t *server_stream; int i; bool r; client = test_framework_client_new (); assert (client); collection = get_test_collection (client, "test_split_insert"); assert (collection); docs = (bson_t **)bson_malloc (sizeof(bson_t*) * 3000); for (i = 0; i < 3000; i++) { docs [i] = bson_new (); bson_oid_init (&oid, NULL); BSON_APPEND_OID (docs [i], "_id", &oid); } _mongoc_write_result_init (&result); _mongoc_write_command_init_insert (&command, docs[0], write_flags, ++client->cluster.operation_id, true); for (i = 1; i < 3000; i++) { _mongoc_write_command_insert_append (&command, docs[i]); } server_stream = mongoc_cluster_stream_for_writes (&client->cluster, &error); ASSERT_OR_PRINT (server_stream, error); _mongoc_write_command_execute (&command, client, server_stream, collection->db, collection->collection, NULL, 0, &result); r = _mongoc_write_result_complete (&result, 2, collection->write_concern, &reply, &error); ASSERT_OR_PRINT (r, error); assert (result.nInserted == 3000); _mongoc_write_command_destroy (&command); _mongoc_write_result_destroy (&result); ASSERT_OR_PRINT (mongoc_collection_drop (collection, &error), error); for (i = 0; i < 3000; i++) { bson_destroy (docs [i]); } bson_free (docs); mongoc_server_stream_cleanup (server_stream); mongoc_collection_destroy (collection); mongoc_client_destroy (client); }
static void test_bypass_validation (void *context) { mongoc_collection_t *collection; bson_t reply = BSON_INITIALIZER; mongoc_bulk_operation_t *bulk; mongoc_database_t *database; mongoc_write_concern_t *wr; mongoc_client_t *client; bson_error_t error; bson_t *options; char *collname; char *dbname; int r; int i; client = test_framework_client_new (); assert (client); dbname = gen_collection_name ("dbtest"); collname = gen_collection_name ("bypass"); database = mongoc_client_get_database (client, dbname); collection = mongoc_database_get_collection (database, collname); assert (collection); options = tmp_bson ("{'validator': {'number': {'$gte': 5}}, 'validationAction': 'error'}"); ASSERT_OR_PRINT (mongoc_database_create_collection (database, collname, options, &error), error); /* {{{ Default fails validation */ bulk = mongoc_collection_create_bulk_operation(collection, true, NULL); for (i = 0; i < 3; i++) { bson_t *doc = tmp_bson (bson_strdup_printf ("{'number': 3, 'high': %d }", i)); mongoc_bulk_operation_insert (bulk, doc); } r = mongoc_bulk_operation_execute (bulk, &reply, &error); ASSERT(!r); ASSERT_ERROR_CONTAINS (error, MONGOC_ERROR_COMMAND, 121, "Document failed validation"); mongoc_bulk_operation_destroy (bulk); /* }}} */ /* {{{ bypass_document_validation=false Fails validation */ bulk = mongoc_collection_create_bulk_operation(collection, true, NULL); mongoc_bulk_operation_set_bypass_document_validation (bulk, false); for (i = 0; i < 3; i++) { bson_t *doc = tmp_bson (bson_strdup_printf ("{'number': 3, 'high': %d }", i)); mongoc_bulk_operation_insert (bulk, doc); } r = mongoc_bulk_operation_execute (bulk, &reply, &error); ASSERT(!r); ASSERT_ERROR_CONTAINS (error, MONGOC_ERROR_COMMAND, 121, "Document failed validation"); mongoc_bulk_operation_destroy (bulk); /* }}} */ /* {{{ bypass_document_validation=true ignores validation */ bulk = mongoc_collection_create_bulk_operation(collection, true, NULL); mongoc_bulk_operation_set_bypass_document_validation (bulk, true); for (i = 0; i < 3; i++) { bson_t *doc = tmp_bson (bson_strdup_printf ("{'number': 3, 'high': %d }", i)); mongoc_bulk_operation_insert (bulk, doc); } r = mongoc_bulk_operation_execute (bulk, &reply, &error); ASSERT_OR_PRINT(r, error); mongoc_bulk_operation_destroy (bulk); /* }}} */ /* {{{ w=0 and bypass_document_validation=set fails */ bulk = mongoc_collection_create_bulk_operation(collection, true, NULL); wr = mongoc_write_concern_new (); mongoc_write_concern_set_w (wr, 0); mongoc_bulk_operation_set_write_concern (bulk, wr); mongoc_bulk_operation_set_bypass_document_validation (bulk, true); for (i = 0; i < 3; i++) { bson_t *doc = tmp_bson (bson_strdup_printf ("{'number': 3, 'high': %d }", i)); mongoc_bulk_operation_insert (bulk, doc); } r = mongoc_bulk_operation_execute (bulk, &reply, &error); ASSERT_OR_PRINT(!r, error); ASSERT_ERROR_CONTAINS (error, MONGOC_ERROR_COMMAND, MONGOC_ERROR_COMMAND_INVALID_ARG, "Cannot set bypassDocumentValidation for unacknowledged writes"); mongoc_bulk_operation_destroy (bulk); mongoc_write_concern_destroy (wr); /* }}} */ ASSERT_OR_PRINT (mongoc_collection_drop (collection, &error), error); mongoc_collection_destroy (collection); mongoc_client_destroy (client); }
static void test_exhaust_cursor (void) { mongoc_write_concern_t *wr; mongoc_client_t *client; mongoc_collection_t *collection; mongoc_cursor_t *cursor; mongoc_cursor_t *cursor2; mongoc_stream_t *stream; mongoc_cluster_node_t *node; const bson_t *doc; bson_t q; bson_t b[10]; bson_t *bptr[10]; int i; bool r; bson_error_t error; bson_oid_t oid; client = mongoc_client_new (gTestUri); assert (client); collection = get_test_collection (client, "test_exhaust_cursor"); assert (collection); mongoc_collection_drop(collection, &error); wr = mongoc_write_concern_new (); mongoc_write_concern_set_journal (wr, true); /* bulk insert some records to work on */ { bson_init(&q); for (i = 0; i < 10; i++) { bson_init(&b[i]); bson_oid_init(&oid, NULL); bson_append_oid(&b[i], "_id", -1, &oid); bson_append_int32(&b[i], "n", -1, i % 2); bptr[i] = &b[i]; } BEGIN_IGNORE_DEPRECATIONS; r = mongoc_collection_insert_bulk (collection, MONGOC_INSERT_NONE, (const bson_t **)bptr, 10, wr, &error); END_IGNORE_DEPRECATIONS; if (!r) { MONGOC_WARNING("Insert bulk failure: %s\n", error.message); } assert(r); } /* create a couple of cursors */ { cursor = mongoc_collection_find (collection, MONGOC_QUERY_EXHAUST, 0, 0, 0, &q, NULL, NULL); cursor2 = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 0, 0, &q, NULL, NULL); } /* Read from the exhaust cursor, ensure that we're in exhaust where we * should be and ensure that an early destroy properly causes a disconnect * */ { r = mongoc_cursor_next (cursor, &doc); assert (r); assert (doc); assert (cursor->in_exhaust); assert (client->in_exhaust); node = &client->cluster.nodes[cursor->hint - 1]; stream = node->stream; mongoc_cursor_destroy (cursor); /* make sure a disconnect happened */ assert (stream != node->stream); assert (! client->in_exhaust); } /* Grab a new exhaust cursor, then verify that reading from that cursor * (putting the client into exhaust), breaks a mid-stream read from a * regular cursor */ { cursor = mongoc_collection_find (collection, MONGOC_QUERY_EXHAUST, 0, 0, 0, &q, NULL, NULL); for (i = 0; i < 5; i++) { r = mongoc_cursor_next (cursor2, &doc); assert (r); assert (doc); } r = mongoc_cursor_next (cursor, &doc); assert (r); assert (doc); doc = NULL; r = mongoc_cursor_next (cursor2, &doc); assert (!r); assert (!doc); mongoc_cursor_error(cursor2, &error); assert (error.domain == MONGOC_ERROR_CLIENT); assert (error.code == MONGOC_ERROR_CLIENT_IN_EXHAUST); mongoc_cursor_destroy (cursor2); } /* make sure writes fail as well */ { BEGIN_IGNORE_DEPRECATIONS; r = mongoc_collection_insert_bulk (collection, MONGOC_INSERT_NONE, (const bson_t **)bptr, 10, wr, &error); END_IGNORE_DEPRECATIONS; assert (!r); assert (error.domain == MONGOC_ERROR_CLIENT); assert (error.code == MONGOC_ERROR_CLIENT_IN_EXHAUST); } /* we're still in exhaust. * * 1. check that we can create a new cursor, as long as we don't read from it * 2. fully exhaust the exhaust cursor * 3. make sure that we don't disconnect at destroy * 4. make sure we can read the cursor we made during the exhuast */ { cursor2 = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 0, 0, &q, NULL, NULL); node = &client->cluster.nodes[cursor->hint - 1]; stream = node->stream; for (i = 1; i < 10; i++) { r = mongoc_cursor_next (cursor, &doc); assert (r); assert (doc); } r = mongoc_cursor_next (cursor, &doc); assert (!r); assert (!doc); mongoc_cursor_destroy (cursor); assert (stream == node->stream); r = mongoc_cursor_next (cursor2, &doc); assert (r); assert (doc); } bson_destroy(&q); for (i = 0; i < 10; i++) { bson_destroy(&b[i]); } r = mongoc_collection_drop (collection, &error); assert (r); mongoc_write_concern_destroy (wr); mongoc_cursor_destroy (cursor2); mongoc_collection_destroy(collection); mongoc_client_destroy (client); }
static void test_aggregate (void) { mongoc_collection_t *collection; mongoc_client_t *client; mongoc_cursor_t *cursor; const bson_t *doc; bson_error_t error; bool r; bson_t b; bson_t match; bson_t pipeline; bson_iter_t iter; bson_init(&b); bson_append_utf8(&b, "hello", -1, "world", -1); bson_init(&match); bson_append_document(&match, "$match", -1, &b); bson_init(&pipeline); bson_append_document(&pipeline, "0", -1, &match); client = mongoc_client_new(gTestUri); ASSERT (client); collection = mongoc_client_get_collection(client, "test", "test"); ASSERT (collection); mongoc_collection_drop(collection, &error); r = mongoc_collection_insert(collection, MONGOC_INSERT_NONE, &b, NULL, &error); ASSERT (r); cursor = mongoc_collection_aggregate(collection, MONGOC_QUERY_NONE, &pipeline, NULL); ASSERT (cursor); /* * This can fail if we are connecting to a pre-2.5.x MongoDB instance. */ r = mongoc_cursor_next(cursor, &doc); if (mongoc_cursor_error(cursor, &error)) { MONGOC_WARNING("%s", error.message); } ASSERT (r); ASSERT (doc); ASSERT (bson_iter_init_find (&iter, doc, "hello") && BSON_ITER_HOLDS_UTF8 (&iter)); r = mongoc_cursor_next(cursor, &doc); if (mongoc_cursor_error(cursor, &error)) { MONGOC_WARNING("%s", error.message); } ASSERT (!r); ASSERT (!doc); mongoc_cursor_destroy(cursor); mongoc_collection_destroy(collection); mongoc_client_destroy(client); bson_destroy(&b); bson_destroy(&pipeline); bson_destroy(&match); }
static void test_insert_bulk (void) { mongoc_collection_t *collection; mongoc_client_t *client; bson_context_t *context; bson_error_t error; bool r; bson_oid_t oid; unsigned i; bson_t q; bson_t b[10]; bson_t *bptr[10]; int64_t count; client = mongoc_client_new(gTestUri); ASSERT (client); collection = mongoc_client_get_collection(client, "test", "test"); ASSERT (collection); mongoc_collection_drop(collection, &error); context = bson_context_new(BSON_CONTEXT_NONE); ASSERT (context); bson_init(&q); bson_append_int32(&q, "n", -1, 0); for (i = 0; i < 10; i++) { bson_init(&b[i]); bson_oid_init(&oid, context); bson_append_oid(&b[i], "_id", -1, &oid); bson_append_int32(&b[i], "n", -1, i % 2); bptr[i] = &b[i]; } r = mongoc_collection_insert_bulk (collection, MONGOC_INSERT_NONE, (const bson_t **)bptr, 10, NULL, &error); if (!r) { MONGOC_WARNING("%s\n", error.message); } ASSERT (r); count = mongoc_collection_count (collection, MONGOC_QUERY_NONE, &q, 0, 0, NULL, &error); ASSERT (count == 5); for (i = 8; i < 10; i++) { bson_destroy(&b[i]); bson_init(&b[i]); bson_oid_init(&oid, context); bson_append_oid(&b[i], "_id", -1, &oid); bson_append_int32(&b[i], "n", -1, i % 2); bptr[i] = &b[i]; } r = mongoc_collection_insert_bulk (collection, MONGOC_INSERT_NONE, (const bson_t **)bptr, 10, NULL, &error); ASSERT (!r); ASSERT (error.code == 11000); count = mongoc_collection_count (collection, MONGOC_QUERY_NONE, &q, 0, 0, NULL, &error); ASSERT (count == 5); r = mongoc_collection_insert_bulk (collection, MONGOC_INSERT_CONTINUE_ON_ERROR, (const bson_t **)bptr, 10, NULL, &error); ASSERT (!r); ASSERT (error.code == 11000); count = mongoc_collection_count (collection, MONGOC_QUERY_NONE, &q, 0, 0, NULL, &error); ASSERT (count == 6); bson_destroy(&q); for (i = 0; i < 10; i++) { bson_destroy(&b[i]); } mongoc_collection_destroy(collection); bson_context_destroy(context); mongoc_client_destroy(client); }