[SERVER-4221] Working with a large set of geospatial data but is there no way around "can't have 2 special fields"? Created: 06/Nov/11  Updated: 29/Mar/16  Resolved: 16/Nov/11

Status: Closed
Project: Core Server
Component/s: Geo
Affects Version/s: None
Fix Version/s: None

Type: Question Priority: Critical - P2
Reporter: Max Countryman Assignee: Greg Studer
Resolution: Duplicate Votes: 0
Labels: query
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Linux


Issue Links:
Duplicate
duplicates SERVER-3653 Issues with multiple spatial indexes Closed
Participants:

 Description   

My team is working on a product that involves a very large set of geospatial data and initially thought Mongo might be the right choice for us. However it seems there's an inherit limitation with GeoPointFields. Is there no way to work around the 2 special fields limitation? It's critical that we are able to filter by at least two geopoint fields in a single query. If this isn't possible at all in Mongo this may not be the right tool for us.



 Comments   
Comment by shibimukesh [ 29/Mar/16 ]

i need to calculate the distance from start to destination how to do a query in mongo... "distance" : 0.0079,
"free_record" : "",
"loc" :

{ "type" : "LineString", "coordinates" : [ [ 11.0214407, 76.9165522 ], [ 11.0214407, 76.9165522 ], [ 11.0214409, 76.9165544 ], [ 11.0214429, 76.9165543 ], [ 11.0214428, 76.9165556 ], [ 11.0214429, 76.9165562 ], [ 11.0214432, 76.916555 ], [ 11.0214427, 76.9165549 ], [ 11.0214443, 76.9165546 ], [ 11.0214439, 76.9165545 ], [ 11.0214435, 76.9165544 ] ] }

pls give a query to calculate the distance

Comment by Greg Studer [ 07/Nov/11 ]

Right, multiple geospatial fields aren't currently supported - you can work around this currently with two collections and using an $in query :

var origin_route_ids = db.origins.find({ pos : { $within :

{ <origin> }

} },

{ route_id : 1 }

).toArray()
var dest_route_ids = db.dests.find({ pos : { $within :

{ <destination> }

}, route_id :

{ $in : origin_route_ids }

},

{ route_id : 1 }

).toArray()
return db.routes.find(

{ route_id : dest_route_ids }

)

The downside is of course that you'll have to pull the _ids back to the app in the meantime - these will probably be pretty small, but there could be many. Again, this should be supported as a single query using aggregation operators in the future, but the algorithm will effectively be the same. It's something we're working on.

There's probably a way to do this as a single query using the $where operator and checking distance via javascript, but the overhead is probably not be worth it for large data sets.

Comment by Max Countryman [ 07/Nov/11 ]

For instance, say we have a Route document, containing both an origin and a destination as GeoPointFields. We want to be able to filter within a given distance on both the origin and the destination in a single query. It seems there's a limit on filtering by special fields, "can't have 2 special fields", which is getting in the way of doing this as a single query.

Comment by Greg Studer [ 07/Nov/11 ]

Not sure I know exactly what you mean, but if you're referring to the ability to do something like :

db.coll.find({ posA : { $within :

{ $center [...] }

}, posB : { $within :

{ $polygon : [...] }

} }), there isn't currently direct support for intersections/unions like this - you'd have to join the shapes yourself and search there. The new aggregation framework we're releasing should allow this, but the query would still work by first searching one region, then filtering all those results by the second region.

If you have documents that incorporate multiple locations, you can embed them in nested subdocuments, like :

{ positions : [

{ pos : [ ... ] }

,

{ pos : [ ... ] }

,

{ pos : [ ... ] }

] }
db.ensureIndex(

{ "positions.pos" : "2d" }

)

then you can query for all documents with any position inside a region, or find the nearest point (or points) in the document to another location (x,y).

Can you give more info about what kind of queries you're looking to run?

Generated at Thu Feb 08 03:05:20 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.