void mongoc_bulk_operation_delete (mongoc_bulk_operation_t *bulk, const bson_t *selector) { ENTRY; mongoc_bulk_operation_remove (bulk, selector); EXIT; }
int lua_mongo_collection_delete_many(lua_State *L) { collection_t *collection; bson_t selector = BSON_INITIALIZER; mongoc_bulk_operation_t *bulk_remove = NULL; bson_error_t error; bson_t reply; bool throw_error = false; bool ret; collection = (collection_t *)luaL_checkudata(L, 1, "lua_mongoc_collection"); if ((lua_istable(L, 2))) { if (!(lua_table_to_bson(L, &selector, 2, false, &error))) { throw_error = true; goto DONE; } } else { luaL_error(L, "second input must be a table"); } bulk_remove = mongoc_collection_create_bulk_operation(collection->c_collection, true, NULL); mongoc_bulk_operation_remove(bulk_remove, &selector); ret = mongoc_bulk_operation_execute(bulk_remove, &reply, &error); if (!(ret)) { throw_error = true; goto DONE; } if (!(generate_DeleteResult(L, &reply, ret, &error))) { throw_error = true; goto DONE; } DONE: bson_destroy(&selector); if (bulk_remove) { mongoc_bulk_operation_destroy (bulk_remove); } if (throw_error) { luaL_error(L, error.message); } return 1; }
bool mongoc_gridfs_remove_by_filename (mongoc_gridfs_t *gridfs, const char *filename, bson_error_t *error) { mongoc_bulk_operation_t *bulk_files = NULL; mongoc_bulk_operation_t *bulk_chunks = NULL; mongoc_cursor_t *cursor = NULL; bson_error_t files_error; bson_error_t chunks_error; const bson_t *doc; const char *key; char keybuf[16]; int count = 0; bool chunks_ret; bool files_ret; bool ret = false; bson_iter_t iter; bson_t *files_q = NULL; bson_t *chunks_q = NULL; bson_t q = BSON_INITIALIZER; bson_t fields = BSON_INITIALIZER; bson_t ar = BSON_INITIALIZER; BSON_ASSERT (gridfs); if (!filename) { bson_set_error (error, MONGOC_ERROR_GRIDFS, MONGOC_ERROR_GRIDFS_INVALID_FILENAME, "A non-NULL filename must be specified."); return false; } /* * Find all files matching this filename. Hopefully just one, but not * strictly required! */ BSON_APPEND_UTF8 (&q, "filename", filename); BSON_APPEND_INT32 (&fields, "_id", 1); cursor = _mongoc_cursor_new (gridfs->client, gridfs->files->ns, MONGOC_QUERY_NONE, 0, 0, 0, false /* is command */, &q, &fields, NULL, NULL); BSON_ASSERT (cursor); while (mongoc_cursor_next (cursor, &doc)) { if (bson_iter_init_find (&iter, doc, "_id")) { const bson_value_t *value = bson_iter_value (&iter); bson_uint32_to_string (count, &key, keybuf, sizeof keybuf); BSON_APPEND_VALUE (&ar, key, value); } } if (mongoc_cursor_error (cursor, error)) { goto failure; } bulk_files = mongoc_collection_create_bulk_operation (gridfs->files, false, NULL); bulk_chunks = mongoc_collection_create_bulk_operation (gridfs->chunks, false, NULL); files_q = BCON_NEW ("_id", "{", "$in", BCON_ARRAY (&ar), "}"); chunks_q = BCON_NEW ("files_id", "{", "$in", BCON_ARRAY (&ar), "}"); mongoc_bulk_operation_remove (bulk_files, files_q); mongoc_bulk_operation_remove (bulk_chunks, chunks_q); files_ret = mongoc_bulk_operation_execute (bulk_files, NULL, &files_error); chunks_ret = mongoc_bulk_operation_execute (bulk_chunks, NULL, &chunks_error); if (error) { if (!files_ret) { memcpy (error, &files_error, sizeof *error); } else if (!chunks_ret) { memcpy (error, &chunks_error, sizeof *error); } } ret = (files_ret && chunks_ret); failure: if (cursor) { mongoc_cursor_destroy (cursor); } if (bulk_files) { mongoc_bulk_operation_destroy (bulk_files); } if (bulk_chunks) { mongoc_bulk_operation_destroy (bulk_chunks); } bson_destroy (&q); bson_destroy (&fields); bson_destroy (&ar); if (files_q) { bson_destroy (files_q); } if (chunks_q) { bson_destroy (chunks_q); } return ret; }
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_remove (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); }