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

MongoDB에 데이터 쓰기

이 페이지에서는 MongoDB 에 데이터를 쓰기 위한 일반적인 C++ 운전자 메서드를 보여주는 복사 가능한 코드 예제를 볼 수 있습니다.

팁

이 페이지에 표시된 메서드에 대해 자세히 알아보려면 각 섹션에 제공된 링크를 참조하세요.

To use an example from this page, copy the code example into the sample application or your own application. Be sure to replace all placeholders, such as <connection string>, with the relevant values for your MongoDB deployment.

다음 샘플 애플리케이션을 사용하여 이 페이지의 코드 예제를 테스트할 수 있습니다. 샘플 애플리케이션을 사용하려면 다음 단계를 수행하세요.

  1. 프로젝트 에서 가져올 수 있는 위치 에 C++ 운전자 가 설치되어 있는지 확인합니다.

  2. 다음 코드를 복사하여 프로젝트 내의 새 .cpp 파일 에 붙여넣습니다.

  3. 이 페이지에서 코드 예시 를 복사하여 파일 의 강조 표시된 섹션에 붙여넣습니다.

1#include <iostream>
2
3#include <bsoncxx/builder/basic/document.hpp>
4#include <bsoncxx/json.hpp>
5#include <mongocxx/client.hpp>
6#include <mongocxx/exception/exception.hpp>
7#include <mongocxx/instance.hpp>
8#include <mongocxx/uri.hpp>
9
10using bsoncxx::builder::basic::kvp;
11using bsoncxx::builder::basic::make_document;
12
13int main() {
14 try {
15 mongocxx::instance instance;
16
17 mongocxx::uri uri("<connection string>");
18 mongocxx::client client(uri);
19
20 auto database = client["<database name>"];
21 auto collection = database["<collection name>"];
22
23 // Start example code here
24
25 // End example code here
26
27 } catch (const mongocxx::exception& e) {
28 std::cout << "An exception occurred: " << e.what() << "\n";
29 return EXIT_FAILURE;
30 }
31
32 return EXIT_SUCCESS;
33}

다음 코드는 단일 문서 를 컬렉션 에 삽입하는 방법을 보여줍니다.

auto result = collection.insert_one(make_document(kvp("<field name>", "<value>")));

insert_one() 메서드에 대해 자세히 알아보려면 문서 삽입 가이드를 참조하세요.

다음 코드는 컬렉션 에 여러 문서를 삽입하는 방법을 보여줍니다.

std::vector<bsoncxx::document::value> documents;
documents.push_back(make_document(kvp("<field name>", "<value>")));
documents.push_back(make_document(kvp("<field name>", "<value>")));
auto result = collection.insert_many(documents);

insert_many() 메서드에 대해 자세히 알아보려면 문서 삽입 가이드를 참조하세요.

다음 코드는 필드 를 만들거나 편집하여 컬렉션 의 단일 문서 를 업데이트 하는 방법을 보여줍니다.

auto query_filter = make_document(kvp("<field to match>", "<value to match>"));
auto update_doc = make_document(kvp("$set", make_document(kvp("<field name>", "<value>"))));
auto result = collection.update_one(query_filter.view(), update_doc.view());

update_one() 메서드에 대해 자세히 알아보려면 문서 업데이트 가이드를 참조하세요.

다음 코드는 필드 를 만들거나 편집하여 컬렉션 에 있는 여러 문서를 업데이트 하는 방법을 보여줍니다.

auto query_filter = make_document(kvp("<field to match>", "<value to match>"));
auto update_doc = make_document(kvp("$set", make_document(kvp("<field name>", "<value>"))));
auto result = collection.update_many(query_filter.view(), update_doc.view());

update_many() 메서드에 대해 자세히 알아보려면 문서 업데이트 가이드를 참조하세요.

다음 코드는 컬렉션 에서 단일 문서 를 대체하는 방법을 보여줍니다.

auto query_filter = make_document(kvp("<field to match>", "<value to match>"));
auto replace_doc = make_document(make_document(kvp("<field name>", "<value>")));
auto result = collection.replace_one(query_filter.view(), replace_doc.view());

replace_one() 메서드에 대해 자세히 알아보려면 문서 교체 가이드를 참조하세요.

다음 코드는 컬렉션 에서 단일 문서 를 삭제 하는 방법을 보여줍니다.

auto result = collection.delete_one(make_document(kvp("<field name>", "<value>")));

delete_one() 메서드에 대해 자세히 알아보려면 문서 삭제 가이드를 참조하세요.

다음 코드는 컬렉션 에서 여러 문서를 삭제 하는 방법을 보여줍니다.

auto result = collection.delete_many(make_document(kvp("<field name>", "<value>")));

delete_many() 메서드에 대해 자세히 알아보려면 문서 삭제 가이드를 참조하세요.

다음 코드는 단일 대량 작업으로 여러 쓰기 (write) 작업을 수행하는 방법을 보여줍니다.

auto bulk = collection.create_bulk_write();
auto insert_doc = make_document(kvp("<field name>", "<value>"));
auto update_filter = make_document(kvp("<field name>", "<value>"));
auto update_doc = make_document(kvp("$set", make_document(kvp("<field name>", "<value>"))));
auto delete_filter = make_document(kvp("<field name>", "<value>"));
mongocxx::model::insert_one insert_op{insert_doc.view()};
mongocxx::model::update_many update_op{update_filter.view(), update_doc.view()};
mongocxx::model::delete_one delete_op{delete_filter.view()};
bulk.append(insert_op);
bulk.append(update_op);
bulk.append(delete_op);
auto result = bulk.execute();

create_bulk_write() 메서드에 대해 자세히 알아보려면 대량 쓰기 가이드를 참조하세요.