Skip to content

Commit 5576e28

Browse files
authored
(DOCSP-40807) Indexes > Compound indexes (#63) (#66)
* (DOCSP-40807) Add compound indexes to work-with-indexes page and code examples page. * (DOCSP-40807) Fix toctree error. * (DOCSP-40807) Fixing toctree error and deleting copies of example code. * (DOCSP-40807) Fixes. * (DOCSP-40807) Fixes. * (DOCSP_40807) Migrating single index section to its own page. * (DOCSP-40807) edits. * (DOCSP-40807) Added pages to toctree * (DOCSP-40807) Added pages to toctree * (DOCSP-40807) Added to compound index page. ggp * (DOCSP-40807) Edits. * (DOCSP-40807) Edits for indexes landing page. * (DOCSP-40807) Add output to code examples. * (DOCSP-40807) Dedent all code examples * (DOCSP-40807) Edited output for compound index. ggp * (DOCSP-40807) Changed introduction to remove index section. * (Docsp-40807) @norareidy Introducing lists with a full sentence. * (Docsp-40807) @norareidy No articles when starting list. * (DOCSP-40807) @norareidy Review changes. * (DOCSP-40807) Fix reference. * (DOCSP-40807) Reduce wordiness. * (DOCSP-40807) @norareidy Review changes. * (DOCSP-40807) @kevinAlbs Correcting programming language in code examples. ---------
1 parent d2c85e9 commit 5576e28

File tree

6 files changed

+381
-78
lines changed

6 files changed

+381
-78
lines changed

source/includes/indexes/indexes.cpp

Lines changed: 69 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,69 @@
1-
// start-index-single
2-
auto index_specification = make_document(kvp("title", 1));
3-
collection.create_index(index_specification.view());
4-
// end-index-single
5-
6-
// start-remove-index
7-
collection.indexes().drop_one("title_1");
8-
// end-remove-index
9-
10-
// start-remove-all-indexes
11-
collection.indexes().drop_all();
12-
// end-remove-all-indexes
13-
14-
// start-remove-all-wildcard
15-
collection.indexes().drop_one("*");
16-
// end-remove-all-wildcard
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+
13+
int main(){
14+
15+
mongocxx::instance instance;
16+
mongocxx::uri uri("<connection string>");
17+
mongocxx::client client(uri);
18+
19+
// start-db-coll
20+
auto db = client["sample_mflix"];
21+
auto collection = db["movies"];
22+
// end-db-coll
23+
24+
{
25+
// start-index-single
26+
auto index_specification = make_document(kvp("title", 1));
27+
auto result = collection.create_index(index_specification.view());
28+
// end-index-single
29+
}
30+
{
31+
// start-index-single-query
32+
auto document = collection.find_one(make_document(kvp("title","Peter Pan")));
33+
std::cout << bsoncxx::to_json(*document) << std::endl;
34+
// end-index-single-query
35+
}
36+
37+
{
38+
// start-index-compound
39+
auto index_specification = make_document(kvp("title", 1), kvp("year", 1));
40+
auto result = collection.create_index(index_specification.view());
41+
// end-index-compound
42+
}
43+
44+
{
45+
// start-index-compound-query
46+
auto document = collection.find_one(make_document(kvp("title","Peter Pan"), kvp("year", 1924)));
47+
std::cout << bsoncxx::to_json(*document) << std::endl;
48+
// end-index-compound-query
49+
}
50+
51+
{
52+
// start-remove-index
53+
collection.indexes().drop_one("title_1");
54+
// end-remove-index
55+
}
56+
57+
{
58+
// start-remove-all-indexes
59+
collection.indexes().drop_all();
60+
// end-remove-all-indexes
61+
}
62+
63+
{
64+
// start-remove-all-wildcard
65+
collection.indexes().drop_one("*");
66+
// end-remove-all-wildcard
67+
}
68+
69+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ auto result = collection.create_index(index_specification.view());
55
std::cout << "Index created: " << bsoncxx::to_json(result) << std::endl;
66
// end-single-field
77

8+
// start-compound-field
9+
auto index_specification = make_document(kvp("<field name 1>", -1), kvp("<field name 2>", -1));
10+
auto result = collection.create_index(index_specification.view());
11+
12+
std::cout << "Index created: " << bsoncxx::to_json(result) << std::endl;
13+
// end-compound-field
14+
815
// start-remove-index
916
collection.indexes().drop_one("<index name>");
1017

source/indexes.txt

Lines changed: 70 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ Optimize Queries with Indexes
2121
.. toctree::
2222
:titlesonly:
2323
:maxdepth: 1
24-
25-
/work-with-indexes
24+
25+
/indexes/work-with-indexes
26+
/indexes/single-field-index
27+
/indexes/compound-index
2628

2729
Overview
2830
--------
@@ -56,25 +58,66 @@ Single-Field Index
5658

5759
The following code shows how to create an ascending single-field index:
5860

59-
.. literalinclude:: /includes/usage-examples/index-code-examples.cpp
60-
:start-after: start-single-field
61-
:end-before: end-single-field
62-
:language: cpp
63-
:copyable:
61+
.. io-code-block::
62+
:copyable: true
63+
64+
.. input:: /includes/usage-examples/index-code-examples.cpp
65+
:start-after: start-single-field
66+
:end-before: end-single-field
67+
:language: cpp
68+
:dedent:
69+
70+
.. output::
71+
:language: cli
72+
:visible: false
73+
74+
Index created: { "name" : "<field name>_1" }
6475

6576
To learn more about single-field indexes, see the
66-
:ref:`cpp-single-field-index` section of the Work With Indexes guide.
77+
:ref:`cpp-single-field-index` guide.
78+
79+
Compound Index
80+
------------------
81+
82+
The following code shows how to create a descending compound index:
83+
84+
.. io-code-block::
85+
:copyable: true
86+
87+
.. input:: /includes/usage-examples/index-code-examples.cpp
88+
:start-after: start-compound-field
89+
:end-before: end-compound-field
90+
:language: cpp
91+
:dedent:
92+
93+
.. output::
94+
:language: cli
95+
:visible: false
96+
97+
Index created: { "name" : "<field name 1>_-1_<field name 2>_-1" }
98+
99+
To learn more about compound indexes, see the
100+
:ref:`cpp-compound-index` guide.
67101

68102
Remove an Index
69103
---------------
70104

71105
The following code shows how to remove an index:
72106

73-
.. literalinclude:: /includes/usage-examples/index-code-examples.cpp
74-
:start-after: start-remove-index
75-
:end-before: end-remove-index
76-
:language: cpp
77-
:copyable:
107+
.. io-code-block::
108+
:copyable: true
109+
110+
.. input:: /includes/usage-examples/index-code-examples.cpp
111+
:start-after: start-remove-index
112+
:end-before: end-remove-index
113+
:language: cpp
114+
:dedent:
115+
116+
.. output::
117+
:language: cli
118+
:visible: false
119+
120+
Index dropped.
78121

79122
To learn more about removing indexes, see the
80123
:ref:`cpp-indexes-remove` section of the Work With Indexes guide.
@@ -84,11 +127,20 @@ Remove All Indexes
84127

85128
The following code shows how to remove all indexes in a collection:
86129

87-
.. literalinclude:: /includes/usage-examples/index-code-examples.cpp
88-
:start-after: start-remove-all-indexes
89-
:end-before: end-remove-all-indexes
90-
:language: cpp
91-
:copyable:
130+
.. io-code-block::
131+
:copyable: true
132+
133+
.. input:: /includes/usage-examples/index-code-examples.cpp
134+
:start-after: start-remove-all-indexes
135+
:end-before: end-remove-all-indexes
136+
:language: cpp
137+
:dedent:
138+
139+
.. output::
140+
:language: cli
141+
:visible: false
142+
143+
All indexes removed.
92144

93145
To learn more about removing indexes, see the
94146
:ref:`cpp-indexes-remove` section of the Work With Indexes guide.

source/indexes/compound-index.txt

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
.. _cpp-compound-index:
2+
3+
================
4+
Compound Indexes
5+
================
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 2
11+
:class: singlecol
12+
13+
.. facet::
14+
:name: genre
15+
:values: reference
16+
17+
.. meta::
18+
:keywords: index, query, optimization, efficiency
19+
20+
Overview
21+
--------
22+
23+
**Compound indexes** are indexes that hold references to multiple
24+
fields within a collection's documents. These indexes improve multi-field query and
25+
sort performance.
26+
27+
To create a compound index, call the ``create_index()`` method and specify a document containing the following information:
28+
29+
- Fields on which to create the index.
30+
- Sort order for the indexed values. Use``1`` for ascending or ``-1`` for descending.
31+
32+
Sample Data
33+
~~~~~~~~~~~
34+
35+
The examples in this guide use the ``movies`` collection in the ``sample_mflix``
36+
database from the :atlas:`Atlas sample datasets </sample-data>`. To access this collection
37+
from your C++ application, instantiate a ``mongocxx::client`` that connects to an Atlas cluster
38+
and assign the following values to your ``db`` and ``collection`` variables:
39+
40+
.. literalinclude:: /includes/indexes/indexes.cpp
41+
:start-after: start-db-coll
42+
:end-before: end-db-coll
43+
:language: cpp
44+
:copyable:
45+
:dedent:
46+
47+
To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the
48+
:atlas:`Get Started with Atlas </getting-started>` guide.
49+
50+
Create Compound Index
51+
---------------------
52+
53+
The following example creates an ascending compound index on the ``title`` and ``year`` fields:
54+
55+
.. literalinclude:: /includes/indexes/indexes.cpp
56+
:start-after: start-index-compound
57+
:end-before: end-index-compound
58+
:language: cpp
59+
:copyable:
60+
:dedent:
61+
62+
The following query is covered by the index
63+
created in the preceding code example:
64+
65+
.. io-code-block::
66+
:copyable: true
67+
68+
.. input:: /includes/indexes/indexes.cpp
69+
:start-after: start-index-compound-query
70+
:end-before: end-index-compound-query
71+
:language: cpp
72+
:dedent:
73+
74+
.. output::
75+
:language: cli
76+
:visible: false
77+
78+
{ "_id" :..., "plot" : "Peter Pan enters the nursery of the Darling children...",
79+
..., "year" : 1924, "imdb" : ..., "type", "movie",...}
80+
81+
Additional Information
82+
----------------------
83+
84+
To view runnable examples that demonstrate how to manage indexes, see
85+
:ref:`cpp-indexes`.
86+
87+
To learn more about indexes, see the following resources in the {+mdb-server+} manual:
88+
89+
- :manual:`Indexes </indexes>`
90+
- :manual:`Compound Indexes </core/index-compound>`
91+
92+
API Documentation
93+
~~~~~~~~~~~~~~~~~
94+
95+
To learn more about the methods discussed in this guide, see the
96+
following API documentation:
97+
98+
- `create_index() <{+api+}/classmongocxx_1_1v__noabi_1_1collection.html#a39cf05fd8da3a7993929c8bfd3de9b46>`__
99+
- `find_one() <{+api+}/classmongocxx_1_1v__noabi_1_1collection.html#a85f4d18d0d3bb3426109a445196ac587>`__

0 commit comments

Comments
 (0)