Exemplo n.º 1
0
        public async Task <PDVEntity> GetNearst(double longt, double lat)
        {
            var coord = new GeoJson2DCoordinates(longt, lat);
            var geo   = new GeoJsonPoint <GeoJson2DCoordinates>(coord);

            var filterBuiler  = new FilterDefinitionBuilder <PDVEntity>();
            var filterInAreas = filterBuiler.GeoIntersects(p => p.coverageArea, geo);
            var filterNear    = filterBuiler.Near(p => p.address, geo);

            var filter = filterBuiler.And(filterInAreas, filterNear);

            var collection = Context.GetCollection <PDVEntity>(TypeName);
            var indexKey   = new IndexKeysDefinitionBuilder <PDVEntity>().Geo2DSphere(p => p.address);
            var index      = new CreateIndexModel <PDVEntity>(indexKey);

            collection.Indexes.CreateOne(index);

            var list = await Context.GetCollection <PDVEntity>(TypeName).FindAsync(filter);

            return(list.FirstOrDefault());
        }