Skip to content

Commit 5384645

Browse files
committed
CXX-517 nest $maxDistance inside $near and $nearSphere
1 parent 4c8ecca commit 5384645

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

src/mongo/client/examples/geojson_demo.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ void queryGeoData(DBClientBase* conn) {
131131
Query q5 = MONGO_QUERY(kLocField << WITHINQUERY(poly.getBoundingBox()));
132132
Query q6 = MONGO_QUERY(kLocField << INTERSECTSQUERY(poly));
133133

134+
geo::Coordinates2DGeographic coords(1, 2);
135+
geo::coords2dgeographic::Point point(coords);
136+
137+
Query nearQuery = MONGO_QUERY(kLocField << NEARQUERY(point, 0.5));
138+
Query nearSphereQuery = MONGO_QUERY(kLocField << NEARSPHEREQUERY(point, 0.5));
139+
134140
cout << "*** Testing LineString ***" << endl;
135141

136142
auto_ptr<DBClientCursor> cursor = conn->query(kDbCollectionName, q);
@@ -173,6 +179,18 @@ void queryGeoData(DBClientBase* conn) {
173179
cout << cursor6->next().toString() << endl;
174180
cout << "---------------" << endl;
175181

182+
auto_ptr<DBClientCursor> nearQueryCursor = conn->query(kDbCollectionName, nearQuery);
183+
cout << "Results from NEAR" << endl;
184+
while (nearQueryCursor->more())
185+
cout << nearQueryCursor->next().toString() << endl;
186+
cout << "---------------" << endl;
187+
188+
auto_ptr<DBClientCursor> nearSphereQueryCursor = conn->query(kDbCollectionName, nearSphereQuery);
189+
cout << "Results from NEARSPHERE" << endl;
190+
while (nearSphereQueryCursor->more())
191+
cout << nearSphereQueryCursor->next().toString() << endl;
192+
cout << "---------------" << endl;
193+
176194
}
177195

178196
int main( int argc, const char **argv ) {

src/mongo/geo/queryutils.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ namespace geo {
5050
}
5151

5252
template<typename TCoordinates>
53-
inline BSONObj NEARQUERY(const GeoObj<TCoordinates>& geoObj, double distance) {
54-
return BSON("$near" << BSON("$geometry" << geoObj.toBSON()) <<
55-
"$maxDistance" << distance);
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()
56+
<< "$maxDistance" << maxDistance));
5657
}
5758

5859
template<typename TCoordinates>
@@ -61,10 +62,11 @@ namespace geo {
6162
}
6263

6364
template<typename TCoordinates>
64-
inline BSONObj NEARSPHEREQUERY(const GeoObj<TCoordinates>& geoObj,
65-
double distance) {
66-
return BSON("$nearSphere" << BSON("$geometry" << geoObj.toBSON()) <<
67-
"$maxDistance" << distance);
65+
inline BSONObj NEARSPHEREQUERY(const GeoObj<TCoordinates>& geoObj, double maxDistance) {
66+
uassert(0, "$maxDistance param to $nearSphere query must be non-negative.", maxDistance >= 0.0);
67+
return BSON("$nearSphere" << BSON("$geometry" << geoObj.toBSON()
68+
<< "$maxDistance" << maxDistance));
69+
6870
}
6971

7072
} // namespace geo

0 commit comments

Comments
 (0)