0

I've successfully got

 var locations = MongoCollection.Find(Query.Within<GeoJson2DGeographicCoordinates>("Position", poly))

to return the correct results when my document has this structure:

class Location
{
    [BsonId]
    public int Id { get; set; }
    public string Address { get; set; }
    public GeoJsonPoint<GeoJson2DGeographicCoordinates> Position { get; set; }
}

and this index:

MongoCollection.CreateIndex(IndexKeys.GeoSpatialSpherical("Position"));

However, the minute I change my document to contain a sub document with an array of Foos like this:

 class Location
    {
        [BsonId]
        public int Id { get; set; }
        public List<Foo> Locations {get;set;}
    }

class Foo
       {
            public int Id { get; set; }
            public string Address { get; set; }
            public GeoJsonPoint<GeoJson2DGeographicCoordinates> Position { get; set; }
        }  

and I change the index and query to use the sub document fields like so:

MongoCollection.CreateIndex(IndexKeys.GeoSpatialSpherical("Locations.Position"));    
var locations = MongoCollection.Find(Query.Within<GeoJson2DGeographicCoordinates>("Locations.Position", poly)).AsQueryable().ToList();

It stops working as expected. In the above scenario it returns back a list of Location with 1 item, that item is all the Foos.

Updated To elaborate, if i have 400 Foo objects, then after i run the query on the sub-document it returns me a List where Locations is a List with 400 Foos. So it's not successfully checking if the Foos are in the poly.

From my immediate window:

locations
Count = 1
    [0]: {MyGeo.GeoTest.Location}
locations[0].Locations
Count = 408

What I expect is for List Locations to be filtered based on the poly.

Can anyone tell me where i'm going wrong? I've searched online and there doesn't seem to be a lot of examples with this structure but the documentation says you can have the index the way i have it.

You may also model the location data as a field inside of a sub-document. In this case, the document would contain a field (e.g. addresses) that holds an array of documents where each document has a field (e.g. loc:) that holds location coordinates. For example:

{ _id : ObjectId(...),
  name : "...",
  addresses : [ {
                 context : "home" ,
                 loc : [ 55.5, 42.3 ]
                } ,
                {
                 context : "home",
                 loc : [ -74 , 44.74 ]
                }
              ]
}

You could then create the geospatial index on the addresses.loc field as in the following example:

db.records.ensureIndex( { "addresses.loc": "2d" } )

My query must be wrong because i must not be understanding how the sub documents work correctly.

Any ideas?

Thanks.

Stringer Bell
  • 265
  • 1
  • 4
  • 13
  • For clarity, could you post the results you get vs the results you expect? Are you expecting the geo query to return just the subdocument that satisfies the within predicate? That's not how MongoDB queries work - queries match the documents in the collection and the those documents are what are returned (though their shape can be modified via projection). – wdberkeley Feb 13 '15 at 18:15
  • updated to include more on expected vs actual result. – Stringer Bell Feb 14 '15 at 12:27

0 Answers0