Ruby - Sort MongoDB records by Timezone?

烂漫一生 提交于 2020-04-14 07:19:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!