Skip to content

Commit 70e2849

Browse files
committed
CSHARP-1221: Fix incorrect method signature for $geoWithin $box
1 parent bd062ec commit 70e2849

File tree

2 files changed

+38
-23
lines changed

2 files changed

+38
-23
lines changed

src/MongoDB.Driver.Tests/FilterDefinitionBuilderTests.cs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,8 @@ public void GeoIntersects_Typed()
212212
GeoJson.Geographic(41, 19),
213213
GeoJson.Geographic(40, 18));
214214

215-
Assert(subject.GeoIntersects(x => x.Age, poly), "{age: {$geoIntersects: {$geometry: {type: 'Polygon', coordinates: [[[40.0, 18.0], [40.0, 19.0], [41.0, 19.0], [40.0, 18.0]]]}}}}");
216-
Assert(subject.GeoIntersects("Age", poly), "{age: {$geoIntersects: {$geometry: {type: 'Polygon', coordinates: [[[40.0, 18.0], [40.0, 19.0], [41.0, 19.0], [40.0, 18.0]]]}}}}");
215+
Assert(subject.GeoIntersects(x => x.Location, poly), "{loc: {$geoIntersects: {$geometry: {type: 'Polygon', coordinates: [[[40.0, 18.0], [40.0, 19.0], [41.0, 19.0], [40.0, 18.0]]]}}}}");
216+
Assert(subject.GeoIntersects("Location", poly), "{loc: {$geoIntersects: {$geometry: {type: 'Polygon', coordinates: [[[40.0, 18.0], [40.0, 19.0], [41.0, 19.0], [40.0, 18.0]]]}}}}");
217217
}
218218

219219
[Test]
@@ -226,8 +226,8 @@ public void GeoIntersects_Typed_with_GeoJson()
226226
GeoJson.Geographic(41, 19),
227227
GeoJson.Geographic(40, 18));
228228

229-
Assert(subject.GeoIntersects(x => x.Age, poly), "{age: {$geoIntersects: {$geometry: {type: 'Polygon', coordinates: [[[40.0, 18.0], [40.0, 19.0], [41.0, 19.0], [40.0, 18.0]]]}}}}");
230-
Assert(subject.GeoIntersects("Age", poly), "{age: {$geoIntersects: {$geometry: {type: 'Polygon', coordinates: [[[40.0, 18.0], [40.0, 19.0], [41.0, 19.0], [40.0, 18.0]]]}}}}");
229+
Assert(subject.GeoIntersects(x => x.Location, poly), "{loc: {$geoIntersects: {$geometry: {type: 'Polygon', coordinates: [[[40.0, 18.0], [40.0, 19.0], [41.0, 19.0], [40.0, 18.0]]]}}}}");
230+
Assert(subject.GeoIntersects("Location", poly), "{loc: {$geoIntersects: {$geometry: {type: 'Polygon', coordinates: [[[40.0, 18.0], [40.0, 19.0], [41.0, 19.0], [40.0, 18.0]]]}}}}");
231231
}
232232

233233
[Test]
@@ -254,25 +254,25 @@ public void GeoWithin_Typed()
254254
GeoJson.Geographic(41, 19),
255255
GeoJson.Geographic(40, 18));
256256

257-
Assert(subject.GeoWithin(x => x.Age, poly), "{age: {$geoWithin: {$geometry: {type: 'Polygon', coordinates: [[[40.0, 18.0], [40.0, 19.0], [41.0, 19.0], [40.0, 18.0]]]}}}}");
258-
Assert(subject.GeoWithin("Age", poly), "{age: {$geoWithin: {$geometry: {type: 'Polygon', coordinates: [[[40.0, 18.0], [40.0, 19.0], [41.0, 19.0], [40.0, 18.0]]]}}}}");
257+
Assert(subject.GeoWithin(x => x.Location, poly), "{loc: {$geoWithin: {$geometry: {type: 'Polygon', coordinates: [[[40.0, 18.0], [40.0, 19.0], [41.0, 19.0], [40.0, 18.0]]]}}}}");
258+
Assert(subject.GeoWithin("Location", poly), "{loc: {$geoWithin: {$geometry: {type: 'Polygon', coordinates: [[[40.0, 18.0], [40.0, 19.0], [41.0, 19.0], [40.0, 18.0]]]}}}}");
259259
}
260260

