Query.Near 的 maxDistance 参数不起作用
我正在使用 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 以获得弧度并将其输入到查询中,但是无论我传入什么弧度值,它都会返回相同数量的待售房屋。我还尝试了两种设置spherical param 为 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()
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有很多问题:
您上面执行的转换实际上更接近于从英里到度的转换(将英里距离除以大约 69)。
A bunch of problems here:
The conversion you performed above is actually much closer to the conversion from miles to degrees (divide the distance in miles by about 69).
确定问题出在 C# 驱动程序还是查询本身的一个好方法是首先让查询在 mongo shell 中运行。然后,您可以使用 C# 查询生成器编写等效的查询。作为最后的检查,您可以看到等效的 JSON 查询如下:
var query = Query.Near(...);
var json = query.ToJson();
您能否提供一些您认为应该退回的样本文件?
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?