예제 #1
0
        public void TestGeoSpatial()
        {
            var    keys     = IndexKeys.GeoSpatial("a");
            string expected = "{ \"a\" : \"2d\" }";

            Assert.AreEqual(expected, keys.ToJson());
        }
예제 #2
0
        public void TestNearWithSphericalTrue()
        {
            var query    = Query.Near("loc", 1.5, 2.5, 3.5, true);
            var expected = "{ 'loc' : { '$nearSphere' : [1.5, 2.5], '$maxDistance' : 3.5 } }".Replace("'", "\"");

            Assert.Equal(expected, query.ToJson());

            var collection = LegacyTestConfiguration.Collection;

            collection.Drop();
            collection.CreateIndex(IndexKeys.GeoSpatial("loc"));
            collection.Insert(new BsonDocument {
                { "_id", 1 }, { "loc", new BsonArray {
                                    1, 1
                                } }
            });
            collection.Insert(new BsonDocument {
                { "_id", 2 }, { "loc", new BsonArray {
                                    2, 2
                                } }
            });

            var radiansPerDegree = 2 * Math.PI / 360.0;

            query = Query.Near("loc", 0.0, 0.0, 2.0 * radiansPerDegree, true);
            var results = collection.Find(query).ToList();

            Assert.Equal(1, results.Count);
            Assert.Equal(1, results[0]["_id"].ToInt32());
        }
예제 #3
0
        public void TestGeoSpatialAscending()
        {
            var    keys     = IndexKeys.GeoSpatial("a").Ascending("b");
            string expected = "{ \"a\" : \"2d\", \"b\" : 1 }";

            Assert.AreEqual(expected, keys.ToJson());
        }
예제 #4
0
        public void TestFindWithinCircleSphericalTrue()
        {
            if (collection.Exists())
            {
                collection.Drop();
            }
            collection.Insert(new Place {
                Location = new[] { -74.0, 40.74 }, Name = "10gen", Type = "Office"
            });
            collection.Insert(new Place {
                Location = new[] { -75.0, 40.74 }, Name = "Two", Type = "Coffee"
            });
            collection.Insert(new Place {
                Location = new[] { -74.0, 41.73 }, Name = "Three", Type = "Coffee"
            });
            collection.CreateIndex(IndexKeys.GeoSpatial("Location"));

            var query = Query.WithinCircle("Location", -74.0, 40.74, 0.1, true); // spherical
            var hits  = collection.Find(query).ToArray();

            Assert.AreEqual(3, hits.Length);
            // note: the hits are unordered

            query = Query.WithinCircle("Location", -74.0, 40.74, 0.01, false); // smaller radius
            hits  = collection.Find(query).ToArray();
            Assert.AreEqual(1, hits.Length);

            query = Query.WithinCircle("Location", -174.0, 40.74, 0.1, false); // different part of the world
            hits  = collection.Find(query).ToArray();
            Assert.AreEqual(0, hits.Length);
        }
예제 #5
0
        public void TestNearWithGeoJson()
        {
            var point    = GeoJson.Point(GeoJson.Geographic(40, 18));
            var query    = Query.Near("loc", point);
            var selector = "{ '$near' : { '$geometry' : { 'type' : 'Point', 'coordinates' : [40.0, 18.0] } } }";

            Assert.AreEqual(PositiveTest("loc", selector), query.ToJson());

            var collection = LegacyTestConfiguration.Collection;

            collection.Drop();
            collection.CreateIndex(IndexKeys.GeoSpatial("loc"));
            collection.Insert(new BsonDocument {
                { "_id", 1 }, { "loc", new BsonArray {
                                    1, 1
                                } }
            });
            collection.Insert(new BsonDocument {
                { "_id", 2 }, { "loc", new BsonArray {
                                    2, 2
                                } }
            });

            query = Query.Near("loc", 0.0, 0.0);
            var results = collection.Find(query).ToList();

            Assert.AreEqual(2, results.Count);
            Assert.AreEqual(1, results[0]["_id"].ToInt32());
            Assert.AreEqual(2, results[1]["_id"].ToInt32());
        }
