I am trying to do GeoSpatial Queries on MongoDb using the C# driver. I have a document as follows:
public class Journey// IJourney
{
public ObjectId Id { get; set; }
public GeoJsonPoint<GeoJson2DGeographicCoordinates> Start { get; set; }
public GeoJsonPoint<GeoJson2DGeographicCoordinates> End { get; set; }
}
And I have written a method which performs the following.
var point = GeoJson.Point(GeoJson.Geographic(-0.1258020000000215, 51.44695)); //long, lat
var point2 = GeoJson.Point(GeoJson.Geographic(-0.1258020000000215, 51.44695)); //long, lat
Collection.CreateIndex(IndexKeys<Journey>.GeoSpatialSpherical(x => x.Start));
Collection.CreateIndex(IndexKeys<Journey>.GeoSpatialSpherical(x => x.End));
var startQuery = Query<Journey>.Near(y => y.Start, point, 1950);
var endQuery = Query<Journey>.Near(y => y.End, point2, 1);
var t = Query.And(startQuery, endQuery);
IQueryable<T> query = Collection.AsQueryable().Where(x => t.Inject());
return query.ToList<T>();
I am trying to find Journeys which have a start x distance from point one and an end x distance from point 2. The above query produces the following.
{ "Start" : { "$near" : { "$geometry" : { "type" : "Point", "coordinates" : [-0.12580200000002151, 51.44695] } }, "$maxDistance" : 1950.0 }, "End" : { "$near" : { "$geometry" : { "type" : "Point", "coordinates" : [-0.12580200000002151, 51.44695] } }, "$maxDistance" : 1.0 } }
My data has only 1 row with the end coordinates which meets the query. When I run this query I get 8 rows which is what meets the start query parameter. If I comment out the startQuery and just run it with the end one I get one row as exected. It appears to be doing an OR when I specify Query.And and I don't know why. How do I make it do an AND?
According to this mongo blog what I am trying to achieve should be possible.
"Additionally, we can have multiple 2dsphere indexes in the same compound index. This allows queries like: “Find routes with a start location within 50 miles from JFK, and an end location within 100 miles of YYC”."