我正在使用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()
答案 0 :(得分:5)
这里有很多问题:
您在上面执行的转换实际上更接近从英里到度数的转换(将距离以英里为单位除以69)。
答案 1 :(得分:1)
确定问题是在C#驱动程序中还是在查询本身中的一种好方法是首先使查询在mongo shell中运行。然后,您可以使用C#Query构建器编写等效查询。作为最终检查,您可以看到等效的JSON查询的内容如下:
var query = Query.Near(...); var json = query.ToJson();
您能提供一些您认为应该退回的样本文件吗?