如何在2.1 MongoDB C#驱动程序中使用地理空间查询? [英] How do I use a geospatial query in the 2.1 MongoDB C# driver?

查看:62
本文介绍了如何在2.1 MongoDB C#驱动程序中使用地理空间查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为此努力了好几天.我有一个非常简单的查询,试图在C#中运行,它在外壳中看起来像这样.

I've been banging my head on this one for days. I have a very simple query I'm trying to run in C#, it looks like this in the shell.

db.runCommand({geoNear: "items", near: {type: "Point", coordinates : [-111.283344899999, 47.4941836]}, spherical : true, distanceMultiplier: 3963.2, maxDistance : 25});

我的收藏看起来像这样

{    
  "_id" : ObjectId(),    
  "Title" : "arst",    
  "Description" : "<p>arst</p>",    
  "Date" : new Date("11/29/2015 09:28:15"),    
  "Location" : {    
    "type" : "Point",    
    "Coordinates" : [-111.28334489999998, 47.4941836]    
  },    
  "Zip" : "59405"    
}

根据此处的文档 MongoDB C#API文档 MongoDB. Driver.Builders.Query对象现在是旧版.所以当我做这样的事情

According to the docs here MongoDB C# API Docs the MongoDB.Driver.Builders.Query object is now legacy. So when I do something like this

 var point = new GeoJson2DGeographicCoordinates(double.Parse(longitude), double.Parse(latitude)) ;
        var query = Query<Item>.Near(x => x.Location, new GeoJsonPoint<GeoJson2DGeographicCoordinates>(point), distance, true);

        var result = collection.Find(query);

编译器抱怨无法将其从IMongoQuery转换为FilterDefinition.这告诉我,新的2.1库不支持旧版Query<>构建器.但是对于我一生,我无法在引用替代文件的api文档中找到任何地方?

The compiler complains that it can't convert from IMongoQuery to FilterDefinition. This tells me that the legacy Query<> builder isn't supported by the new 2.1 library. But for the life of me I can't find anywhere in the api docs that reference a replacement?

有人可以在2.1 C#驱动程序中执行此简单的地理空间查询时指出正确的方向吗?我很困惑.

Can anyone point me in the right direction on executing this simple geo-spatial query in the 2.1 C# Driver? I'm stumped.

此外,如果没有,shell命令将无法在集合上创建2dsphere索引.这是索引输出.

Also, I do have a 2dsphere index created on the collection, if I didn't the shell command wouldn't work. Here's the index output.

{
            "v" : 1,
            "key" : {
                    "Location.Coordinates" : "2dsphere"
            },
            "name" : "Location.Coordinates_2dsphere",
            "ns" : "ppn.items",
            "2dsphereIndexVersion" : 2
    }

编辑

仔细阅读大量文档后,我想我找到了它.所有示例仍显示旧的Query<>方法,但是新方法似乎是Builders<>.Filter命名空间的一部分.所以这个代码块似乎对我有用,

After digging through a TON of documentation I think I found it. All the examples still show the legacy Query<> method, but it seems that the new method is part of the Builders<>.Filter namespace. So this code block seems to be working for me,

 double lng = double.Parse(longitude);
 double lat = double.Parse(latitude);
 point = new GeoJson2DGeographicCoordinates(lng, lat);
 pnt = new GeoJsonPoint<GeoJson2DGeographicCoordinates>(point);
 dis = distance * 1609.34;
 fil = Builders<Item>.Filter.NearSphere(p => p.Location.Coordinates, pnt, dis);

 filter = filter & fil;

 var sort = Builders<Item>.Sort.Descending("Date");

 // This is the actual query execution
 List<Item> items = collection.Find(filter).Sort(sort).ToListAsync().Result;

此代码块非常混乱,它是一遍又一遍的尝试和失败的结果.我敢肯定,我会想办法清理它.对于我来说,似乎有些冗长,您必须根据GeoJson2DGeographicCoordinates创建一个GeoJsonPoint,但是我敢肯定有充分的理由.如果有人知道,请随时发表评论.任何有关改进此答案的建议都非常受欢迎,这是对文档的无聊探索,因此希望这可以帮助其他人指明正确的方向.

This code block is very messy, it's the result of try and fail over and over. I'm sure I'll figure out ways to clean it up. It seems a little verbose to me that you have to create a GeoJsonPoint from a GeoJson2DGeographicCoordinates, but I'm sure there's a good reason for it. If anyone knows, please feel free to comment. Any suggestions on improving this answer are very welcome, this has been a frustrating dig through documentation, so hopefully this helps point someone else in the right direction.

推荐答案

这是我要这样做的方式:

this is how I do it on my end:

    public IQueryable<TEntity> FindNear<TEntity>(string collectionName, Expression<Func<TEntity, object>> field, double longitude, double latitude, double maxDistanceInKm) where TEntity : IEntity
    {
        var collection = database.GetCollection<TEntity>(collectionName);
        var point = GeoJson.Point(GeoJson.Geographic(longitude, latitude));
        var filter = Builders<TEntity>.Filter.Near(field, point, maxDistanceInKm * 1000);
        return collection.Find(filter).ToList().AsQueryable();
    }

这篇关于如何在2.1 MongoDB C#驱动程序中使用地理空间查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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