MongoDB C++ Driver  legacy-1.1.2
queryutils.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 "mongo/client/export_macros.h"
19 #include "mongo/db/jsobj.h"
20 #include "mongo/geo/boundingbox.h"
21 #include "mongo/geo/constants.h"
22 #include "mongo/geo/geoobj.h"
23 
24 namespace mongo {
25 namespace geo {
26 
27 template <typename TCoordinates>
28 inline BSONObj GEOQUERY(const char* op, const GeoObj<TCoordinates>& geoObj) {
29  return BSON(op << BSON("$geometry" << geoObj.toBSON()));
30 }
31 
32 template <typename TCoordinates>
33 inline BSONObj WITHINQUERY(const GeoObj<TCoordinates>& geoObj) {
34  return GEOQUERY("$geoWithin", geoObj);
35 }
36 
37 template <typename TCoordinates>
38 inline BSONObj WITHINQUERY(const BoundingBox<TCoordinates>& geoBB) {
39  return BSON("$geoWithin" << BSON("$box" << geoBB.toNestedBSONArray()));
40 }
41 
42 template <typename TCoordinates>
43 inline BSONObj INTERSECTSQUERY(const GeoObj<TCoordinates>& geoObj) {
44  return GEOQUERY("$geoIntersects", geoObj);
45 }
46 
47 template <typename TCoordinates>
48 inline BSONObj NEARQUERY(const GeoObj<TCoordinates>& geoObj) {
49  return GEOQUERY("$near", geoObj);
50 }
51 
52 template <typename TCoordinates>
53 inline BSONObj NEARQUERY(const GeoObj<TCoordinates>& geoObj, double maxDistance) {
54  uassert(0, "$maxDistance param to $near query must be non-negative.", maxDistance >= 0.0);
55  return BSON("$near" << BSON("$geometry" << geoObj.toBSON() << "$maxDistance" << maxDistance));
56 }
57 
58 template <typename TCoordinates>
59 inline BSONObj NEARSPHEREQUERY(const GeoObj<TCoordinates>& geoObj) {
60  return GEOQUERY("$nearSphere", geoObj);
61 }
62 
63 template <typename TCoordinates>
64 inline BSONObj NEARSPHEREQUERY(const GeoObj<TCoordinates>& geoObj, double maxDistance) {
65  uassert(0, "$maxDistance param to $nearSphere query must be non-negative.", maxDistance >= 0.0);
66  return BSON(
67  "$nearSphere" << BSON("$geometry" << geoObj.toBSON() << "$maxDistance" << maxDistance));
68 }
69 
70 } // namespace geo
71 } // namespace mongo
Utility functions for parsing numbers from strings.
Definition: compare_numbers.h:20
BSON classes.