我正在使用 MongoDB 的 C# 驱动程序,并试图让 Query.Near 工作,以便在距离中心点 5、10、25 或 50 英里范围内返回待售房屋。这是查询:
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();
我将英里半径除以 62.1868289 以获得弧度并将其输入到查询中,但是无论我传入什么弧度值,它都会返回相同数量的待售房屋。我还尝试将球形参数设置为 true 和使用 Query.WithinCircle,但是两者都没有更好的效果。
我正在使用最新的 C# 驱动程序 (1.0.0.4098),这是 C# 驱动程序中的错误,MongoDB 中的错误,还是我在这里遗漏了什么?
以下是查询的样子:
5英里外(近):
db.Listing.find({ "Coordinates" : { "$near" : [39.4812172, -76.6438598], "$maxDistance" : 0.072463768115942032 } });
10英里外(近):
db.Listing.find({ "Coordinates" : { "$near" : [39.4812172, -76.6438598], "$maxDistance" : 0.14492753623188406 } });
5 英里外(近球形):
db.Listing.find({ "Coordinates" : { "$nearSphere" : [39.4812172, -76.6438598], "$maxDistance" : 0.0012629451881788331 } });
10 英里外(近球形):
db.Listing.find({ "Coordinates" : { "$nearSphere" : [39.4812172, -76.6438598], "$maxDistance" : 0.0025258903763576662 } });
这是我的测试用例,无论我在 5 英里外还是 25 英里外通过,它都会返回相同的结果数:
[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()