mongodbmongodb-.net-driver

maxDistance parameter of Query.Near not working


I'm using MongoDB's C# driver and am trying to get Query.Near to work to return homes for sale within 5, 10, 25, or 50 miles of a center point. Here is the query:

var near = Query.Near("Coordinates", coordinates.Latitude, coordinates.Longitude, find.GetRadiansAway(), false);
    //var near = Query.WithinCircle("Coordinates", coordinates.Latitude, coordinates.Longitude, find.GetRadiansAway(), false);
    var query = Collection().Find(near);
    query.Limit = 1000;
    var listings = query.ToList();

I'm dividing the mile radius by 62.1868289 to get the radians and inputting that into the query, however it's returning the same amount of homes for sale regardless of what radians value I pass in. I also tried both setting the spherical param to true and using Query.WithinCircle, however neither works any better.

I'm using the latest C# driver (1.0.0.4098), is this a bug in the C# driver, a bug in MongoDB, or am I missing something here?

Here are what the queries look like:

5 miles away (near):

db.Listing.find({ "Coordinates" : { "$near" : [39.4812172, -76.6438598], "$maxDistance" : 0.072463768115942032 } });

10 miles away (near):

db.Listing.find({ "Coordinates" : { "$near" : [39.4812172, -76.6438598], "$maxDistance" : 0.14492753623188406 } });

5 miles away (spherical near):

db.Listing.find({ "Coordinates" : { "$nearSphere" : [39.4812172, -76.6438598], "$maxDistance" : 0.0012629451881788331 } });

10 miles away (spherical near):

db.Listing.find({ "Coordinates" : { "$nearSphere" : [39.4812172, -76.6438598], "$maxDistance" : 0.0025258903763576662 } });

Here is my test case that returns the same # of results whether I pass in 5 miles away or 25 miles away:

[Test]
        public void NearTest()
        {
            var isSpherical = true;
            var latitude = 39.4812172;
            var longitude = -76.6438598;
            var milesAway = 5;
            var distance = milesAway / (isSpherical ? 3959.0 : 69.0);

            //search within 5 miles.
            var near = Query.Near("Coordinates", latitude, longitude, distance, isSpherical);
            var collection = ContextWorker.Database.GetCollection<Schemas.Listing.Listing>("Listing");
            var query = collection.Find(near);
            query.Limit = 1000;
            var listings = query.ToList();
            var count1 = listings.Count;
            //Console.WriteLine("query: {0}", query.ToJson());
            Console.WriteLine(count1 + " results returned that are " + milesAway + " miles away");

            //search within 25 miles.
            milesAway = 25;
            distance = milesAway / (isSpherical ? 3959.0 : 69.0);
            near = Query.Near("Coordinates", latitude, longitude, distance, isSpherical);
            query = collection.Find(near);
            query.Limit = 1000;
            listings = query.ToList();
            var count2 = listings.Count;
            //Console.WriteLine("query: {0}", query.ToJson());
            Console.WriteLine(count2 + " results returned that are " + milesAway + " miles away");

            //values should be different.
            Assert.AreNotEqual(count1, count2, "Returned same results for 5 and 25 miles away");
        }

172 results returned that are 5 miles away
172 results returned that are 25 miles away
Test 'Housters.Test.SearchTest.NearTest' failed: 
  Returned same results for 5 and 25 miles away
  Expected: not 172
  But was:  172
    SearchTest.cs(68,0): at Housters.Test.SearchTest.NearTest()

Solution

  • A good way to determine whether a problem is in the C# driver or in the queries themselves is to get the queries working in the mongo shell first. Then you can write the equivalent query using the C# Query builder. As a final check you can see what the equivalent JSON query would be as follows:

    var query = Query.Near(...);
    var json = query.ToJson();
    

    Can you provide some sample documents that you think should be returned?