[CSHARP-2364] Documentation lacking for geospatial queries Created: 24/Aug/18  Updated: 31/Mar/22

Status: Backlog
Project: C# Driver
Component/s: Documentation
Affects Version/s: 2.7.0
Fix Version/s: None

Type: Improvement Priority: Minor - P4
Reporter: Matt Welke Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: documentation-needed
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

I had an issue recently figuring out how to perform a $nearSphere query with the strongly-typed methods available in the driver. I could get it to work by creating a filter with a string containing the JSON query syntax, but then the compiler would be unable to help me. I ended up creating a StackOverflow question for it, until I eventually solved it myself by tinkering: https://stackoverflow.com/questions/51987894/why-do-i-get-a-valid-but-inaccurate-nearsphere-query-when-using-the-typed-metho

I'm going to copy and paste parts of my StackOverflow question here to describe the issue:


 
{{var query = _col.Find($"{{ location: {{ $nearSphere: {{ $geometry: {{ type: \"Point\", coordinates: [

{lon}

, {lat} ] }}, $maxDistance: {rad} }} }} }}");}}

This worked fine. My query was accurate. But wherever possible, I'd like to use the typed methods instead so the compiler can help me in the future, so I looked online to figure out how to code it, and I came up with this after reading a previous StackOverflow answer:
 
var filter = Builders<Node>.Filter.NearSphere(n => n.Location, lon, lat, rad);var query2 = _col.Find<Node>(filter);

The problem is that these generate different queries under the hood, and the query generated by the latter is inaccurate. It catches many more points.

{{}}

By calling .ToString() on the queries generated, I see that the first one turns into...
 
{{{find({ "location" : { "$nearSphere" : { "$geometry" :

{ "type" : "Point", "coordinates" : [1, 2] }

, "$maxDistance" : 3 } } })}}}

...which produces an accurate result).

And the second one turns into...
 
{{{find({ "location" :

{ "$nearSphere" : [1.0, 2.0], "$maxDistance" : 3.0 }

})}}}

..which has a different form and does not produce an accurate result.

[and then when I found the solution...]

The code for performing this type of query with version 2.7 of the C# driver is:
 
var filterPoint = GeoJson.Point(new GeoJson2DCoordinates(lon, lat));var filter = new FilterDefinitionBuilder<Node>().NearSphere(n => n.Location, filterPoint, rad);var query = _col.Find<Node>(filter);

{{}}

I also had to make sure I was using the MongoDB GeoJSON point data type in my model (the class I use to perform the queries):

{{}}
 
{{public class Node{[BsonElement("_id")]public long Id

{ get; set; }[BsonElement("location")]public GeoJsonPoint<GeoJson2DCoordinates> Location { get; set; }

[BsonElement("tags")]public BsonDocument Tags { get; set; }}}}{{}}

{{}}

}}{{



 Comments   
Comment by Ian Whalen (Inactive) [ 17/Dec/18 ]

Thanks for opening this report Matt.

Comment by Matt Welke [ 24/Aug/18 ]

I see the formatting got messy when I copy and pasted that here. I recommend checking out the StackOverflow link to see the issue more clearly.

Generated at Wed Feb 07 21:42:21 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.