Skip to content

Commit f418feb

Browse files
author
Matt Cotter
committed
CXX-877 add example for nested bson document
1 parent 14abf61 commit f418feb

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

examples/bsoncxx/builder_stream.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,33 @@ int main(int, char**) {
5858
using builder::stream::finalize;
5959
auto myQuery = document{} << "foo"
6060
<< "bar" << finalize;
61+
62+
// There is a special concatenate helper to add all keys and corresponding values from one
63+
// document into another.
64+
using bsoncxx::builder::concatenate;
65+
doc << concatenate(myQuery.view());
66+
// `doc` now looks like:
67+
// {
68+
// "myKey": "myValue",
69+
// ...
70+
// "mySubArr": [...],
71+
// "foo": "bar"
72+
// }
73+
74+
// To nest an existing bsoncxx::document::value into a builder stream, you can create a
75+
// types::b_document and append it. Alternatively you can open a new document and concatenate
76+
// the value in.
77+
bsoncxx::document::value nestedValue = document{} << "nested" << true << finalize;
78+
document topLevelDoc{};
79+
topLevelDoc << "subDoc1" << types::b_document{nestedValue.view()} << "subDoc2" << open_document
80+
<< concatenate(nestedValue.view()) << close_document;
81+
// `topLevelDoc` now looks like:
82+
// {
83+
// "subDoc1": {
84+
// "nested": true
85+
// },
86+
// "subDoc2": {
87+
// "nested": true
88+
// }
89+
// }
6190
}

0 commit comments

Comments
 (0)