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; }
int main (int argc, char *argv[]) { bson_t *options; bson_error_t error; mongoc_client_t *client; mongoc_collection_t *collection; mongoc_database_t *database; mongoc_init (); client = mongoc_client_new ("mongodb://localhost/"); database = mongoc_client_get_database (client, "testasdf"); /* Create schema validator */ options = BCON_NEW ("validator", "{", "number", "{", "$gte", BCON_INT32 (5), "}", "}"); collection = mongoc_database_create_collection (database, "collname", options, &error); if (collection) { bulk5_fail (collection); bulk5_success (collection); mongoc_collection_destroy (collection); } else { fprintf(stderr, "Couldn't create collection: '%s'\n", error.message); } bson_free (options); mongoc_database_destroy (database); mongoc_client_destroy (client); mongoc_cleanup (); return 0; }
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_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); }
static void test_get_collection_names (void) { mongoc_database_t *database; mongoc_collection_t *collection; mongoc_client_t *client; bson_error_t error = { 0 }; bson_t options; int r; int namecount = 0; char **names; char **name; char *curname; char *dbname; char *name1; char *name2; char *name3; char *name4; char *name5; const char *system_prefix = "system."; 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); name1 = gen_collection_name ("name1"); name2 = gen_collection_name ("name2"); name3 = gen_collection_name ("name3"); name4 = gen_collection_name ("name4"); name5 = gen_collection_name ("name5"); collection = mongoc_database_create_collection (database, name1, &options, &error); assert (collection); mongoc_collection_destroy (collection); collection = mongoc_database_create_collection (database, name2, &options, &error); assert (collection); mongoc_collection_destroy (collection); collection = mongoc_database_create_collection (database, name3, &options, &error); assert (collection); mongoc_collection_destroy (collection); collection = mongoc_database_create_collection (database, name4, &options, &error); assert (collection); mongoc_collection_destroy (collection); collection = mongoc_database_create_collection (database, name5, &options, &error); assert (collection); mongoc_collection_destroy (collection); names = mongoc_database_get_collection_names (database, &error); assert (!error.domain); assert (!error.code); for (name = names; *name; ++name) { /* inefficient, but OK for a unit test. */ curname = *name; if (0 == strcmp (curname, name1) || 0 == strcmp (curname, name2) || 0 == strcmp (curname, name3) || 0 == strcmp (curname, name4) || 0 == strcmp (curname, name5)) { ++namecount; } else if (0 == strncmp (curname, system_prefix, strlen (system_prefix))) { /* Collections prefixed with 'system.' are system collections */ } else { assert (false); } bson_free (curname); } assert (namecount == 5); bson_free (name1); bson_free (name2); bson_free (name3); bson_free (name4); bson_free (name5); bson_free (names); r = mongoc_database_drop (database, &error); assert (r); assert (!error.domain); assert (!error.code); mongoc_database_destroy (database); mongoc_client_destroy (client); }
static void test_get_collection_info (void) { mongoc_database_t *database; mongoc_collection_t *collection; mongoc_client_t *client; bson_error_t error = { 0 }; bson_iter_t iter; bson_iter_t col_array; bson_iter_t col_iter; bson_t capped_options = BSON_INITIALIZER; bson_t autoindexid_options = BSON_INITIALIZER; bson_t noopts_options = BSON_INITIALIZER; bson_t name_filter = BSON_INITIALIZER; int r; int num_infos = 0; bson_t *infos = NULL; const char *name; char *dbname; char *capped_name; char *autoindexid_name; char *noopts_name; client = mongoc_client_new (gTestUri); assert (client); dbname = gen_collection_name ("dbtest"); database = mongoc_client_get_database (client, dbname); assert (database); bson_free (dbname); capped_name = gen_collection_name ("capped"); BSON_APPEND_BOOL (&capped_options, "capped", true); BSON_APPEND_INT32 (&capped_options, "size", 10000000); BSON_APPEND_INT32 (&capped_options, "max", 1024); autoindexid_name = gen_collection_name ("autoindexid"); BSON_APPEND_BOOL (&autoindexid_options, "autoIndexId", false); noopts_name = gen_collection_name ("noopts"); collection = mongoc_database_create_collection (database, capped_name, &capped_options, &error); assert (collection); mongoc_collection_destroy (collection); collection = mongoc_database_create_collection (database, autoindexid_name, &autoindexid_options, &error); assert (collection); mongoc_collection_destroy (collection); collection = mongoc_database_create_collection (database, noopts_name, &noopts_options, &error); assert (collection); mongoc_collection_destroy (collection); /* first we filter on collection name. */ BSON_APPEND_UTF8 (&name_filter, "name", noopts_name); /* We only test with filters since get_collection_names will * test w/o filters for us. */ /* Filter on an exact match of name */ infos = mongoc_database_get_collection_info (database, &name_filter, &error); assert (infos); assert (!error.domain); assert (!error.code); if (bson_iter_init_find (&iter, infos, "collections") && BSON_ITER_HOLDS_ARRAY (&iter) && bson_iter_recurse (&iter, &col_array)) { while (bson_iter_next (&col_array)) { if (BSON_ITER_HOLDS_DOCUMENT (&col_array) && bson_iter_recurse (&col_array, &col_iter) && bson_iter_find (&col_iter, "name") && BSON_ITER_HOLDS_UTF8 (&col_iter) && (name = bson_iter_utf8 (&col_iter, NULL))) { ++num_infos; assert (0 == strcmp (name, noopts_name)); } else { assert (false); } } } assert (1 == num_infos); num_infos = 0; bson_destroy (infos); infos = NULL; r = mongoc_database_drop (database, &error); assert (r); assert (!error.domain); assert (!error.code); bson_free (capped_name); bson_free (noopts_name); bson_free (autoindexid_name); mongoc_database_destroy (database); 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); }