MongoDB C++ Driver  legacy-1.1.2
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 
129  IndexSpec& partialFilterExpression(const BSONObj& value);
130 
131  //
132  // Text options
133  //
134 
136  IndexSpec& textWeights(const BSONObj& value);
137 
139  IndexSpec& textDefaultLanguage(const StringData& value);
140 
142  IndexSpec& textLanguageOverride(const StringData& value);
143 
147  IndexSpec& textIndexVersion(int value);
148 
149 
150  //
151  // 2D Sphere Options
152  //
153 
157  IndexSpec& geo2DSphereIndexVersion(int value);
158 
159 
160  //
161  // Geo2D Options
162  //
163 
165  IndexSpec& geo2DBits(int value);
166 
168  IndexSpec& geo2DMin(double value);
169 
171  IndexSpec& geo2DMax(double value);
172 
173 
174  //
175  // Geo Haystack Options
176  //
177 
179  IndexSpec& geoHaystackBucketSize(double value);
180 
181 
182  //
183  // Support for adding generic options. This is here so that if new index options
184  // become available on the server those options can be set independently of the
185  // named methods above.
186  //
187 
189  IndexSpec& addOption(const BSONElement& option);
190 
192  IndexSpec& addOptions(const BSONObj& options);
193 
200  std::string name() const;
201 
203  BSONObj toBSON() const;
204 
205 private:
206  void _rename();
207 
208  std::string _name;
209  bool _dynamicName;
210 
211  mutable BSONObjBuilder _keys;
212  mutable BSONObjBuilder _options;
213 };
214 
215 } // namespace mongo
A StringData object wraps a 'const string&' or a 'const char*' without copying its contents...
Definition: string_data.h:43
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
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