static mongoc_cursor_t * query_collection (mongoc_collection_t *collection, uint32_t last_time) { mongoc_cursor_t *cursor; bson_t query; bson_t gt; bson_t opts; BSON_ASSERT (collection); bson_init (&query); BSON_APPEND_DOCUMENT_BEGIN (&query, "ts", >); BSON_APPEND_TIMESTAMP (>, "$gt", last_time, 0); bson_append_document_end (&query, >); bson_init (&opts); BSON_APPEND_BOOL (&opts, "tailable", true); BSON_APPEND_BOOL (&opts, "awaitData", true); cursor = mongoc_collection_find_with_opts (collection, &query, &opts, NULL); bson_destroy (&query); bson_destroy (&opts); return cursor; }
void _mongoc_write_command_update_append (mongoc_write_command_t *command, const bson_t *selector, const bson_t *update, bool upsert, bool multi) { const char *key; char keydata [16]; bson_t doc; ENTRY; BSON_ASSERT (command); BSON_ASSERT (command->type == MONGOC_WRITE_COMMAND_UPDATE); BSON_ASSERT (selector && update); bson_init (&doc); BSON_APPEND_DOCUMENT (&doc, "q", selector); BSON_APPEND_DOCUMENT (&doc, "u", update); BSON_APPEND_BOOL (&doc, "upsert", upsert); BSON_APPEND_BOOL (&doc, "multi", multi); key = NULL; bson_uint32_to_string (command->n_documents, &key, keydata, sizeof keydata); BSON_ASSERT (key); BSON_APPEND_DOCUMENT (command->documents, key, &doc); command->n_documents++; bson_destroy (&doc); EXIT; }
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); }
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); }
void mongoc_bulk_operation_update_one (mongoc_bulk_operation_t *bulk, const bson_t *selector, const bson_t *document, bool upsert) { bson_t opts; bson_error_t *error = &bulk->result.error; ENTRY; BULK_EXIT_IF_PRIOR_ERROR; bson_init (&opts); BSON_APPEND_BOOL (&opts, "upsert", upsert); mongoc_bulk_operation_update_one_with_opts ( bulk, selector, document, &opts, error); bson_destroy (&opts); if (error->domain) { MONGOC_WARNING ("%s", error->message); } EXIT; }
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); }
bool bson_append_bool_from_s (bson_t *bson, const char *key, const char *value) { bool ret = true; if (value && strcmp ("\\N", value) != 0) { strcmp ("t", value) == 0 || strcmp ("f", value) == 0 || DIE; ret = BSON_APPEND_BOOL (bson, key, (strcmp ("t", value) == 0) ? true : false); } return ret; }
/* takes initialized bson_t *doc and begins formatting a write command */ void _mongoc_write_command_init (bson_t *doc, mongoc_write_command_t *command, const char *collection) { ENTRY; if (!command->n_documents) { EXIT; } BSON_APPEND_UTF8 (doc, gCommandNames[command->type], collection); BSON_APPEND_BOOL (doc, "ordered", command->flags.ordered); if (command->flags.bypass_document_validation != MONGOC_BYPASS_DOCUMENT_VALIDATION_DEFAULT) { BSON_APPEND_BOOL (doc, "bypassDocumentValidation", !!command->flags.bypass_document_validation); } EXIT; }
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); }
void mongoc_bulk_operation_replace_one (mongoc_bulk_operation_t *bulk, const bson_t *selector, const bson_t *document, bool upsert) { bson_t opts = BSON_INITIALIZER; bson_error_t *error = &bulk->result.error; ENTRY; BSON_APPEND_BOOL (&opts, "upsert", upsert); if (!mongoc_bulk_operation_replace_one_with_opts ( bulk, selector, document, &opts, error)) { MONGOC_WARNING ("%s", error->message); } bson_destroy (&opts); EXIT; }
/** * mongoc_database_add_user: * @database: A #mongoc_database_t. * @username: A string containing the username. * @password: (allow-none): A string containing password, or NULL. * @roles: (allow-none): An optional bson_t of roles. * @custom_data: (allow-none): An optional bson_t of data to store. * @error: (out) (allow-none): A location for a bson_error_t or %NULL. * * Creates a new user with access to @database. * * Returns: None. * Side effects: None. */ bool mongoc_database_add_user (mongoc_database_t *database, const char *username, const char *password, const bson_t *roles, const bson_t *custom_data, bson_error_t *error) { bson_error_t lerror; bson_t cmd; bson_t ar; char *input; char *hashed_password; bool ret = false; ENTRY; BSON_ASSERT (database); BSON_ASSERT (username); /* * CDRIVER-232: * * Perform a (slow and tedious) round trip to mongod to determine if * we can safely call createUser. Otherwise, we will fallback and * perform legacy insertion into users collection. */ bson_init (&cmd); BSON_APPEND_UTF8 (&cmd, "usersInfo", username); ret = mongoc_database_command_simple (database, &cmd, NULL, NULL, &lerror); bson_destroy (&cmd); if (!ret && (lerror.code == MONGOC_ERROR_QUERY_COMMAND_NOT_FOUND)) { ret = mongoc_database_add_user_legacy (database, username, password, error); } else if (ret || (lerror.code == 13)) { /* usersInfo succeeded or failed with auth err, we're on modern mongod */ input = bson_strdup_printf ("%s:mongo:%s", username, password); hashed_password = _mongoc_hex_md5 (input); bson_free (input); bson_init (&cmd); BSON_APPEND_UTF8 (&cmd, "createUser", username); BSON_APPEND_UTF8 (&cmd, "pwd", hashed_password); BSON_APPEND_BOOL (&cmd, "digestPassword", false); if (custom_data) { BSON_APPEND_DOCUMENT (&cmd, "customData", custom_data); } if (roles) { BSON_APPEND_ARRAY (&cmd, "roles", roles); } else { bson_append_array_begin (&cmd, "roles", 5, &ar); bson_append_array_end (&cmd, &ar); } ret = mongoc_database_command_simple (database, &cmd, NULL, NULL, error); bson_free (hashed_password); bson_destroy (&cmd); } else if (error) { memcpy (error, &lerror, sizeof *error); } RETURN (ret); }
static void _mongoc_write_command(mongoc_write_command_t *command, mongoc_client_t *client, mongoc_server_stream_t *server_stream, const char *database, const char *collection, const mongoc_write_concern_t *write_concern, uint32_t offset, mongoc_write_result_t *result, bson_error_t *error) { const uint8_t *data; bson_iter_t iter; const char *key; uint32_t len = 0; bson_t tmp; bson_t ar; bson_t cmd; bson_t reply; char str [16]; bool has_more; bool ret = false; uint32_t i; int32_t max_bson_obj_size; int32_t max_write_batch_size; int32_t min_wire_version; uint32_t key_len; ENTRY; BSON_ASSERT (command); BSON_ASSERT (client); BSON_ASSERT (database); BSON_ASSERT (server_stream); BSON_ASSERT (collection); max_bson_obj_size = mongoc_server_stream_max_bson_obj_size (server_stream); max_write_batch_size = mongoc_server_stream_max_write_batch_size (server_stream); /* * If we have an unacknowledged write and the server supports the legacy * opcodes, then submit the legacy opcode so we don't need to wait for * a response from the server. */ min_wire_version = server_stream->sd->min_wire_version; if ((min_wire_version == 0) && !_mongoc_write_concern_needs_gle (write_concern)) { if (command->flags.bypass_document_validation != MONGOC_BYPASS_DOCUMENT_VALIDATION_DEFAULT) { bson_set_error (error, MONGOC_ERROR_COMMAND, MONGOC_ERROR_COMMAND_INVALID_ARG, "Cannot set bypassDocumentValidation for unacknowledged writes"); EXIT; } gLegacyWriteOps[command->type] (command, client, server_stream, database, collection, write_concern, offset, result, error); EXIT; } if (!command->n_documents || !bson_iter_init (&iter, command->documents) || !bson_iter_next (&iter)) { _empty_error (command, error); result->failed = true; EXIT; } again: bson_init (&cmd); has_more = false; i = 0; BSON_APPEND_UTF8 (&cmd, gCommandNames[command->type], collection); BSON_APPEND_DOCUMENT (&cmd, "writeConcern", WRITE_CONCERN_DOC (write_concern)); BSON_APPEND_BOOL (&cmd, "ordered", command->flags.ordered); if (command->flags.bypass_document_validation != MONGOC_BYPASS_DOCUMENT_VALIDATION_DEFAULT) { BSON_APPEND_BOOL (&cmd, "bypassDocumentValidation", !!command->flags.bypass_document_validation); } if (!_mongoc_write_command_will_overflow (0, command->documents->len, command->n_documents, max_bson_obj_size, max_write_batch_size)) { /* copy the whole documents buffer as e.g. "updates": [...] */ BSON_APPEND_ARRAY (&cmd, gCommandFields[command->type], command->documents); i = command->n_documents; } else { bson_append_array_begin (&cmd, gCommandFields[command->type], -1, &ar); do { if (!BSON_ITER_HOLDS_DOCUMENT (&iter)) { BSON_ASSERT (false); } bson_iter_document (&iter, &len, &data); key_len = (uint32_t) bson_uint32_to_string (i, &key, str, sizeof str); if (_mongoc_write_command_will_overflow (ar.len, key_len + len + 2, i, max_bson_obj_size, max_write_batch_size)) { has_more = true; break; } if (!bson_init_static (&tmp, data, len)) { BSON_ASSERT (false); } BSON_APPEND_DOCUMENT (&ar, key, &tmp); bson_destroy (&tmp); i++; } while (bson_iter_next (&iter)); bson_append_array_end (&cmd, &ar); } if (!i) { too_large_error (error, i, len, max_bson_obj_size, NULL); result->failed = true; ret = false; } else { ret = mongoc_cluster_run_command (&client->cluster, server_stream->stream, MONGOC_QUERY_NONE, database, &cmd, &reply, error); if (!ret) { result->failed = true; } _mongoc_write_result_merge (result, command, &reply, offset); offset += i; bson_destroy (&reply); } bson_destroy (&cmd); if (has_more && (ret || !command->flags.ordered)) { GOTO (again); } EXIT; }
void _mongoc_matcher_op_to_bson (mongoc_matcher_op_t *op, /* IN */ bson_t *bson) /* IN */ { const char *str; bson_t child; bson_t child2; BSON_ASSERT (op); BSON_ASSERT (bson); switch (op->base.opcode) { case MONGOC_MATCHER_OPCODE_EQ: bson_append_iter (bson, op->compare.path, -1, &op->compare.iter); break; case MONGOC_MATCHER_OPCODE_GT: case MONGOC_MATCHER_OPCODE_GTE: case MONGOC_MATCHER_OPCODE_IN: case MONGOC_MATCHER_OPCODE_LT: case MONGOC_MATCHER_OPCODE_LTE: case MONGOC_MATCHER_OPCODE_NE: case MONGOC_MATCHER_OPCODE_NIN: switch ((int)op->base.opcode) { case MONGOC_MATCHER_OPCODE_GT: str = "$gt"; break; case MONGOC_MATCHER_OPCODE_GTE: str = "$gte"; break; case MONGOC_MATCHER_OPCODE_IN: str = "$in"; break; case MONGOC_MATCHER_OPCODE_LT: str = "$lt"; break; case MONGOC_MATCHER_OPCODE_LTE: str = "$lte"; break; case MONGOC_MATCHER_OPCODE_NE: str = "$ne"; break; case MONGOC_MATCHER_OPCODE_NIN: str = "$nin"; break; default: str = "???"; break; } bson_append_document_begin (bson, op->compare.path, -1, &child); bson_append_iter (&child, str, -1, &op->compare.iter); bson_append_document_end (bson, &child); break; case MONGOC_MATCHER_OPCODE_OR: case MONGOC_MATCHER_OPCODE_AND: case MONGOC_MATCHER_OPCODE_NOR: if (op->base.opcode == MONGOC_MATCHER_OPCODE_OR) { str = "$or"; } else if (op->base.opcode == MONGOC_MATCHER_OPCODE_AND) { str = "$and"; } else if (op->base.opcode == MONGOC_MATCHER_OPCODE_NOR) { str = "$nor"; } else { BSON_ASSERT (false); str = NULL; } bson_append_array_begin (bson, str, -1, &child); bson_append_document_begin (&child, "0", 1, &child2); _mongoc_matcher_op_to_bson (op->logical.left, &child2); bson_append_document_end (&child, &child2); if (op->logical.right) { bson_append_document_begin (&child, "1", 1, &child2); _mongoc_matcher_op_to_bson (op->logical.right, &child2); bson_append_document_end (&child, &child2); } bson_append_array_end (bson, &child); break; case MONGOC_MATCHER_OPCODE_NOT: bson_append_document_begin (bson, op->not.path, -1, &child); bson_append_document_begin (&child, "$not", 4, &child2); _mongoc_matcher_op_to_bson (op->not.child, &child2); bson_append_document_end (&child, &child2); bson_append_document_end (bson, &child); break; case MONGOC_MATCHER_OPCODE_EXISTS: BSON_APPEND_BOOL (bson, "$exists", op->exists.exists); break; case MONGOC_MATCHER_OPCODE_TYPE: BSON_APPEND_INT32 (bson, "$type", (int)op->type.type); break; default: BSON_ASSERT (false); break; } }
void switch_value(lua_State *L, int index, bson_t* bson, int level, const char* key) { switch(lua_type(L, index)) { case LUA_TTABLE: { int is_a=is_array(L, index); if (is_a) { bson_t child; //start array BSON_APPEND_ARRAY_BEGIN(bson, key, &child); iterate_table(L, index, &child, 0, level+1, NULL); bson_append_array_end(bson, &child); } else { bson_t child; //start map BSON_APPEND_DOCUMENT_BEGIN(bson, key, &child); iterate_table(L, index, &child, 1, level+1, NULL); bson_append_document_end(bson, &child); } break; } case LUA_TNIL: { BSON_APPEND_NULL(bson, key); break; } case LUA_TNUMBER: { BSON_APPEND_DOUBLE(bson, key, lua_tonumber(L, index)); break; } case LUA_TBOOLEAN: { BSON_APPEND_BOOL(bson, key, lua_toboolean(L, index)); break; } case LUA_TSTRING: { BSON_APPEND_UTF8(bson, key, lua_tostring(L, index)); break; } case LUA_TUSERDATA: { // switch userdata type if (luaL_checkudata_ex(L, index, REGEX_METATABLE)) { cbson_regex_t* regex = check_cbson_regex(L, index); BSON_APPEND_REGEX (bson, key, regex->regex, regex->options); } else if (luaL_checkudata_ex(L, index, OID_METATABLE)) { cbson_oid_t* oid = check_cbson_oid(L, index); bson_oid_t boid; bson_oid_init_from_string (&boid, oid->oid); BSON_APPEND_OID (bson, key, &boid); } else if (luaL_checkudata_ex(L, index, BINARY_METATABLE)) { cbson_binary_t* bin = check_cbson_binary(L, index); size_t binary_len = b64_pton (bin->data, NULL, 0); unsigned char* buf=malloc(binary_len+1); b64_pton(bin->data, buf, binary_len+1); BSON_APPEND_BINARY(bson, key, bin->type, buf, binary_len); free(buf); } else if (luaL_checkudata_ex(L, index, SYMBOL_METATABLE)) { cbson_symbol_t* sym = check_cbson_symbol(L, index); BSON_APPEND_SYMBOL(bson, key, sym->symbol); } else if (luaL_checkudata_ex(L, index, REF_METATABLE)) { cbson_ref_t* ref = check_cbson_ref(L, index); bson_oid_t boid; bson_oid_init_from_string (&boid, ref->id); BSON_APPEND_DBPOINTER(bson, key, ref->ref, &boid); } else if (luaL_checkudata_ex(L, index, MINKEY_METATABLE)) { check_cbson_minkey(L, index); BSON_APPEND_MINKEY(bson, key); } else if (luaL_checkudata_ex(L, index, MAXKEY_METATABLE)) { check_cbson_maxkey(L, index); BSON_APPEND_MAXKEY(bson, key); } else if (luaL_checkudata_ex(L, index, TIMESTAMP_METATABLE)) { cbson_timestamp_t* time = check_cbson_timestamp(L, index); BSON_APPEND_TIMESTAMP(bson, key, time->timestamp, time->increment); } else if (luaL_checkudata_ex(L, index, INT64_METATABLE) || luaL_checkudata_ex(L, index, UINT64_METATABLE)) { cbson_int64_t i = cbson_int64_check(L, index); if (i < INT32_MIN || i > INT32_MAX) { BSON_APPEND_INT64(bson, key, i); } else { BSON_APPEND_INT32(bson, key, (int32_t)i); } } else if (luaL_checkudata_ex(L, index, CODE_METATABLE)) { cbson_code_t* code = check_cbson_code(L, index); BSON_APPEND_CODE(bson, key, code->code); } else if (luaL_checkudata_ex(L, index, CODEWSCOPE_METATABLE)) { cbson_codewscope_t* code = check_cbson_codewscope(L, index); BSON_APPEND_CODE_WITH_SCOPE(bson, key, code->code, NULL); } else if (luaL_checkudata_ex(L, index, UNDEFINED_METATABLE)) { check_cbson_undefined(L, index); BSON_APPEND_UNDEFINED(bson, key); } else if (luaL_checkudata_ex(L, index, DATE_METATABLE)) { BSON_APPEND_DATE_TIME(bson, key, cbson_date_check(L, index)); } break; } case LUA_TFUNCTION: case LUA_TTHREAD: case LUA_TLIGHTUSERDATA: default: break; // or bail out? } }
static void test_aggregate (void) { mongoc_collection_t *collection; mongoc_database_t *database; mongoc_client_t *client; mongoc_cursor_t *cursor; const bson_t *doc; bson_error_t error; bool r; bson_t b; bson_t opts; bson_t match; bson_t pipeline; bson_iter_t iter; int i; 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); database = get_test_database (client); ASSERT (database); collection = get_test_collection (client, "test_aggregate"); ASSERT (collection); mongoc_collection_drop(collection, &error); r = mongoc_collection_insert(collection, MONGOC_INSERT_NONE, &b, NULL, &error); ASSERT (r); for (i = 0; i < 2; i++) { if (i % 2 == 0) { cursor = mongoc_collection_aggregate(collection, MONGOC_QUERY_NONE, &pipeline, NULL, NULL); ASSERT (cursor); } else { bson_init (&opts); BSON_APPEND_INT32 (&opts, "batchSize", 10); BSON_APPEND_BOOL (&opts, "allowDiskUse", true); cursor = mongoc_collection_aggregate(collection, MONGOC_QUERY_NONE, &pipeline, &opts, NULL); ASSERT (cursor); bson_destroy (&opts); } /* * This can fail if we are connecting to a 2.0 MongoDB instance. */ r = mongoc_cursor_next(cursor, &doc); if (mongoc_cursor_error(cursor, &error)) { if ((error.domain == MONGOC_ERROR_QUERY) && (error.code == MONGOC_ERROR_QUERY_COMMAND_NOT_FOUND)) { mongoc_cursor_destroy (cursor); break; } MONGOC_WARNING("[%d.%d] %s", error.domain, error.code, 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); } r = mongoc_collection_drop(collection, &error); ASSERT (r); mongoc_collection_destroy(collection); mongoc_database_destroy(database); mongoc_client_destroy(client); bson_destroy(&b); bson_destroy(&pipeline); bson_destroy(&match); }
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 bool appendBsonValue(bson_t *bson, const QString &key, const QVariant &value) { const QLatin1String oidkey("_id"); bool ok = true; int type = value.type(); // _id if (key == oidkey) { QByteArray oidVal = value.toByteArray(); if (oidVal.length() == 24) { // ObjectId bson_oid_t oid; bson_oid_init_from_string(&oid, oidVal.data()); BSON_APPEND_OID(bson, oidkey.latin1(), &oid); } else { int id = value.toInt(&ok); if (ok) { BSON_APPEND_INT32(bson, oidkey.latin1(), id); } else { BSON_APPEND_UTF8(bson, oidkey.latin1(), value.toString().toUtf8().data()); } } return true; } switch (type) { case QVariant::Int: BSON_APPEND_INT32(bson, qPrintable(key), value.toInt(&ok)); break; case QVariant::String: BSON_APPEND_UTF8(bson, qPrintable(key), value.toString().toUtf8().data()); break; case QVariant::LongLong: BSON_APPEND_INT64(bson, qPrintable(key), value.toLongLong(&ok)); break; case QVariant::Map: BSON_APPEND_DOCUMENT(bson, qPrintable(key), (const bson_t *)TBson::toBson(value.toMap()).constData()); break; case QVariant::Double: BSON_APPEND_DOUBLE(bson, qPrintable(key), value.toDouble(&ok)); break; case QVariant::Bool: BSON_APPEND_BOOL(bson, qPrintable(key), value.toBool()); break; case QVariant::DateTime: { #if QT_VERSION >= 0x040700 BSON_APPEND_DATE_TIME(bson, qPrintable(key), value.toDateTime().toMSecsSinceEpoch()); #else QDateTime utcDate = value.toDateTime().toUTC(); qint64 ms = utcDate.time().msec(); qint64 tm = utcDate.toTime_t() * 1000LL; if (ms > 0) { tm += ms; } BSON_APPEND_DATE_TIME(bson, qPrintable(key), tm); #endif break; } case QVariant::ByteArray: { QByteArray ba = value.toByteArray(); BSON_APPEND_BINARY(bson, qPrintable(key), BSON_SUBTYPE_BINARY, (uint8_t *)ba.constData(), ba.length()); break; } case QVariant::List: // FALL THROUGH case QVariant::StringList: { bson_t child; BSON_APPEND_ARRAY_BEGIN(bson, qPrintable(key), &child); int i = 0; for (auto &var : value.toList()) { appendBsonValue(&child, QString::number(i++), var); } bson_append_array_end(bson, &child); break; } case QVariant::Invalid: BSON_APPEND_UNDEFINED(bson, qPrintable(key)); break; default: tError("toBson() failed to convert name:%s type:%d", qPrintable(key), type); ok = false; break; } return ok; }