예제 #6
0
        public static T[] GetNear <T>(this MongoCollection <T> collection, string key, double x, double y, int limit)
        {
            collection.EnsureIndex(IndexKeys.GeoSpatial(key));
            var query = Query.Near(key, x, y);

            return(collection.Find(query).Take(limit).ToArray());
        }
예제 #7
0
        public void TestNear()
        {
            var query    = Query.Near("loc", 1.1, 2.2);
            var selector = "{ '$near' : [1.1, 2.2] }";

            Assert.AreEqual(PositiveTest("loc", selector), query.ToJson());

            var collection = LegacyTestConfiguration.Collection;

            collection.Drop();
            collection.CreateIndex(IndexKeys.GeoSpatial("loc"));
            collection.Insert(new BsonDocument {
                { "_id", 1 }, { "loc", new BsonArray {
                                    1, 1
                                } }
            });
            collection.Insert(new BsonDocument {
                { "_id", 2 }, { "loc", new BsonArray {
                                    2, 2
                                } }
            });

            query = Query.Near("loc", 0.0, 0.0);
            var results = collection.Find(query).ToList();

            Assert.AreEqual(2, results.Count);
            Assert.AreEqual(1, results[0]["_id"].ToInt32());
            Assert.AreEqual(2, results[1]["_id"].ToInt32());
        }
예제 #8
0
        public void TestNearWithMaxDistance()
        {
            var query    = Query.Near("loc", 1.1, 2.2, 3.3);
            var expected = "{ 'loc' : { '$near' : [1.1, 2.2], '$maxDistance' : 3.3 } }".Replace("'", "\"");

            Assert.AreEqual(expected, query.ToJson());

            var collection = LegacyTestConfiguration.Collection;

            collection.Drop();
            collection.CreateIndex(IndexKeys.GeoSpatial("loc"));
            collection.Insert(new BsonDocument {
                { "_id", 1 }, { "loc", new BsonArray {
                                    1, 1
                                } }
            });
            collection.Insert(new BsonDocument {
                { "_id", 2 }, { "loc", new BsonArray {
                                    2, 2
                                } }
            });

            query = Query.Near("loc", 0.0, 0.0, 2.0);
            var results = collection.Find(query).ToList();

            Assert.AreEqual(1, results.Count);
            Assert.AreEqual(1, results[0]["_id"].ToInt32());
        }
예제 #9
0
        static MongoCollections()
        {
            var db    = MongoManager.getDB();
            var store = db.GetCollection <StoreMgDTO>(MongoKeyConst.CollectionName);
            var geoindexMapLocation = IndexKeys <StoreMgDTO> .GeoSpatial(c => c.Location);

            store.EnsureIndex(geoindexMapLocation, IndexOptions.SetGeoSpatialRange(0, 300));
        }
        public void TestGeoSpatialAscending_Typed()
        {
            var keys = IndexKeys <Test> .GeoSpatial(x => x.A).Ascending(x => x.B);

            string expected = "{ \"a\" : \"2d\", \"b\" : 1 }";

            Assert.AreEqual(expected, keys.ToJson());
        }
        public void TestGeoSpatial_Typed()
        {
            var keys = IndexKeys <Test> .GeoSpatial(x => x.A);

            string expected = "{ \"a\" : \"2d\" }";

            Assert.AreEqual(expected, keys.ToJson());
        }
