MongoDB C++ Driver  legacy-1.0.5
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
index_spec.h
1 /* Copyright 2014 MongoDB Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include <string>
19 #include <vector>
20 #include <utility>
21 
22 #include "mongo/client/export_macros.h"
23 #include "mongo/db/jsobj.h"
24 
25 namespace mongo {
26 
27  class StringData;
28 
29  class MONGO_CLIENT_API IndexSpec {
30  public:
31 
32  // An enumeration of symbolic names for index types.
33  enum IndexType {
34  kIndexTypeAscending,
35  kIndexTypeDescending,
36  kIndexTypeText,
37  kIndexTypeGeo2D,
38  kIndexTypeGeoHaystack,
39  kIndexTypeGeo2DSphere,
40  kIndexTypeHashed,
41  };
42 
43  // The values to be encoded in BSON for each type of index.
44  static const int kIndexValAscending = 1;
45  static const int kIndexValDescending = -1;
46  static const char kIndexValText[];
47  static const char kIndexValGeo2D[];
48  static const char kIndexValGeoHaystack[];
49  static const char kIndexValGeo2DSphere[];
50  static const char kIndexValHashed[];
51 
53  IndexSpec();
54 
55  //
56  // Methods for adding keys. Methods on this class will prevent you from adding a given
57  // key multiple times. Constraints on the validity of compound indexes are not enforced
58  // here.
59  //
60 
62  IndexSpec& addKey(const StringData& field, IndexType type = kIndexTypeAscending);
63 
69  IndexSpec& addKey(const BSONElement& fieldAndType);
70 
72  typedef std::vector<std::pair<std::string, IndexType> > KeyVector;
73  IndexSpec& addKeys(const KeyVector& keys);
74 
76  IndexSpec& addKeys(const BSONObj& keys);
77 
78 
79  //
80  // Methods for adding options. As for keys, duplicated settings are checked and will
81  // raise an error.
82  //
83 
84  //
85  // Common index options
86  //
87 
91  IndexSpec& background(bool value = true);
92 
96  IndexSpec& unique(bool value = true);
97 
98 
100  IndexSpec& name(const StringData& name);
101 
105  MONGO_CLIENT_DEPRECATED("deprecated in MongoDB 2.8")
106  IndexSpec& dropDuplicates(bool value = true);
107 
110  IndexSpec& dropDuplicatesDeprecated(bool value = true);
111 
115  IndexSpec& sparse(bool value = true);
116 
121  IndexSpec& expireAfterSeconds(int value);
122 
127  IndexSpec& version(int value);
128 
129 
130  //
131  // Text options
132  //
133 
135  IndexSpec& textWeights(const BSONObj& value);
136 
138  IndexSpec& textDefaultLanguage(const StringData& value);
139 
141  IndexSpec& textLanguageOverride(const StringData& value);
142 
146  IndexSpec& textIndexVersion(int value);
147 
148 
149  //
150  // 2D Sphere Options
151  //
152 
156  IndexSpec& geo2DSphereIndexVersion(int value);
157 
158 
159  //
160  // Geo2D Options
161  //
162 
164  IndexSpec& geo2DBits(int value);
165 
167  IndexSpec& geo2DMin(double value);
168 
170  IndexSpec& geo2DMax(double value);
171 
172 
173  //
174  // Geo Haystack Options
175  //
176 
178  IndexSpec& geoHaystackBucketSize(double value);
179 
180 
181  //
182  // Support for adding generic options. This is here so that if new index options
183  // become available on the server those options can be set independently of the
184  // named methods above.
185  //
186 
188  IndexSpec& addOption(const BSONElement& option);
189 
191  IndexSpec& addOptions(const BSONObj& options);
192 
199  std::string name() const;
200 
202  BSONObj toBSON() const;
203 
204  private:
205  void _rename();
206 
207  std::string _name;
208  bool _dynamicName;
209 
210  mutable BSONObjBuilder _keys;
211  mutable BSONObjBuilder _options;
212  };
213 
214 } // namespace mongo
the main MongoDB namespace
Definition: bulk_operation_builder.h:24
BSON classes.
std::vector< std::pair< std::string, IndexType > > KeyVector
Add all components in the provided key vector to the index descriptor.
Definition: index_spec.h:72
Utility for creating a BSONObj.
Definition: bsonobjbuilder.h:53
BSONElement represents an "element" in a BSONObj.
Definition: bsonelement.h:55
Definition: index_spec.h:29
C++ representation of a "BSON" object – that is, an extended JSON-style object in a binary represent...
Definition: bsonobj.h:78