Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB Geospacial search and official C# driver

Can some expert point the best ways to a Geospacial search using official C# driver in MongoDB. Best Object constructor(strings /doubles), Build an index, find near. Many thanks for your help.

db.places.ensureIndex( { loc : "2d" } , { min : -500 , max : 500 } ),  
db.places.find( { loc : { $near : [50,50] , $maxDistance : 5 } } ).limit(20),
like image 480
user325558 Avatar asked Feb 19 '11 18:02

user325558


1 Answers

The C# equivalent to those Mongo shell commands is:

places.EnsureIndex(IndexKeys.GeoSpatial("loc"), IndexOptions.SetGeoSpatialRange(-500, 500));
var query = Query.Near("loc", 50, 50, 5);
var cursor = places.Find(query).SetLimit(20);
foreach (var hit in cursor) {
    // process hit
}
like image 58
Robert Stam Avatar answered Oct 05 '22 22:10

Robert Stam