Skip to content

Commit 2884319

Browse files
committed
add index creation examples
1 parent 191b030 commit 2884319

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

examples/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ include_directories(
66

77
set(MONGOCXX_EXAMPLES
88
aggregate.cpp
9+
create.cpp
10+
index.cpp
911
query.cpp
1012
remove.cpp
1113
update.cpp
12-
create.cpp
1314
)
1415

1516
foreach(EXAMPLE_SRC ${MONGOCXX_EXAMPLES})

examples/index.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <bsoncxx/builder.hpp>
2+
3+
#include <mongocxx/client.hpp>
4+
5+
using namespace bsoncxx::builder::helpers;
6+
using bsoncxx::builder::document;
7+
8+
int main(int, char**) {
9+
mongocxx::client conn{};
10+
11+
auto db = conn["test"];
12+
13+
// Create a single field index.
14+
{
15+
// @begin: cpp-single-field-index
16+
document index_spec;
17+
index_spec << "cuisine" << 1;
18+
db["restaurants"].create_index(index_spec, {});
19+
// @end: cpp-single-field-index
20+
}
21+
22+
// Create a compound index.
23+
{
24+
// @begin: cpp-create-compound-index
25+
document index_spec;
26+
index_spec << "cuisine" << 1 << "address.zipcode" << -1;
27+
db["restaurants"].create_index(index_spec, {});
28+
// @end: cpp-create-compound-index
29+
}
30+
}

0 commit comments

Comments
 (0)