AI 에이전트의 경우: 문서 인덱스는 https://www.mongodb.com/ko-kr/docs/llms.txt에서 사용할 수 있으며, 모든 페이지의 마크다운 버전은 어떤 URL 경로에 .md를 추가하여 사용할 수 있습니다.
Docs Menu

대량 쓰기 작업

이 가이드 에서는 대량 쓰기 (write) 작업 을 사용하여 단일 데이터베이스 호출에서 여러 쓰기 (write) 작업을 수행하는 방법에 학습 설명합니다.

컬렉션에 문서를 삽입하고, 다른 여러 문서를 업데이트하고, 그리고 문서를 삭제하려는 상황을 고려해 보세요. 개별 함수를 사용하는 경우, 각 작업에는 고유의 데이터베이스 호출이 필요합니다. 그 대신, 대량 작업을 사용하여 데이터베이스 호출 횟수를 줄일 수 있습니다.

C 드라이버는 두 개의 일괄 쓰기 (write) API를 제공합니다. mongoc_bulkwrite_t 및 mongoc_bulk_operation_t

참고

If you're using MongoDB Server 8.0 or later, we recommend that you use mongoc_bulkwrite_t. It can perform operations across multiple collections or databases in one call, while mongoc_bulk_operation_t can target only one collection per call.

이 가이드의 예시에서는 Atlas 샘플 데이터 세트의 sample_restaurants 데이터베이스에 있는 restaurants 컬렉션을 사용합니다. 무료 MongoDB Atlas 클러스터 생성하고 샘플 데이터 세트를 로드하는 방법을 학습하려면 MongoDB 시작하기 가이드 를 참조하세요.

일괄 쓰기 (write) 작업에는 하나 이상의 쓰기 (write) 작업이 포함됩니다. 대량 쓰기 (write)를 사용하려면 대량 쓰기 (write) 유형을 생성하고, 수행하려는 각 작업에 대해 해당 추가 (append) 함수를 호출한 다음 대량 쓰기 (write)를 실행합니다.

대량 쓰기 (write) 작업을 만들려면 mongoc_client_bulkwrite_new() 함수를 호출합니다. 이 함수는 수행할 쓰기 (write) 작업에 대한 지침을 저장 데 사용할 수 있는 mongoc_bulkwrite_t 유형의 값을 반환합니다. mongoc_client_t 값을 매개변수로 전달하여 대량 쓰기 (write)에 대한 클라이언트를 지정합니다.

mongoc_bulkwrite_t *bulk = mongoc_client_bulkwrite_new(client);

mongoc_bulkwrite_t 값에 삽입, 업데이트, 대체 및 삭제 작업을 추가할 수 있습니다.

삽입 작업을 수행하려면 대량 쓰기 (write) 의 일부로 작업을 대기열에 추가하는 mongoc_bulkwrite_t 값에 삽입 지침을 추가합니다.

다음 예시에서 mongoc_bulkwrite_append_insertone() 함수를 호출합니다. 이 함수는 다음 매개변수를 허용합니다.

Parameter
설명

self

작업에 추가할 mongoc_bulkwrite_t 값입니다.

ns

문서를 삽입할 네임스페이스(<database>.<collection>으로 지정).

document

삽입할 문서입니다.

opts

작업을 사용자 지정하는 선택 사항 설정 또는 NULL

error

함수가 false을 반환하는 경우 오류 세부 정보를 받습니다.

다음 예시는 대량 쓰기 (write)에 삽입 작업을 추가합니다.

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_bulkwrite_append_insertone(
bulk, "sample_restaurants.restaurants", insert_doc, NULL, &error)) {
fprintf(stderr, "Failed to add insert operation: %s\n", error.message);
}
bson_destroy(insert_doc);

여러 문서를 삽입하려면 각 문서에 대해 mongoc_bulkwrite_append_insertone() 함수를 호출합니다.

업데이트 작업을 수행하려면 대량 쓰기 (write) 의 일부로 작업을 대기열에 추가하는 mongoc_bulkwrite_t에 업데이트 지침을 추가합니다.

