MongoDb AND GeoSpatial Query

112 views
Skip to first unread message

Mark Towse

unread,
Nov 24, 2014, 9:12:37 AM11/24/14
to mongod...@googlegroups.com
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”."

Asya Kamsky

unread,
Nov 28, 2014, 1:24:45 PM11/28/14
to mongodb-user
If you want to find 
Journeys which have a start x distance from point one and an end x distance from point 2

then you should use $geoWithin and *not* $near.   Near will sort results by distance, nearest to furtherest and you have *two* $near operator creating a bit of an undefined scenario.   Using $within is something that can be done for more than one point, logically speaking.   In addition, this will be faster than $near as it doesn't have to produce results in a specific order.

Asya


--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/bc1894b9-cb9a-4ce3-bd4c-c39e0c828a4a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages