MongoDB C++ Driver  legacy-1.0.6
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  // An enumeration of symbolic names for index types.
32  enum IndexType {
33  kIndexTypeAscending,
34  kIndexTypeDescending,
35  kIndexTypeText,
36  kIndexTypeGeo2D,
37  kIndexTypeGeoHaystack,
38  kIndexTypeGeo2DSphere,
39  kIndexTypeHashed,
40  };
41 
42  // The values to be encoded in BSON for each type of index.
43  static const int kIndexValAscending = 1;
44  static const int kIndexValDescending = -1;
45  static const char kIndexValText[];
46  static const char kIndexValGeo2D[];
47  static const char kIndexValGeoHaystack[];
48  static const char kIndexValGeo2DSphere[];
49  static const char kIndexValHashed[];
50 
52  IndexSpec();
53 
54  //
55  // Methods for adding keys. Methods on this class will prevent you from adding a given
56  // key multiple times. Constraints on the validity of compound indexes are not enforced
57  // here.
58  //
59 
61  IndexSpec& addKey(const StringData& field, IndexType type = kIndexTypeAscending);
62 
68  IndexSpec& addKey(const BSONElement& fieldAndType);
69 
71  typedef std::vector<std::pair<std::string, IndexType> > KeyVector;
72  IndexSpec& addKeys(const KeyVector& keys);
73 
75  IndexSpec& addKeys(const BSONObj& keys);
76 
77 
78  //
79  // Methods for adding options. As for keys, duplicated settings are checked and will
80  // raise an error.
81  //
82 
83  //
84  // Common index options
85  //
86 
90  IndexSpec& background(bool value = true);
91 
95  IndexSpec& unique(bool value = true);
96 
97 
99  IndexSpec& name(const StringData& name);
100 
104  MONGO_CLIENT_DEPRECATED("deprecated in MongoDB 2.8")
105  IndexSpec& dropDuplicates(bool value = true);
106 
109  IndexSpec& dropDuplicatesDeprecated(bool value = true);
110 
114  IndexSpec& sparse(bool value = true);
115 
120  IndexSpec& expireAfterSeconds(int value);
121 
126  IndexSpec& version(int value);
127 
128 
129  //
130  // Text options
131  //
132 
134  IndexSpec& textWeights(const BSONObj& value);
135 
137  IndexSpec& textDefaultLanguage(const StringData& value);
138 
140  IndexSpec& textLanguageOverride(const StringData& value);
141 
145  IndexSpec& textIndexVersion(int value);
146 
147 
148  //
149  // 2D Sphere Options
150  //
151 
155  IndexSpec& geo2DSphereIndexVersion(int value);
156 
157 
158  //
159  // Geo2D Options
160  //
161 
163  IndexSpec& geo2DBits(int value);
164 
166  IndexSpec& geo2DMin(double value);
167 
169  IndexSpec& geo2DMax(double value);
170 
171 
172  //
173  // Geo Haystack Options
174  //
175 
177  IndexSpec& geoHaystackBucketSize(double value);
178 
179 
180  //
181  // Support for adding generic options. This is here so that if new index options
182  // become available on the server those options can be set independently of the
183  // named methods above.
184  //
185 
187  IndexSpec& addOption(const BSONElement& option);
188 
190  IndexSpec& addOptions(const BSONObj& options);
191 
198  std::string name() const;
199 
201  BSONObj toBSON() const;
202 
203 private:
204  void _rename();
205 
206  std::string _name;
207  bool _dynamicName;
208 
209  mutable BSONObjBuilder _keys;
210  mutable BSONObjBuilder _options;
211 };
212 
213 } // 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:71
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