예제 #12
0
        public void TestFindNearSphericalTrue()
        {
            if (collection.Exists())
            {
                collection.Drop();
            }
            collection.Insert(new Place {
                Location = new[] { -74.0, 40.74 }, Name = "10gen", Type = "Office"
            });
            collection.Insert(new Place {
                Location = new[] { -75.0, 40.74 }, Name = "Two", Type = "Coffee"
            });
            collection.Insert(new Place {
                Location = new[] { -74.0, 41.73 }, Name = "Three", Type = "Coffee"
            });
            collection.CreateIndex(IndexKeys.GeoSpatial("Location"));

            var query = Query.Near("Location", -74.0, 40.74, double.MaxValue, true); // spherical
            var hits  = collection.Find(query).ToArray();

            Assert.AreEqual(3, hits.Length);

            var hit0 = hits[0];

            Assert.AreEqual(-74.0, hit0["Location"].AsBsonArray[0].AsDouble);
            Assert.AreEqual(40.74, hit0["Location"].AsBsonArray[1].AsDouble);
            Assert.AreEqual("10gen", hit0["Name"].AsString);
            Assert.AreEqual("Office", hit0["Type"].AsString);

            // with spherical true "Two" is considerably closer than "Three"
            var hit1 = hits[1];

            Assert.AreEqual(-75.0, hit1["Location"].AsBsonArray[0].AsDouble);
            Assert.AreEqual(40.74, hit1["Location"].AsBsonArray[1].AsDouble);
            Assert.AreEqual("Two", hit1["Name"].AsString);
            Assert.AreEqual("Coffee", hit1["Type"].AsString);

            var hit2 = hits[2];

            Assert.AreEqual(-74.0, hit2["Location"].AsBsonArray[0].AsDouble);
            Assert.AreEqual(41.73, hit2["Location"].AsBsonArray[1].AsDouble);
            Assert.AreEqual("Three", hit2["Name"].AsString);
            Assert.AreEqual("Coffee", hit2["Type"].AsString);

            query = Query.Near("Location", -74.0, 40.74, 0.5); // with maxDistance
            hits  = collection.Find(query).ToArray();
            Assert.AreEqual(1, hits.Length);

            hit0 = hits[0];
            Assert.AreEqual(-74.0, hit0["Location"].AsBsonArray[0].AsDouble);
            Assert.AreEqual(40.74, hit0["Location"].AsBsonArray[1].AsDouble);
            Assert.AreEqual("10gen", hit0["Name"].AsString);
            Assert.AreEqual("Office", hit0["Type"].AsString);

            query = Query.Near("Location", -174.0, 40.74, 0.5); // with no hits
            hits  = collection.Find(query).ToArray();
            Assert.AreEqual(0, hits.Length);
        }
예제 #13
0
        public void TestGeoNearSphericalTrue()
        {
            if (collection.Exists())
            {
                collection.Drop();
            }
            collection.Insert(new Place {
                Location = new[] { -74.0, 40.74 }, Name = "10gen", Type = "Office"
            });
            collection.Insert(new Place {
                Location = new[] { -75.0, 40.74 }, Name = "Two", Type = "Coffee"
            });
            collection.Insert(new Place {
                Location = new[] { -74.0, 41.73 }, Name = "Three", Type = "Coffee"
            });
            collection.CreateIndex(IndexKeys.GeoSpatial("Location"));

            var options = GeoNearOptions.SetSpherical(true);
            var result  = collection.GeoNearAs <Place>(Query.Null, -74.0, 40.74, 100, options);

            Assert.IsTrue(result.Ok);
            Assert.AreEqual("onlinetests.testcollection", result.Namespace);
            Assert.IsTrue(result.Stats.AverageDistance >= 0.0);
            Assert.IsTrue(result.Stats.BTreeLocations >= 0);
            Assert.IsTrue(result.Stats.Duration >= TimeSpan.Zero);
            Assert.IsTrue(result.Stats.MaxDistance >= 0.0);
            Assert.IsTrue(result.Stats.NumberScanned >= 0);
            Assert.IsTrue(result.Stats.ObjectsLoaded >= 0);
            Assert.AreEqual(3, result.Hits.Count);

            var hit0 = result.Hits[0];

            Assert.IsTrue(hit0.Distance == 0.0);
            Assert.AreEqual(-74.0, hit0.RawDocument["Location"].AsBsonArray[0].AsDouble);
            Assert.AreEqual(40.74, hit0.RawDocument["Location"].AsBsonArray[1].AsDouble);
            Assert.AreEqual("10gen", hit0.RawDocument["Name"].AsString);
            Assert.AreEqual("Office", hit0.RawDocument["Type"].AsString);

            // with spherical true "Two" is considerably closer than "Three"
            var hit1 = result.Hits[1];

            Assert.IsTrue(hit1.Distance > 0.0);
            Assert.AreEqual(-75.0, hit1.RawDocument["Location"].AsBsonArray[0].AsDouble);
            Assert.AreEqual(40.74, hit1.RawDocument["Location"].AsBsonArray[1].AsDouble);
            Assert.AreEqual("Two", hit1.RawDocument["Name"].AsString);
            Assert.AreEqual("Coffee", hit1.RawDocument["Type"].AsString);

            var hit2 = result.Hits[2];

            Assert.IsTrue(hit2.Distance > 0.0);
            Assert.IsTrue(hit2.Distance > hit1.Distance);
            Assert.AreEqual(-74.0, hit2.RawDocument["Location"].AsBsonArray[0].AsDouble);
            Assert.AreEqual(41.73, hit2.RawDocument["Location"].AsBsonArray[1].AsDouble);
            Assert.AreEqual("Three", hit2.RawDocument["Name"].AsString);
            Assert.AreEqual("Coffee", hit2.RawDocument["Type"].AsString);
        }