261261
[Test]
262262
public void GeoWithinBox()
263263
{
264264
var subject = CreateSubject<BsonDocument>();
265265

266-
Assert(subject.GeoWithinBox("x", 10, 20), "{x: {$geoWithin: {$box: [10.0, 20.0]}}}");
266+
Assert(subject.GeoWithinBox("x", 10, 20, 30, 40), "{x: {$geoWithin: {$box: [[10.0, 20.0], [30.0, 40.0]]}}}");
267267
}
268268

269269
[Test]
270270
public void GeoWithinBox_Typed()
271271
{
272272
var subject = CreateSubject<Person>();
273273

274-
Assert(subject.GeoWithinBox(x => x.Age, 10, 20), "{age: {$geoWithin: {$box: [10.0, 20.0]}}}");
275-
Assert(subject.GeoWithinBox("Age", 10, 20), "{age: {$geoWithin: {$box: [10.0, 20.0]}}}");
274+
Assert(subject.GeoWithinBox(x => x.Location, 10, 20, 30, 40), "{loc: {$geoWithin: {$box: [[10.0, 20.0], [30.0, 40.0]]}}}");
275+
Assert(subject.GeoWithinBox("Location", 10, 20, 30, 40), "{loc: {$geoWithin: {$box: [[10.0, 20.0], [30.0, 40.0]]}}}");
276276
}
277277

278278
[Test]
@@ -288,8 +288,8 @@ public void GeoWithinCenter_Typed()
288288
{
289289
var subject = CreateSubject<Person>();
290290

291-
Assert(subject.GeoWithinCenter(x => x.Age, 10, 20, 30), "{age: {$geoWithin: {$center: [[10.0, 20.0], 30.0]}}}");
292-
Assert(subject.GeoWithinCenter("Age", 10, 20, 30), "{age: {$geoWithin: {$center: [[10.0, 20.0], 30.0]}}}");
291+
Assert(subject.GeoWithinCenter(x => x.Location, 10, 20, 30), "{loc: {$geoWithin: {$center: [[10.0, 20.0], 30.0]}}}");
292+
Assert(subject.GeoWithinCenter("Location", 10, 20, 30), "{loc: {$geoWithin: {$center: [[10.0, 20.0], 30.0]}}}");
293293
}
294294

295295
[Test]
@@ -305,8 +305,8 @@ public void GeoWithinCenterSphere_Typed()
305305
{
306306
var subject = CreateSubject<Person>();
307307

308-
Assert(subject.GeoWithinCenterSphere(x => x.Age, 10, 20, 30), "{age: {$geoWithin: {$centerSphere: [[10.0, 20.0], 30.0]}}}");
309-
Assert(subject.GeoWithinCenterSphere("Age", 10, 20, 30), "{age: {$geoWithin: {$centerSphere: [[10.0, 20.0], 30.0]}}}");
308+
Assert(subject.GeoWithinCenterSphere(x => x.Location, 10, 20, 30), "{loc: {$geoWithin: {$centerSphere: [[10.0, 20.0], 30.0]}}}");
309+
Assert(subject.GeoWithinCenterSphere("Location", 10, 20, 30), "{loc: {$geoWithin: {$centerSphere: [[10.0, 20.0], 30.0]}}}");
310310
}
311311

