示例#1
0
        /// <summary>
        /// Get total population by year in states the center of which is within a circle of 500 km radius around Memphis
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        public List <BsonDocument> GetPopulationByState500KmsAroundMemphis(IMongoCollection <BsonDocument> collection, string outCollection = "")
        {
            BsonDocument geoPoint = new BsonDocument
            {
                { "type", "Point" },
                { "coordinates", new BsonArray(new Double[] { 90, 35 }) }
            };
            var geoNearOptions = new BsonDocument {
                { "near", geoPoint },
                { "distanceField", "dist.calculated" },
                { "maxDistance", 500000 },
                { "includeLocs", "dist.location" },
                { "spherical", true },
            };
            //var geonear = new BsonDocument { { "$geoNear", geoNearOptions } };
            var stage = new BsonDocumentPipelineStageDefinition <BsonDocument, BsonDocument>(new BsonDocument {
                { "$geoNear", geoNearOptions }
            });
            var aggregate = collection.Aggregate()
                            .AppendStage(stage)
                            .Unwind("data")
                            .Group(new BsonDocument
            {
                { "_id", "$data.year" },
                { "totalPop", new BsonDocument("$sum", "$data.totalPop") },
                { "states", new BsonDocument("$addToSet", "$name") }
            })
                            .Sort(new BsonDocument("_id", 1));

            if (!string.IsNullOrWhiteSpace(outCollection))
            {
                aggregate.Out(outCollection);
            }
            return(aggregate.ToList());
        }
示例#2
0
        private static async Task <List <ZipCodeCollection> > GetNearByZipCodeWithDistance(double lat, double lng)
        {
            BsonDocument geoPoint = new BsonDocument
            {
                { "type", "Point" },
                {
                    "coordinates", new BsonArray(new double[]

                                                 { lng, lat }
                                                 )
                }
            };
            BsonDocument geoNearOptions = new BsonDocument
            {
                { "spherical", true },
                { "near", geoPoint },
                { "distanceField", "Distance" }
            };
            var stage = new BsonDocumentPipelineStageDefinition <ZipCodeCollection, ZipCodeCollection>(new BsonDocument
            {
                { "$geoNear", geoNearOptions }
            });
            var result = await Collection.Aggregate().AppendStage(stage).Limit(50).ToListAsync();

            var nearByZipcodes = result;

            return(nearByZipcodes);
        }
示例#3
0
        public async Task <IEnumerable <GasStationLocationValueObject> > GetProximityAsync(double latitude, double longitude, string name)
        {
            var filter = new BsonDocument {
                { "name", new BsonRegularExpression($"{name}") }
            };
            var pipeline = new BsonDocumentPipelineStageDefinition <GasStation, GasStationLocationValueObject>(new BsonDocument
            {
                { "$geoNear", new BsonDocument
                  {
                      { "near", new BsonDocument
                        {
                            { "type", "Point" },
                            { "coordinates", new BsonArray(new[] { latitude, longitude }) }
                        } },
                      { "distanceField", "dist.calculated" },
                      { "maxDistance", 10000 },
                      { "query", filter },
                      { "num", 20 },
                      { "includeLocs", "dist.location" },
                      { "spherical", true }
                  } }
            });

            var colAggregate = _context.GasStations.Aggregate().AppendStage(pipeline);

            return(await colAggregate.ToListAsync());
        }
示例#4
0
        public async Task <IReadOnlyList <UserWithDistanceDto> > FindWithGeoSpatial(GetUsersQuery query)
        {
            //var gp = new GeoJsonPoint<GeoJson2DGeographicCoordinates>(new GeoJson2DGeographicCoordinates(query.Coordinates[0], query.Coordinates[1]));
            //var filter = Builders<User>.Filter.Near(u => u.Location, gp, query.Distance);
            //var raw = await _mongoRepository.Collection.Find(filter).ToListAsync();

            if (query.Coordinates == null)
            {
                throw new ArgumentException("This method needs a coordinates", nameof(query.Coordinates));
            }

            var queryCriteria = new BsonDocument();

            if (query.Gender.HasValue)
            {
                queryCriteria.AddRange(new BsonDocument {
                    { "gender", query.Gender }
                });
            }
            var geoNearOptions = new BsonDocument
            {
                {
                    "near", new BsonDocument
                    {
                        { "type", "Point" },
                        { "coordinates", new BsonArray(query.Coordinates) }
                    }
                },
                { "maxDistance", query.Distance },
                { "query", queryCriteria },
                { "distanceField", "distance" }
            };
            var geoNearAggregate = new BsonDocumentPipelineStageDefinition <User, UserWithDistanceDto>(new BsonDocument
            {
                { "$geoNear", geoNearOptions }
            });

            return(await _mongoRepository.Collection.Aggregate()
                   .AppendStage(geoNearAggregate)
                   .ToListAsync());
        }