개요
이 가이드 에서는 대량 쓰기 (write) 작업 을 사용하여 단일 데이터베이스 호출에서 여러 쓰기 (write) 작업을 수행하는 방법에 학습 설명합니다.
컬렉션 에 문서 삽입하고 다른 여러 문서를 업데이트 다음 문서 삭제 시나리오를 가정해 보겠습니다. 개별 함수를 사용하는 경우 각 작업에는 자체 데이터베이스 호출이 필요합니다. 대신 대량 작업을 사용하여 데이터베이스 에 대한 호출 수를 줄일 수 있습니다.
샘플 데이터
이 가이드 의 예제에서는 Atlas 샘플 데이터 세트의 sample_restaurants 데이터베이스 에 있는 restaurants 컬렉션 사용합니다. 무료 MongoDB Atlas cluster 생성하고 샘플 데이터 세트를 로드하는 방법을 학습하려면 MongoDB 시작하기 가이드를 참조하세요.
대량 쓰기 작업 시작
대량 쓰기 (write) 작업을 실행 전에 mongoc_collection_create_bulk_operation_with_opts() 함수를 호출합니다. 이 함수는 수행할 대량 쓰기에 대한 지침을 저장 데 사용할 수 있는 mongoc_bulk_operation_t 유형의 값을 반환합니다.
mongoc_collection_create_bulk_operation_with_opts() 함수는 다음 매개변수를 허용합니다.
Collection: 수정할 컬렉션 지정합니다.
옵션 문서: 작업을 사용자 지정하는 옵션을 지정하거나
NULL
다음 예시 mongoc_collection_create_bulk_operation_with_opts() 함수를 호출하고 restaurants 컬렉션 매개 변수로 전달합니다.
mongoc_bulk_operation_t *bulk = mongoc_collection_create_bulk_operation_with_opts(collection, NULL);
그런 다음 대량 작업에 쓰기 (write) 지침을 추가할 수 있습니다. 자세한 내용은 다음 쓰기 작업 정의 섹션을 참조하세요.
쓰기 작업 정의
다음 메서드를 호출하여 쓰기 (write) 작업을 정의하고 대량 쓰기 (write) 에 추가할 수 있습니다.
mongoc_bulk_operation_insert_with_opts()mongoc_bulk_operation_update_one_with_opts()mongoc_bulk_operation_update_many_with_opts()mongoc_bulk_operation_replace_one_with_opts()mongoc_bulk_operation_remove_one_with_opts()mongoc_bulk_operation_remove_many_with_opts()
다음 섹션에서는 이러한 메서드를 사용하여 해당 쓰기 (write) 작업을 지정하는 방법을 보여 줍니다.
삽입 작업
삽입 작업을 수행하려면 대량 쓰기 (write) 의 일부로 작업을 대기열에 추가하는 mongoc_bulk_operation_t에 삽입 지침을 추가합니다.
다음 예시 mongoc_bulk_operation_insert_with_opts() 함수를 호출하여 삽입할 문서 와 mongoc_bulk_operation_t 값을 매개변수로 전달합니다.
bson_t *insert_doc = BCON_NEW( "name", BCON_UTF8("Mongo's Deli"), "cuisine", BCON_UTF8("Sandwiches"), "borough", BCON_UTF8("Manhattan"), "restaurant_id", BCON_UTF8("1234") ); bson_error_t error; if (!mongoc_bulk_operation_insert_with_opts(bulk, insert_doc, NULL, &error)) { fprintf(stderr, "Failed to add insert operation: %s\n", error.message); } bson_destroy(insert_doc);
여러 문서를 삽입하려면 각 문서 에 대해 mongoc_bulk_operation_insert_with_opts() 를 호출합니다.
업데이트 작업
업데이트 작업을 수행하려면 대량 쓰기 (write) 의 일부로 작업을 대기열에 추가하는 mongoc_bulk_operation_t에 업데이트 지침을 추가합니다.
다음 예시 mongoc_bulk_operation_update_one_with_opts() 함수를 호출하여 쿼리 필터하다, 문서 업데이트 및 mongoc_bulk_operation_t 값을 매개변수로 전달합니다.
bson_t *filter_doc = BCON_NEW("name", BCON_UTF8("Mongo's Deli")); bson_t *update_doc = BCON_NEW("$set", "{", "cuisine", BCON_UTF8("Sandwiches and Salads"), "}"); bson_error_t error; if (!mongoc_bulk_operation_update_one_with_opts(bulk, filter_doc, update_doc, NULL, &error)) { fprintf(stderr, "Failed to add update operation: %s\n", error.message); } bson_destroy(filter_doc); bson_destroy(update_doc);
여러 문서를 업데이트 하려면 mongoc_bulk_operation_update_many_with_opts() 를 호출하고 동일한 매개변수를 전달합니다. 이렇게 하면 운전자 쿼리 필터하다 와 일치하는 모든 문서를 업데이트 하도록 지시합니다.
다음 예시 대량 쓰기 (write) 에 대한 업데이트 작업을 대기열에 추가합니다.
bson_t *filter_doc = BCON_NEW("name", BCON_UTF8("Mongo's Deli")); bson_t *update_doc = BCON_NEW("$set", "{", "cuisine", BCON_UTF8("Sandwiches and Salads"), "}"); bson_error_t error; if (!mongoc_bulk_operation_update_many_with_opts(bulk, filter_doc, update_doc, NULL, &error)) { fprintf(stderr, "Failed to add update operation: %s\n", error.message); } bson_destroy(filter_doc); bson_destroy(update_doc);
대체 작업
바꾸기 작업은 지정된 문서 의 모든 필드와 값을 제거하고 새 항목으로 바꿉니다. 교체 작업을 수행하려면 대량 쓰기 (write) 의 일부로 작업을 대기열에 추가하는 mongoc_bulk_operation_t에 교체 지침을 추가합니다.
다음 예시 mongoc_bulk_operation_replace_one_with_opts() 함수를 호출하여 쿼리 필터하다, 대체 문서 및 mongoc_bulk_operation_t 값을 매개변수로 전달합니다.
bson_t *filter_doc = BCON_NEW("restaurant_id", BCON_UTF8("1234")); bson_t *replace_doc = BCON_NEW( "name", BCON_UTF8("Mongo's Deli"), "cuisine", BCON_UTF8("Sandwiches and Salads"), "borough", BCON_UTF8("Brooklyn"), "restaurant_id", BCON_UTF8("5678") ); bson_error_t error; if (!mongoc_bulk_operation_replace_one_with_opts(bulk, filter_doc, replace_doc, NULL, &error)) { fprintf(stderr, "Failed to add replace operation: %s\n", error.message); } bson_destroy(filter_doc); bson_destroy(replace_doc);
여러 문서를 바꾸려면 각 문서 에 대해 mongoc_bulk_operation_replace_one_with_opts() 를 호출합니다.
삭제 작업
삭제 작업을 수행하려면 대량 쓰기 (write) 의 일부로 작업을 대기열에 추가하는 mongoc_bulk_operation_t에 삭제 지침을 추가합니다.
다음 예시 mongoc_bulk_operation_remove_one_with_opts() 함수를 호출하여 쿼리 필터하다 와 mongoc_bulk_operation_t 값을 매개변수로 전달합니다.
bson_t *filter_doc = BCON_NEW("restaurant_id", BCON_UTF8("5678")); bson_error_t error; if (!mongoc_bulk_operation_remove_one_with_opts(bulk, filter_doc, NULL, &error)) { fprintf(stderr, "Failed to add delete operation: %s\n", error.message); } bson_destroy(filter_doc);
여러 문서를 삭제 하려면 mongoc_bulk_operation_remove_many_with_opts() 함수를 호출하고 동일한 매개변수를 전달합니다. 이렇게 하면 운전자 쿼리 필터하다 와 일치하는 모든 문서를 삭제 하도록 지시합니다.
다음 예시 대량 쓰기 (write) 에 대한 삭제 작업을 대기열에 추가합니다.
bson_t *filter_doc = BCON_NEW("borough", BCON_UTF8("Manhattan")); bson_error_t error; if (!mongoc_bulk_operation_remove_many_with_opts(bulk, filter_doc, NULL, &error)) { fprintf(stderr, "Failed to add delete operation: %s\n", error.message); } bson_destroy(filter_doc);
대량 작업 실행
대량 쓰기 (write) 에 대기 중인 각 쓰기 (write) 작업을 실행 하려면 mongoc_bulk_operation_execute() 함수를 호출합니다. 이 함수는 다음 매개변수를 허용합니다:
mongoc_bulk_operation_t value: 각 쓰기 (write) 작업에 대한 지침을 포함합니다.
결과 위치: 작업 결과를 포함할 덮어쓸 수 있는 저장 에 대한 포인터 또는
NULL오류 위치: 오류 값의 위치 지정하거나
NULL
문서를 대량으로 쓰기 (write) 하려면 다음 구문을 사용하세요.
bson_error_t error; mongoc_bulk_operation_t *bulk = mongoc_collection_create_bulk_operation_with_opts(collection, NULL); bson_t *insert_doc = BCON_NEW( "<field name>", BCON_UTF8("<value>"), "<field name>", BCON_UTF8("<value>"), "<field name>", BCON_UTF8("<value>"), "<field name>", BCON_UTF8("<value>") ); mongoc_bulk_operation_insert(bulk, insert_doc); bson_destroy(insert_doc); bson_t *query = BCON_NEW("<field to match>", BCON_UTF8("<value to match>")); bson_t *update = BCON_NEW("$set", "{", "<field name>", BCON_UTF8("<value>"), "}"); mongoc_bulk_operation_update_one(bulk, query, update, false); bson_destroy(query); bson_destroy(update); bool result = mongoc_bulk_operation_execute(bulk, NULL, &error); if (!result) { fprintf(stderr, "Bulk operation error: %s\n", error.message); } mongoc_bulk_operation_destroy(bulk);
다음 예시 함수를 호출하여 이 가이드 의 이전 섹션에 지정된 삽입, 업데이트, 바꾸기 및 삭제 작업을 mongoc_bulk_operation_execute() 수행합니다.
bson_error_t error; bool result = mongoc_bulk_operation_execute(bulk, NULL, &error); if (!result) { printf("Bulk operation error: %s\n", error.message); } mongoc_bulk_operation_destroy(bulk);
쓰기 (write) 작업 중 하나라도 실패하면 C 운전자 출력 오류를 설정하고 더 이상의 작업을 수행하지 않습니다.
대량 쓰기 작업 사용자 지정
옵션 값을 지정하는 BSON 문서 전달하여 mongoc_collection_create_bulk_operation_with_opts() 함수의 동작을 수정할 수 있습니다. 다음 표에서는 문서 에서 설정하다 수 있는 옵션에 대해 설명합니다.
옵션 | 설명 |
|---|---|
|
|
| 대량 작업에 대한 쓰기 고려 (write concern) 지정합니다. 자세한 내용은 MongoDB Server 매뉴얼의 쓰기 고려를 참조하세요. |
| 지정된 세션 내에서 불크 작업을 실행합니다. 자세한 내용은 MongoDB Server 매뉴얼의 서버 세션을 참조하세요. |
| 작업에 주석을 첨부합니다. 자세한 내용은 MongoDB Server 매뉴얼의 삭제 명령 필드 가이드 참조하세요. |
| 작업 가독성을 높이기 위해 값 목록이 포함된 문서 지정합니다. 값은 문서 필드를 참조하지 않는 상수 또는 닫힌 표현식이어야 합니다. 자세한 내용은 MongoDB Server 매뉴얼의 let 성명서 참조하세요. |
다음 예시 mongoc_collection_create_bulk_operation_with_opts() 함수를 호출하고 ordered 옵션을 false로 설정합니다.
bson_t opts; BSON_APPEND_BOOL(&opts, "ordered", false); bulk = mongoc_collection_create_bulk_operation_with_opts(collection, &opts); // Perform bulk operation bson_destroy(&opts); mongoc_bulk_operation_destroy(bulk);
순서가 지정되지 않은 대량 쓰기 (write) 의 쓰기 (write) 작업 중 하나라도 실패하면 C 운전자 모든 작업을 시도한 후에만 오류를 보고합니다.
참고
순서가 지정되지 않은 대량 작업은 실행 순서가 보장되지 않습니다. 이 순서는 런타임을 최적화하기 위해 나열한 방식과 다를 수 있습니다.
추가 정보
개별 쓰기 작업을 수행하는 방법을 알아보려면 다음 가이드를 참조하세요.
API 문서
이 가이드 에 설명된 함수 또는 유형에 대해 자세히 학습 다음 API 문서를 참조하세요.