问题
I have a collection of documents with a geospatial index location: {lat:XX.XXXX, lng:XX.XXXX}
. But I would like them to be outputted by TimeZone.
I was thinking of converting geolocation to Timezone, and have an index based on that, but I'm not sure how to do that either, or if it'll be easier to use the geospatial index anyway.
回答1:
The excellent answer Sim provided will let you do it all in your own code - but it does require a bit of overhead. If you just want to resolve the locations to time zones, and don'y mind calling an external service, you can use Google's Time Zone API.
See also this related question and its answers.
回答2:
MongoDB can perform checks for inclusion of a point in an arbitrary polygon described in GeoJSON
using $geoWithin
query criteria. You can create a new collection of timezone documents with their location polygons in GeoJSON
. For that, try this GitHub repo. My answer to this related SO question could also be useful.
Once you have the timezone collection you can pre-calculate the TZ, store it in your documents and index it for faster queries. You'll need to come up with an ordering for the timezones. Offsets from UTC could be a good starting point.
来源:https://stackoverflow.com/questions/15867990/ruby-sort-mongodb-records-by-timezone