MongoDB: Cluster documents by geographic location given area and max points? [closed] MongoDB: Cluster documents by geographic location given area and max points? [closed] mongodb mongodb

MongoDB: Cluster documents by geographic location given area and max points? [closed]


Even if you do this querying at runtime, it is going to be inefficient and not fast to be considered a good user interface.I would suggest you pregenerate clusters of specific sizes and keep them stored in your current collection along with your original documents.Here is how:

  • Each document will store an additional field ( lets call it geolevel ), which will denote how small or big entity it is. Your base documents will have geolevel=1 :

    {    "PlaceName" : "Boston",    "Location" : {        "type" : "Point",        "coordinates" : [ 42.358056, -71.063611 ]    },    "Subpopulations": {        "Age": {                 "0_4" : 37122,                "6_11" : 33167,                "12_17" : 35464,                "18_24" : 130885,                "25_34" : 127058,                "34_44" : 79092,                "45_54" : 72076,                "55_64" : 59766,                "65_74" : 33997,                "75_84" : 20219,                "85_" : 9057        }    },    "geolevel":1  // added geolevel}
    • You can run processing on your database to pre-generate similar documents for clusters, and for multiple levels.e.g. geolevel:2 will be a cluster of few cities within 250kms radius,geolevel:3 will be cluster of geolevel:2 clusters.

    • You can also store a field like memberids to store ids of children ineach cluster. This might be necessary to avoid an entity going into two adjacent clusters, it can be assigned to any one of the adjacent clusters and your visualization would still work fine.A geolevel:2 cluster doc would look like:

       {    "PlaceName" : "cluster_sdfs34535",  // The id can be generated from hash like sha of a list of all children ids.    "Location" : {  // center of the cluster        "type" : "Point",        "coordinates" : [ 42.358056, -71.063611 ]    },    "Subpopulations": { // total population of the cluster        "Age": {                 "0_4" : 371220,                "6_11" : 331670,                "12_17" : 354640,                "18_24" : 1308850,                "25_34" : 1270580,                "34_44" : 790920,                "45_54" : 720760,                "55_64" : 597660,                "65_74" : 339970,                "75_84" : 202190,                "85_" : 90570        }    },    "geolevel":2 ,    "childs":[4,5,6,7] // ids of child documents}
    • Now your visualization app needs to do a mapping of zoomlevel to geolevel, and based on that you will select your documents. For city level visualization, you can query for geolevel:1 documents, and as you zoom out to state,country etc you can increase the geolevel to 2,3...