예제 #14
0
 public MongoIndexKeysWarpper GeoSpatial(string name)
 {
     if (MongoIndexKeys == null)
     {
         MongoIndexKeys = IndexKeys.GeoSpatial(name);
     }
     else
     {
         MongoIndexKeys = MongoIndexKeys.GeoSpatial(name);
     }
     return(this);
 }
예제 #15
0
        public void Initialize()
        {
            // consumer indexes
            this.ConsumerPortfolios.EnsureIndex(IndexKeys <ConsumerPortfolio> .GeoSpatial(item => item.Preference.Proximity.Location));
            this.ConsumerPortfolios.EnsureIndex(IndexKeys <ConsumerPortfolio> .Ascending(item => item.Request.State));

            // provider indexes
            this.ProviderPortfolios.EnsureIndex(IndexKeys <ProviderPortfolio> .GeoSpatial(item => item.Principal.Address.Location));

            // response indexes
            this.Responses.EnsureIndex(IndexKeys <Response> .Ascending(item => item.ConsumerPortfolioId));
            this.Responses.EnsureIndex(IndexKeys <Response> .Ascending(item => item.ProviderPortfolioId));
        }
예제 #16
0
        public void TestGeoNearGeneric()
        {
            if (collection.Exists())
            {
                collection.Drop();
            }
            collection.Insert(new Place {
                Location = new[] { 1.0, 1.0 }, Name = "One", Type = "Museum"
            });
            collection.Insert(new Place {
                Location = new[] { 1.0, 2.0 }, Name = "Two", Type = "Coffee"
            });
            collection.Insert(new Place {
                Location = new[] { 1.0, 3.0 }, Name = "Three", Type = "Library"
            });
            collection.Insert(new Place {
                Location = new[] { 1.0, 4.0 }, Name = "Four", Type = "Museum"
            });
            collection.Insert(new Place {
                Location = new[] { 1.0, 5.0 }, Name = "Five", Type = "Coffee"
            });
            collection.CreateIndex(IndexKeys.GeoSpatial("Location"));

            var options = GeoNearOptions
                          .SetDistanceMultiplier(1)
                          .SetMaxDistance(100);
            var result = collection.GeoNearAs <Place>(Query.Null, 0.0, 0.0, 100, options);

            Assert.IsTrue(result.Ok);
            Assert.AreEqual("onlinetests.testcollection", result.Namespace);
            Assert.IsTrue(result.Stats.AverageDistance >= 0.0);
            Assert.IsTrue(result.Stats.BTreeLocations >= 0);
            Assert.IsTrue(result.Stats.Duration >= TimeSpan.Zero);
            Assert.IsTrue(result.Stats.MaxDistance >= 0.0);
            Assert.IsTrue(result.Stats.NumberScanned >= 0);
            Assert.IsTrue(result.Stats.ObjectsLoaded >= 0);
            Assert.AreEqual(5, result.Hits.Count);
            Assert.IsTrue(result.Hits[0].Distance > 1.0);
            Assert.AreEqual(1.0, result.Hits[0].RawDocument["Location"].AsBsonArray[0].AsDouble);
            Assert.AreEqual(1.0, result.Hits[0].RawDocument["Location"].AsBsonArray[1].AsDouble);
            Assert.AreEqual("One", result.Hits[0].RawDocument["Name"].AsString);
            Assert.AreEqual("Museum", result.Hits[0].RawDocument["Type"].AsString);

            var place = result.Hits[1].Document;

            Assert.AreEqual(1.0, place.Location[0]);
            Assert.AreEqual(2.0, place.Location[1]);
            Assert.AreEqual("Two", place.Name);
            Assert.AreEqual("Coffee", place.Type);
        }
        public int SaveGeoPointDto(GeoPointDto geopoint)
        {
            var database = CreateSpatialDatabase();

            using (Mongo.RequestStart(database))
            {
                var geolocations = database.GetCollection <Geolocation>(Geolocations);
                var geolocation  = geopoint.ToGeolocation();
                geolocations.Save(geolocation);

                geolocations.EnsureIndex(IndexKeys.GeoSpatial("loc"));
            }
            return(1);
        }
