MongoDB的C#GeoNear查询构造 [英] MongoDb C# GeoNear Query Construction

查看:714
本文介绍了MongoDB的C#GeoNear查询构造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何查询的MongoDB使用C#驱动程序和 GeoNear 法附近的地理点?



下面的回报点,一个不正确的距离值:

  VAR的结果= myCollection.GeoNear(
Query.GT(ExpiresOn 现在),//只是最近值
纬度,
经度,
20
);



我怀疑我应该告诉蒙戈在双[] 位置查询字段,但我不知道查询语法。


解决方案

发现通过<一个答案HREF =http://www.pastie.org/1916916>这个和



  VAR earthRadius = 6378.0; //公里
VAR rangeInKm = 3000.0; //公里

myCollection.EnsureIndex(IndexKeys.GeoSpatial(位置));

=近
Query.GT(ExpiresOn,如今)VAR;

VAR选项= GeoNearOptions
.SetMaxDistance(rangeInKm / earthRadius / *弧度* /)
.SetSpherical(真);

VAR的结果= myCollection.GeoNear(
附近,
request.Longitude,//注意顺序
request.Latitude,// [LNG,纬度] $ b $张使用b 200,
选项
);


How do I query MongoDB for nearby geographic points using the C# driver and the GeoNear method?

The following returns points with an incorrect Distance value:

var results = myCollection.GeoNear(
    Query.GT("ExpiresOn", now), // only recent values
    latitude,
    longitude,
    20
);

I suspect I should be telling Mongo to query on the double[] Location field, but I don't know the query syntax.

解决方案

Found the answer via this and this:

var earthRadius = 6378.0; // km
var rangeInKm = 3000.0; // km

myCollection.EnsureIndex(IndexKeys.GeoSpatial("Location"));

var near =
    Query.GT("ExpiresOn", now);

var options = GeoNearOptions
    .SetMaxDistance(rangeInKm / earthRadius /* to radians */)
    .SetSpherical(true);

var results = myCollection.GeoNear(
    near,
    request.Longitude, // note the order
    request.Latitude,  // [lng, lat]
    200,
    options
);

这篇关于MongoDB的C#GeoNear查询构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