Skip to content

Commit 37186e6

Browse files
authored
(DOCSP-39838) [CPP Driver] Write Data to MongoDB > Replace a Document (#62) (#65)
* (DOCSP-49838) New pages for replace_one * (DOCSP-39838) Build error. * (DOCSP-39838) Edited write landing page to include replace page. * (DOCSP-39838) Reordered options and results tables. * (DOCSP-39838) Hint example. * (DOCSP-39838) Build error fix. * (DOCSP-39838) Adding separate section level for examples. * (DOCSP-39838) Levels. * (DOCSP-39838) Edits. * (DOCSP-39838) Example edit. * (DOCSP-39838) Edits. * (DOCSP-39838) Updated the Write Data landing page to have replace code example. * (DOCSP-39838) Updated the Write Data landing page code sample * (DOCSP-39838) Updated the Write Data landing page code sample (added comment) * (DOCSP-39838) Changes. * (DOCSP-39838) Edit landing page code example. * (DOCSP-39838) Include _id field mention in first paragraph. * (DOCSP-39838) Edits. * (DOCP-39838) Update links in table. * (DOCSP-39838) edits. * (DOCSP-39838) Added important admonition. * (DOCSP-39838) Edits * (DOCSP-39838) Edits * (DOCSP-39838) Edit table header. * (DOCSP-39838) Added comments to example. * (DOCSP-39838) Indentation error. * (DOCSP-39838) Table edits. * (DOCSP-39838) Separated output from example. * (DOCSP-39838) Example wording. * (DOCSP-39838) Render error * (DOCSP-39838) Example rendering fixes. * (DOCSP-39838) Misc edits. * (DOCSP-39838) Misc edits. * (DOCSP-39838) Misc edits. * (DOCSP-39838) Misc edits. * (DOCSP-39838) Indent error * (DOCSP-39838) Indent error * (DOCSP-39838) Adding quotation marks to field values. * (DOCSP-39838) @norareidy removed inline find_one() link. * (DOCSP-39838) @norareidy Replace update with replace. * (DOCSP-39838) @norareidy removed monospace from headers for style guide. * (DOCSP-39838) @norareidy Swapping field and function. * (DOCSP-39838) @norareidy Condensing example introduction. * (DOCSP-39838) @norareidy Condensing example introduction. * (DOCSP-39838) @norareidy Condensing and wording edits for example introductions. * (DOCSP-39838) @norareidy Adding warning that sample ID may differ. * (DOCSP-39838) @norareidy Bolded first instance of replace operation and replaced link for hint field. * (DOCSP-39838) @norareidy Header underline fix. * (DOCSP-39838) Adding context to links. * (DOCSP-39838) @norareidy review edits. * (DOCSP-39838) @norareidy Final review changes. * (DOCSP-39838) @kevinAlbs comment fixes. ---------
1 parent 44e5619 commit 37186e6

File tree

4 files changed

+410
-3
lines changed

4 files changed

+410
-3
lines changed

source/includes/usage-examples/write-code-examples.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ int main() {
5757
// end-update-multiple
5858
}
5959

60+
{
61+
// Replaces a document that matches the specified criteria
62+
// start-replace-one
63+
auto query_filter = make_document(kvp("<field to match>", "<value to match>"));
64+
auto replace_doc = make_document(make_document(kvp("<field name>", "<value>")));
65+
66+
auto result = collection.replace_one(query_filter.view(), replace_doc.view());
67+
// end-replace-one
68+
}
69+
6070
{
6171
// Deletes a document that matches the specified criteria
6272
// start-delete-one

source/includes/write/replace.cpp

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
}

source/write.txt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Write Data to MongoDB
2424

2525
/write/insert
2626
/write/update
27+
/write/replace
2728
/write/delete
2829
/write/bulk-write
2930
/write/gridfs
@@ -116,9 +117,19 @@ or editing a field:
116117
To learn more about the ``update_many()`` method, see the
117118
:ref:`Update Documents <cpp-write-update>` guide.
118119

119-
.. TODO:
120-
Replace One
121-
-----------
120+
Replace One
121+
-----------
122+
123+
The following code shows how to replace a single document in a collection:
124+
125+
.. literalinclude:: /includes/usage-examples/write-code-examples.cpp
126+
:start-after: start-replace-one
127+
:end-before: end-replace-one
128+
:language: cpp
129+
:dedent:
130+
131+
To learn more about the ``replace_one()`` method, see the
132+
:ref:`Replace Documents <cpp-write-replace>` guide.
122133

123134
Delete One
124135
----------

0 commit comments

Comments
 (0)