首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >尝试使用mongodb的filter.near

尝试使用mongodb的filter.near
EN

Stack Overflow用户
提问于 2019-06-03 08:15:33
回答 1查看 0关注 0票数 0

我试图从嵌套的mongodb文档中找到最接近的cordinations。我一直得到的错误如下:

我已经尝试了我能想到的一切。我试图添加索引2d,但两者都不起作用。

var point = GeoJson.Point(GeoJson.Geographic(38.8086, -85.1792));
var locationQuery = new FilterDefinitionBuilder<Book>().NearSphere(tag => tag.CitiesInBook[-1].Location, point,
            5000); 
var query = collection.Find(locationQuery).Limit(10); 
var a =  query.ToList();

Planner返回错误

无法找到$ geoNear查询的索引。

EN

回答 1

Stack Overflow用户

发布于 2019-06-03 17:25:29

这是使用MongoDB.Entities的解决方案。我使用了标签和城市之间的一对多关系,而不是将城市嵌入标签实体中。

using MongoDB.Entities;
using MongoDB.Driver.GeoJsonObjectModel;
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using System.Linq;

namespace StackOverflow
{
    public class Program
    {
        public class Tag : Entity
        {
            public string Name { get; set; }
            public Many<City> Cities { get; set; }

            public Tag() => this.InitOneToMany(() => Cities);
        }

        public class City : Entity
        {
            public string Name { get; set; }
            public GeoJsonPoint<GeoJson2DGeographicCoordinates> Location { get; set; }

        }

        static void Main(string[] args)
        {
            new DB("test");

            //create an index
            DB.Index<City>()
              .Key(c => c.Location, KeyType.Geo2DSphere)
              .Option(o => o.Background = false)
              .Create();

            var paris = new City
            {
                Name = "paris",
                Location = GeoJson.Point(new GeoJson2DGeographicCoordinates(48.8539241, 2.2913515))
            };
            paris.Save();

            var versailles = new City
            {
                Name = "versailles",
                Location = GeoJson.Point(new GeoJson2DGeographicCoordinates(48.796964, 2.137456))
            };
            versailles.Save();

            var poissy = new City
            {
                Name = "poissy",
                Location = GeoJson.Point(new GeoJson2DGeographicCoordinates(48.928860, 2.046889))
            };
            poissy.Save();

            var scifi = new Tag { Name = "sci-fi" };
            scifi.Save();
            scifi.Cities.Add(paris);
            scifi.Cities.Add(versailles);
            scifi.Cities.Add(poissy);

            var horror = new Tag { Name = "horror" };
            horror.Save();
            horror.Cities.Add(poissy);

            var eiffelTower = GeoJson.Point(GeoJson.Geographic(48.857908, 2.295243));

            //find matching city IDs within 20kms of eiffel tower.
            var cities = DB.Find<City, string>()
                           .Match(f => f.NearSphere(c => c.Location, eiffelTower, 20000))
                           .Project(c => c.ID)
                           .Execute();

            //select only the joins for matching city IDs
            var joins = from j in scifi.Cities.JoinQueryable()
                        where cities.Contains(j.ChildID)
                        select j;

            //get the matching tags from previous step's joins
            var tags = from j in joins
                       join t in DB.Queryable<Tag>() on j.ParentID equals t.ID
                       select t ;

            //result is scifi because horror is too far from paris
            var result = tags.Distinct().ToArray();
        }
    }
}

上述程序向mongodb发出2个查询,如下所示

第一个查询:

{
    "find": "City",
    "filter": {
        "Location": {
            "$nearSphere": {
                "$geometry": {
                    "type": "Point",
                    "coordinates": [
                        48.857908,
                        2.295243
                    ]
                },
                "$maxDistance": 20000
            }
        }
    },
    "projection": {
        "_id": NumberInt("1")
    }

第二个查询:

{
    "$match": {
        "ChildID": {
            "$in": [
                ObjectId("5cf3fc1f4090311b6415b75d"),
                ObjectId("5cf3fc1f4090311b6415b75e")
            ]
        }
    }
}, {
    "$lookup": {
        "from": "Tag",
        "localField": "ParentID",
        "foreignField": "_id",
        "as": "t"
    }
}, {
    "$unwind": "$t"
}, {
    "$group": {
        "_id": "$t"
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100006902

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档