Skip to content

Commit 79090cb

Browse files
author
Tess Avitabile
committed
CXX-790 - Bulk write example
1 parent 85ad0a1 commit 79090cb

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

examples/mongocxx/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ link_directories(
99

1010
set(MONGOCXX_EXAMPLES
1111
aggregate.cpp
12+
bulk_write.cpp
1213
create.cpp
1314
document_validation.cpp
1415
index.cpp

examples/mongocxx/bulk_write.cpp

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Copyright 2015 MongoDB Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <cstdlib>
16+
17+
#include <bsoncxx/builder/stream/document.hpp>
18+
19+
#include <mongocxx/client.hpp>
20+
#include <mongocxx/instance.hpp>
21+
#include <mongocxx/uri.hpp>
22+
23+
using bsoncxx::builder::stream::close_document;
24+
using bsoncxx::builder::stream::document;
25+
using bsoncxx::builder::stream::finalize;
26+
using bsoncxx::builder::stream::open_document;
27+
28+
int main(int, char**) {
29+
mongocxx::instance inst{};
30+
mongocxx::client conn{mongocxx::uri{}};
31+
32+
auto coll = conn["test"]["coll"];
33+
coll.drop();
34+
35+
// @begin: cpp-bulk-write
36+
auto doc1 = document{} << "a" << 1 << finalize;
37+
auto doc2 = document{} << "$set" << open_document << "a" << 2 << close_document << finalize;
38+
39+
// Create a model for an insert_one operation.
40+
mongocxx::model::insert_one insert_op{doc1.view()};
41+
42+
// Create a model for a delete_one operation.
43+
mongocxx::model::delete_one delete_op{doc1.view()};
44+
45+
// Create a model for an update_one operation.
46+
mongocxx::model::update_one upsert_op{doc1.view(), doc2.view()};
47+
48+
// Set upsert to true: if no document matches {"a": 1}, insert {"a": 2}.
49+
upsert_op.upsert(true);
50+
51+
// Create a bulk_write operation. By default, its operations are executed in order.
52+
mongocxx::bulk_write bulk{};
53+
54+
// Insert {"a": 1}.
55+
bulk.append(insert_op);
56+
57+
// Delete {"a": 1}.
58+
bulk.append(delete_op);
59+
60+
// Since no document matches {"a": 1}, insert {"a": 2}.
61+
bulk.append(upsert_op);
62+
63+
// Insert {"a": 1}. Note the same model may be appended multiple times.
64+
bulk.append(insert_op);
65+
66+
// Update {"a": 1} to {"a": 2}.
67+
bulk.append(upsert_op);
68+
69+
mongocxx::stdx::optional<mongocxx::result::bulk_write> result = coll.bulk_write(bulk);
70+
71+
if (!result) {
72+
return EXIT_FAILURE;
73+
}
74+
75+
if (result->inserted_count() != 2) {
76+
return EXIT_FAILURE;
77+
}
78+
79+
if (result->matched_count() != 1) {
80+
return EXIT_FAILURE;
81+
}
82+
83+
if (result->modified_count() != 1) {
84+
return EXIT_FAILURE;
85+
}
86+
87+
if (result->deleted_count() != 1) {
88+
return EXIT_FAILURE;
89+
}
90+
91+
if (result->upserted_count() != 1) {
92+
return EXIT_FAILURE;
93+
}
94+
95+
// TODO: print inserted and upserted IDs once CXX-798 is resolved
96+
97+
// The collection should contain two copies of {"a": 2}.
98+
auto cursor = coll.find({});
99+
int i = 0;
100+
for (auto&& doc : cursor) {
101+
i++;
102+
if (doc["a"].get_int32() != 2) {
103+
return EXIT_FAILURE;
104+
}
105+
}
106+
107+
return (i == 2) ? EXIT_SUCCESS : EXIT_FAILURE;
108+
// @end: cpp-bulk-write
109+
}

0 commit comments

Comments
 (0)