1
+ #include < iostream>
2
+
3
+ #include < bsoncxx/builder/basic/document.hpp>
4
+ #include < bsoncxx/json.hpp>
5
+ #include < mongocxx/client.hpp>
6
+ #include < mongocxx/instance.hpp>
7
+ #include < mongocxx/uri.hpp>
8
+
9
+ using bsoncxx::builder::basic::kvp;
10
+ using bsoncxx::builder::basic::make_document;
11
+
12
+ int main () {
13
+ mongocxx::instance instance;
14
+ mongocxx::uri uri (" <connection string>" );
15
+ mongocxx::client client (uri);
16
+
17
+ // start-db-coll
18
+ auto db = client[" sample_restaurants" ];
19
+ auto collection = db[" restaurants" ];
20
+ // end-db-coll
21
+
22
+ {
23
+ // replaces a document that has a "name" value of "Nobu"
24
+ // with a document that has a "name" of "La Bernadin"
25
+ // start-replace-one
26
+ auto query_filter = make_document (kvp (" name" , " Nobu" ));
27
+ auto replace_doc = make_document (kvp (" name" , " La Bernadin" ));
28
+
29
+ auto result = collection.replace_one (query_filter.view (), replace_doc.view ());
30
+ // end-replace-one
31
+ }
32
+
33
+ {
34
+ // Retrieves a document to verify the replacement operation
35
+ // start-replace-one-io
36
+ auto new_doc = collection.find_one (make_document (kvp (" name" , " La Bernadin" )));
37
+ std::cout << " New document: " << bsoncxx::to_json (*new_doc) << std::endl;
38
+ // end-replace-one-io
39
+ }
40
+
41
+ {
42
+ // Replaces a document that has a "name" value of "Nobu" and instructs the operation to use the "name" field index
43
+ // start-replace-options-hint
44
+ auto index_specification = make_document (kvp (" name" , 1 ));
45
+ collection.create_index (index_specification.view ());
46
+
47
+ mongocxx::options::replace opts{};
48
+ opts.hint (mongocxx::hint{" name_1" });
49
+
50
+ auto query_filter = make_document (kvp (" name" , " Nobu" ));
51
+ auto replace_doc = make_document (kvp (" name" , " La Bernadin" ));
52
+
53
+ auto result = collection.replace_one (query_filter.view (), replace_doc.view (), opts);
54
+ // end-replace-options-hint
55
+ }
56
+
57
+ {
58
+ // Replaces a document that has a "name" value of "In-N-Out Burger"
59
+ // and instructs the operation to insert a new operation if none match.
60
+ // start-replace-options-upsert
61
+ std::cout << " Total document count before replace_one(): " << collection.count_documents ({}) << std::endl;
62
+
63
+ mongocxx::options::replace opts{};
64
+ opts.upsert (true );
65
+
66
+ auto query_filter = make_document (kvp (" name" , " In-N-Out Burger" ));
67
+ auto replace_doc = make_document (kvp (" name" , " Shake Shack" ));
68
+
69
+ auto result = collection.replace_one (query_filter.view (), replace_doc.view (), opts);
70
+
71
+ std::cout << " Total document count after replace_one(): " << collection.count_documents ({}) << std::endl;
72
+ // end-replace-options-upsert
73
+ }
74
+
75
+ {
76
+ // Replaces the matching document and prints the number of matched documents
77
+ // start-replace-result-matched
78
+ auto query_filter = make_document (kvp (" name" , " Shake Shack" ));
79
+ auto replace_doc = make_document (kvp (" name" , " In-N-Out Burger" ));
80
+
81
+ auto result = collection.replace_one (query_filter.view (), replace_doc.view ());
82
+ std::cout << " Matched documents: " << result->matched_count () << std::endl;
83
+ // end-replace-result-matched
84
+ }
85
+
86
+ {
87
+
88
+ // Replaces a document that has a "name" value of "In-N-Out Burger" and instructs the operation
89
+ // to insert a new document if none match.
90
+ // start-replace-result-upsert
91
+ mongocxx::options::replace opts{};
92
+ opts.upsert (true );
93
+
94
+ auto query_filter = make_document (kvp (" name" , " In-N-Out Burger" ));
95
+ auto replace_doc = make_document (kvp (" name" , " Shake Shack" ));
96
+
97
+ auto result = collection.replace_one (query_filter.view (), replace_doc.view (), opts);
98
+ auto id = result->upserted_id ()->get_value ();
99
+
100
+ std::cout << " Upserted ID: " << id.get_oid ().value .to_string () << std::endl;
101
+ // end-replace-result-upsert
102
+ }
103
+ }
0 commit comments