312312
[Test]
@@ -322,8 +322,8 @@ public void GeoWithinPolygon_Typed()
322322
{
323323
var subject = CreateSubject<Person>();
324324

325-
Assert(subject.GeoWithinPolygon(x => x.Age, new[,] { { 1d, 2d }, { 3d, 4d } }), "{age: {$geoWithin: {$polygon: [[1.0, 2.0], [3.0, 4.0]]}}}");
326-
Assert(subject.GeoWithinPolygon("Age", new[,] { { 1d, 2d }, { 3d, 4d } }), "{age: {$geoWithin: {$polygon: [[1.0, 2.0], [3.0, 4.0]]}}}");
325+
Assert(subject.GeoWithinPolygon(x => x.Location, new[,] { { 1d, 2d }, { 3d, 4d } }), "{loc: {$geoWithin: {$polygon: [[1.0, 2.0], [3.0, 4.0]]}}}");
326+
Assert(subject.GeoWithinPolygon("Location", new[,] { { 1d, 2d }, { 3d, 4d } }), "{loc: {$geoWithin: {$polygon: [[1.0, 2.0], [3.0, 4.0]]}}}");
327327
}
328328

329329
[Test]
@@ -804,6 +804,9 @@ private class Person
804804

805805
[BsonElement("pets")]
806806
public Pet[] Pets { get; set; }
807+
808+
[BsonElement("loc")]
809+
public int[] Location { get; set; }
807810
}
808811

809812
private class Pet

src/MongoDB.Driver/FilterDefinitionBuilder.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -420,24 +420,36 @@ public FilterDefinition<TDocument> GeoWithin<TCoordinates>(Expression<Func<TDocu
420420
/// Creates a geo within box filter.
421421
/// </summary>
422422
/// <param name="field">The field.</param>
423-
/// <param name="x">The x.</param>
424-
/// <param name="y">The y.</param>
423+
/// <param name="lowerLeftX">The lower left x.</param>
424+
/// <param name="lowerLeftY">The lower left y.</param>
425+
/// <param name="upperRightX">The upper right x.</param>
426+
/// <param name="upperRightY">The upper right y.</param>
425427
/// <returns>A geo within box filter.</returns>
426-
public FilterDefinition<TDocument> GeoWithinBox(FieldDefinition<TDocument> field, double x, double y)
428+
public FilterDefinition<TDocument> GeoWithinBox(FieldDefinition<TDocument> field, double lowerLeftX, double lowerLeftY, double upperRightX, double upperRightY)
427429
{
428-
return new OperatorFilterDefinition<TDocument>("$geoWithin", field, new BsonDocument("$box", new BsonArray { x, y }));
430+
return new OperatorFilterDefinition<TDocument>(
431+
"$geoWithin",
432+
field,
433+
new BsonDocument("$box",
434+
new BsonArray
435+
{
436+
new BsonArray { lowerLeftX, lowerLeftY },
437+
new BsonArray { upperRightX, upperRightY }
438+
}));
429439
}
430440

431441
/// <summary>
432442
/// Creates a geo within box filter.
433443
/// </summary>
434444
/// <param name="field">The field.</param>
435-
/// <param name="x">The x.</param>
436-
/// <param name="y">The y.</param>
445+
/// <param name="lowerLeftX">The lower left x.</param>
446+
/// <param name="lowerLeftY">The lower left y.</param>
447+
/// <param name="upperRightX">The upper right x.</param>
448+
/// <param name="upperRightY">The upper right y.</param>
437449
/// <returns>A geo within box filter.</returns>
438-
public FilterDefinition<TDocument> GeoWithinBox(Expression<Func<TDocument, object>> field, double x, double y)
450+
public FilterDefinition<TDocument> GeoWithinBox(Expression<Func<TDocument, object>> field, double lowerLeftX, double lowerLeftY, double upperRightX, double upperRightY)
439451
{
440-
return GeoWithinBox(new ExpressionFieldDefinition<TDocument>(field), x, y);
452+
return GeoWithinBox(new ExpressionFieldDefinition<TDocument>(field), lowerLeftX, lowerLeftY, upperRightX, upperRightY);
441453
}
442454

443455
/// <summary>

0 commit comments

Comments
 (0)