다음 예시에서 mongoc_bulkwrite_append_updateone() 함수를 호출합니다. 이 함수는 다음 매개변수를 허용합니다.

Parameter
설명

self

작업에 추가할 mongoc_bulkwrite_t 값입니다.

ns

업데이트할 문서가 포함된 네임스페이스(<database>.<collection>으로 지정).

filter

업데이트할 문서를 선택하는 쿼리 필터입니다.

update

일치하는 문서에 적용 업데이트

opts

작업을 사용자 지정하는 선택 사항 설정 또는 NULL

error

함수가 false을 반환하는 경우 오류 세부 정보를 받습니다.

다음 예시는 대량 쓰기 (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_bulkwrite_append_updateone(
bulk, "sample_restaurants.restaurants", 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_bulkwrite_append_updatemany() 함수를 호출하고 이전 예시에 표시된 매개변수를 전달합니다. 이렇게 하면 드라이버가 쿼리 필터와 일치하는 모든 문서를 업데이트하도록 지시합니다.

다음 예시 대량 쓰기 (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_bulkwrite_append_updatemany(
bulk, "sample_restaurants.restaurants", 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_bulkwrite_t 값에 교체 지침을 추가합니다.

다음 예시에서 mongoc_bulkwrite_append_replaceone() 함수를 호출합니다. 이 함수는 다음 매개변수를 허용합니다.

Parameter
설명

self

작업에 추가할 mongoc_bulkwrite_t 값입니다.

ns

<database>.<collection>으로 지정된 바꿀 문서를 포함하는 네임스페이스.

filter

대체할 문서를 선택하는 쿼리 필터.

replacement

일치하는 문서를 대체하는 문서입니다.

opts

작업을 사용자 지정하는 선택 사항 설정 또는 NULL

error

함수가 false을 반환하는 경우 오류 세부 정보를 받습니다.

다음 예시 대량 쓰기 (write) 에 대한 바꾸기 작업을 추가합니다.

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_bulkwrite_append_replaceone(
bulk, "sample_restaurants.restaurants", 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_bulkwrite_append_replaceone() 함수를 호출합니다.

삭제 작업을 수행하려면 대량 쓰기 (write) 의 일부로 작업을 대기열에 추가하는 mongoc_bulkwrite_t에 삭제 지침을 추가합니다.

다음 예시에서 mongoc_bulkwrite_append_deleteone() 함수를 호출합니다. 이 함수는 다음 매개변수를 허용합니다.

Parameter
설명

self

작업에 추가할 mongoc_bulkwrite_t 값입니다.

ns

삭제할 문서가 포함된 네임스페이스(<database>.<collection>으로 지정됨)

filter

삭제할 문서를 선택하는 쿼리 필터.

opts

작업을 사용자 지정하는 선택 사항 설정 또는 NULL

error

함수가 false을 반환하는 경우 오류 세부 정보를 받습니다.

다음 예시는 대량 쓰기 (write) 에 삭제 작업을 추가합니다.

bson_t *filter_doc = BCON_NEW("name", BCON_UTF8("Mongo's Deli"));
bson_error_t error;
if (!mongoc_bulkwrite_append_deleteone(
bulk, "sample_restaurants.restaurants", filter_doc, NULL, &error)) {
fprintf(stderr, "Failed to add delete operation: %s\n", error.message);
}
bson_destroy(filter_doc);

여러 문서를 삭제하려면 mongoc_bulkwrite_append_deletemany() 함수를 호출하고 이전 예시에 표시된 매개변수를 전달합니다. 이는 드라이버에게 쿼리 필터와 일치하는 모든 문서를 삭제하도록 지시합니다.

다음 예시에서는 다수 삭제 작업을 추가합니다.

bson_t *filter_doc = BCON_NEW("borough", BCON_UTF8("Manhattan"));
bson_error_t error;
if (!mongoc_bulkwrite_append_deletemany(
bulk, "sample_restaurants.restaurants", filter_doc, NULL, &error)) {
fprintf(stderr, "Failed to add delete operation: %s\n", error.message);
}
bson_destroy(filter_doc);

mongoc_bulkwrite_execute() 함수는 다음 매개변수를 허용합니다.

Parameter
설명

self

각 쓰기 (write) 작업에 대한 지침을 포함하는 mongoc_bulkwrite_t 값입니다.

opts

작업을 사용자 지정하는 선택 사항 설정 또는 NULL

작업이 성공하면 함수는 작업 결과가 포함되어 있는 mongoc_bulkwritereturn_t 값을 반환합니다. 그러지 않으면 반환 값에 오류 세부 정보가 포함된 mongoc_bulkwriteexception_t 값이 포함됩니다.

다음 예시에서는 일괄 문서 쓰기 (write) 방법을 보여 줍니다.

bson_error_t error;
mongoc_bulkwrite_t *bulk = mongoc_client_bulkwrite_new(client);
bson_t *insert_doc = BCON_NEW(
"<field name 1>", BCON_UTF8("<value 1>"),
"<field name 2>", BCON_UTF8("<value 2>"),
"<field name 3>", BCON_UTF8("<value 3>"),
"<field name 4>", BCON_UTF8("<value 4>")
);
mongoc_bulkwrite_append_insertone(
bulk, "sample_restaurants.restaurants", insert_doc, NULL, &error);
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_bulkwrite_append_updateone(
bulk, "sample_restaurants.restaurants", query, update, NULL, &error);
bson_destroy(query);
bson_destroy(update);
mongoc_bulkwritereturn_t result = mongoc_bulkwrite_execute(bulk, NULL);
if (result.exc) {
if (mongoc_bulkwriteexception_error(result.exc, &error)) {
fprintf(stderr, "Bulk write error: %s\n", error.message);
}
}
mongoc_bulkwriteresult_destroy(result.res);
mongoc_bulkwriteexception_destroy(result.exc);
mongoc_bulkwrite_destroy(bulk);

The following example calls the mongoc_bulkwrite_execute() function to perform the insert, update, replace, and delete operations.

bson_error_t error;
mongoc_bulkwritereturn_t result = mongoc_bulkwrite_execute(bulk, NULL);
if (result.exc) {
if (mongoc_bulkwriteexception_error(result.exc, &error)) {
fprintf(stderr, "Bulk write error: %s\n", error.message);
}
} else if (result.res) {
printf("Inserted: %" PRId64 " document(s)\n",
mongoc_bulkwriteresult_insertedcount(result.res));
printf("Matched: %" PRId64 " document(s)\n",
mongoc_bulkwriteresult_matchedcount(result.res));
printf("Modified: %" PRId64 " document(s)\n",
mongoc_bulkwriteresult_modifiedcount(result.res));
printf("Deleted: %" PRId64 " document(s)\n",
mongoc_bulkwriteresult_deletedcount(result.res));
}
mongoc_bulkwriteresult_destroy(result.res);
mongoc_bulkwriteexception_destroy(result.exc);

쓰기 (write) 작업 중 하나라도 실패하면 C 드라이버는 반환 값에 오류를 보고하고 더 이상의 작업을 수행하지 않습니다.

다음 예시는 mongoc_bulkwriteopts_t 값을 만들고, ordered을 false로 설정하며, 옵션을 mongoc_bulkwrite_execute()에 전달합니다.

mongoc_bulkwriteopts_t *opts = mongoc_bulkwriteopts_new();
mongoc_bulkwriteopts_set_ordered(opts, false);
mongoc_bulkwrite_t *bulk = mongoc_client_bulkwrite_new(client);
// Perform bulk operation
mongoc_bulkwritereturn_t result = mongoc_bulkwrite_execute(bulk, opts);
mongoc_bulkwriteresult_destroy(result.res);
mongoc_bulkwriteexception_destroy(result.exc);
mongoc_bulkwriteopts_destroy(opts);
mongoc_bulkwrite_destroy(bulk);

순서가 지정되지 않은 대량 쓰기 (write) 의 쓰기 (write) 작업 중 하나라도 실패하면 C 운전자 모든 작업을 시도한 후에만 오류를 보고합니다.

참고

순서가 지정되지 않은 대량 작업은 실행 순서가 보장되지 않습니다. 이 순서는 런타임을 최적화하기 위해 나열한 방식과 다를 수 있습니다.

다음 표에서는 일괄 쓰기 (write) 작업을 위해 mongoc_bulkwriteopts_t 값에서 지정할 수 있는 옵션에 대해 설명합니다.

옵션
설명

ordered

true인 경우 드라이버는 제공된 순서대로 쓰기 (write) 작업을 수행합니다. 오류가 발생하면 나머지 작업은 수행되지 않습니다.

false인 경우 드라이버는 임의의 순서대로 작업을 수행하고 모든 작업을 수행하려고 시도합니다.
기본값은 true입니다.

bypassdocumentvalidation

true인 경우 쓰기 (write)에서 문서 수준 유효성 검사를 옵트아웃하도록 허용합니다.

writeconcern

Specifies the write concern for the bulk operation. For more information, see Write Concern in the MongoDB Server manual.

comment

Attaches a comment to the operation. For more information, see the delete command fields guide in the MongoDB Server manual.

let

Specifies a document with a list of values to improve operation readability. Values must be constant or closed expressions that don't reference document fields. For more information, see the let statement in the MongoDB Server manual.

verboseresults

true인 경우, 반환된 결과에는 각 성공적인 작업에 대한 세부 결과가 포함됩니다.

참고

1.28.0보다 이전 C 드라이버 버전을 사용하는 경우 mongoc_bulk_operation_t을(를) 사용합니다, 8.0 미만의 MongoDB Server 버전을 대상으로 하는 경우 또는 단일 컬렉션 쓰기 (write)만 필요한 경우.

일괄 쓰기 (write) 작업에는 하나 이상의 쓰기 (write) 작업이 포함됩니다. 대량 쓰기 (write)를 사용하려면 대량 쓰기 (write) 유형을 생성하고, 수행하려는 각 작업에 대해 해당 추가 (append) 함수를 호출한 다음 대량 쓰기 (write)를 실행합니다.

대량 쓰기 (write) 작업을 실행 전에 mongoc_collection_create_bulk_operation_with_opts() 함수를 호출합니다. 이 함수는 수행할 대량 쓰기에 대한 지침을 저장 데 사용할 수 있는 mongoc_bulk_operation_t 유형의 값을 반환합니다.

mongoc_collection_create_bulk_operation_with_opts() 함수는 다음 매개변수를 허용합니다.

Parameter
설명

collection

수정할 컬렉션입니다.

opts

작업을 사용자 지정하는 옵션 또는 NULL

다음 예시 mongoc_collection_create_bulk_operation_with_opts() 함수를 호출하고 restaurants 컬렉션을 매개 변수로 전달합니다:

mongoc_bulk_operation_t *bulk =
mongoc_collection_create_bulk_operation_with_opts(collection, NULL);

mongoc_bulk_operation_t 값에 삽입, 업데이트, 대체 및 삭제 작업을 추가할 수 있습니다.

삽입 작업을 수행하려면 대량 쓰기 (write) 의 일부로 작업을 대기열에 추가하는 mongoc_bulk_operation_t 값에 삽입 지침을 추가합니다.

다음 예시에서 mongoc_bulk_operation_insert_with_opts() 함수를 호출합니다. 이 함수는 다음 매개변수를 허용합니다.

Parameter
설명

bulk

작업에 추가할 mongoc_bulk_operation_t 값입니다.

document

삽입할 문서입니다.

opts

작업을 사용자 지정하는 선택 사항 설정 또는 NULL

error

bson_error_t 오류 값의 선택 사항 위치.

다음 예시는 대량 쓰기 (write)에 삽입 작업을 추가합니다.

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() 함수를 호출합니다. 이 함수는 다음 매개변수를 허용합니다.

Parameter
설명

bulk

작업에 추가할 mongoc_bulk_operation_t 값입니다.

selector

업데이트할 문서를 선택하는 쿼리 필터입니다.

document

일치하는 문서에 적용 업데이트

opts

작업을 사용자 지정하는 선택 사항 설정 또는 NULL

error

bson_error_t 오류 값의 선택 사항 위치.

다음 예시는 대량 쓰기 (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_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() 함수를 호출합니다. 이 함수는 다음 매개변수를 허용합니다.

Parameter
설명

bulk

작업에 추가할 mongoc_bulk_operation_t 값입니다.

selector

대체할 문서를 선택하는 쿼리 필터.

document

일치하는 문서를 대체하는 문서입니다.

opts

작업을 사용자 지정하는 선택 사항 설정 또는 NULL

error

bson_error_t 오류 값의 선택 사항 위치.

다음 예시 대량 쓰기 (write) 에 대한 바꾸기 작업을 추가합니다.

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() 함수를 호출합니다. 이 함수는 다음 매개변수를 허용합니다.

Parameter
설명

bulk

작업에 추가할 mongoc_bulk_operation_t 값입니다.

selector

삭제할 문서를 선택하는 쿼리 필터.

opts

작업을 사용자 지정하는 선택 사항 설정 또는 NULL

error

bson_error_t 오류 값의 선택 사항 위치.

다음 예시는 대량 쓰기 (write) 에 삭제 작업을 추가합니다.

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);

mongoc_bulk_operation_execute() 함수는 다음 매개변수를 허용합니다.

Parameter
설명

bulk

각 쓰기 (write) 작업에 대한 지침을 포함하는 mongoc_bulk_operation_t 값입니다.

reply

결과 문서의 재작성 가능한 저장 공간을 가리키는 선택 사항 포인터 또는 NULL.

error

bson_error_t 오류 값의 선택 사항 위치.

다음 예시에서는 일괄 문서 쓰기 (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);

The following example calls the mongoc_bulk_operation_execute() function to perform the insert, update, replace, and delete operations.

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 드라이버는 error 출력 매개변수를 설정하고 더 이상의 작업을 수행하지 않습니다.

옵션 값을 지정하는 BSON 문서를 전달하여 mongoc_collection_create_bulk_operation_with_opts() 함수의 동작을 수정할 수 있습니다.

다음 예시 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 운전자 모든 작업을 시도한 후에만 오류를 보고합니다.

참고

순서가 지정되지 않은 대량 작업은 실행 순서가 보장되지 않습니다. 이 순서는 런타임을 최적화하기 위해 나열한 방식과 다를 수 있습니다.

다음 표에서는 mongoc_collection_create_bulk_operation_with_opts()에 전달하는 옵션 문서에서 지정할 수 있는 옵션에 대해 설명합니다.

옵션
설명

ordered

true인 경우 드라이버는 제공된 순서대로 쓰기 (write) 작업을 수행합니다. 오류가 발생하면 나머지 작업은 수행되지 않습니다.

false인 경우 드라이버는 임의의 순서대로 작업을 수행하고 모든 작업을 수행하려고 시도합니다.
기본값은 true입니다.

writeConcern

Specifies the write concern for the bulk operation. For more information, see Write Concern in the MongoDB Server manual.

sessionId

Runs the bulk operations within the specified session. For more information, see Server Sessions in the MongoDB Server manual.

comment

Attaches a comment to the operation. For more information, see the delete command fields guide in the MongoDB Server manual.

let

Specifies a document with a list of values to improve operation readability. Values must be constant or closed expressions that don't reference document fields. For more information, see the let statement in the MongoDB Server manual.

개별 쓰기 작업을 수행하는 방법을 알아보려면 다음 가이드를 참조하세요.

이 가이드 에 설명된 함수 또는 유형에 대해 자세히 학습 다음 API 문서를 참조하세요.