예제 #18
0
        private void RecreateCollection(IEnumerable <Station> stations)
        {
            var server = MongoServer.Create(Settings.TidesMongoConnection);

            server.Connect();

            var db = server["tides"];

            if (db.CollectionExists("stations"))
            {
                db.DropCollection("stations");
            }


            var stationCollection = db["stations"];

            stationCollection.InsertBatch(stations);
            stationCollection.EnsureIndex(IndexKeys.GeoSpatial("Location"));

            server.Disconnect();
        }
예제 #19
0
        public void TestFindWithinRectangle()
        {
            if (collection.Exists())
            {
                collection.Drop();
            }
            collection.Insert(new Place {
                Location = new[] { -74.0, 40.74 }, Name = "10gen", Type = "Office"
            });
            collection.Insert(new Place {
                Location = new[] { -75.0, 40.74 }, Name = "Two", Type = "Coffee"
            });
            collection.Insert(new Place {
                Location = new[] { -74.0, 41.73 }, Name = "Three", Type = "Coffee"
            });
            collection.CreateIndex(IndexKeys.GeoSpatial("Location"));

            var query = Query.WithinRectangle("Location", -75.0, 40, -73.0, 42.0);
            var hits  = collection.Find(query).ToArray();

            Assert.AreEqual(3, hits.Length);
            // note: the hits are unordered
        }
        public IEnumerable <PlaygroundEntity> GetByLocation(float lat, float lng)
        {
            var earthRadius = 6378.0; // km
            var rangeInKm   = 10.0;   // km

            GetCollection().EnsureIndex(IndexKeys.GeoSpatial("Loc"));

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

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

            GeoNearResult <PlaygroundEntity> results = GetCollection().GeoNear(
                Query.Null,
                lng, // note the order
                lat, // [lng, lat]
                200,
                options
                );

            return(results.Hits.Select(result => result.Document));
        }
예제 #21
0
        public static void EnsureGeoSpatialIndex <TProperty>(Expression <Func <T, TProperty> > expression)
        {
            var indexKeys = IndexKeys.GeoSpatial(ExpressionUtility.GetPropertyName(expression));

            GetCollection().EnsureIndex(indexKeys);
        }
예제 #22
0
 private void EnsureIndex()
 {
     this.Nodes.EnsureIndex(IndexKeys.GeoSpatialSpherical("LatLong"));
     this.Ways.EnsureIndex(IndexKeys.GeoSpatial("Nodes"));
 }