Query.Near 的 maxDistance 参数不起作用

发布于 2024-11-08 04:20:59 字数 3232 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

年少掌心 2024-11-15 04:20:59

这里有很多问题:

  1. 几乎所有 Mongo 地理空间查询都首先采用 x 坐标(经度)。
  2. 当您不使用球形查询时,Mongo 仅计算简单的笛卡尔距离,并且您需要传递的距离与坐标的单位相同(可能是十进制度)。
  3. 如果您确实使用球形查询(无论是附近还是内部),您确实需要以弧度指定距离,但要将英里转换为弧度,您需要除以比 62.1868289 大得多的数字。除以地球半径(约 3,959 英里)即可将弧度转换为英里。

您上面执行的转换实际上更接近于从英里到度的转换(将英里距离除以大约 69)。

A bunch of problems here:

  1. Pretty much all the Mongo geospatial queries take the x-coordinate (longitude) first.
  2. When you're not using the spherical queries, Mongo just calculates simple Cartesian distances, and the distance you need to pass is in the same units as your coordinates (likely decimal degrees).
  3. If you do use a spherical query (either near or within), you do need to specify your distance in radians, but to convert from miles to radians, you need to divide by a much bigger number than 62.1868289. You convert from radians to miles by dividing by the Earth's radius (about 3,959 miles).

The conversion you performed above is actually much closer to the conversion from miles to degrees (divide the distance in miles by about 69).

‘画卷フ 2024-11-15 04:20:59

确定问题出在 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文