Search code examples
mongodbgeospatialspatial-query

Fastest way of querying geospatial data in mongodb


Currently I have my geojson data stored in this format:

  coord: [long, lat],
  time: unix timestamp,
  property: some property

I would like to find the nearest location with the closest timestamp (lte). The way how I am doing it right now is:

    collection.ensureIndex({loc: "2d"})
    collection.find(
            {coord : {
                    $near: [xval, yval],
                    $maxDistance: 200
                    },
             time: {
                    $lte: time
             }
            }).sort({time: -1}).limit(1).toArray(function(err, queryResult) {
  (did some return 404 and 200 here)
 }

When the data size is small, this works. But as my database has been increased to 50G+, this fails (always return 404 saying nothing's found) and I am thinking it is because the way how I query my data leads to slow performance. How should I change my query / data structure to improve and let it work again?


Solution

  • You should first scale your mongodb vertically, by adding more ram and CPU power, and this until you reach your next plateau. Your next step is to scale horizontally (or shard) your DB by having more than one instance of MongoDB handling your dataset. Of course sharding improves the DB performance a lot. You can read this article which describe pretty much the steps to take to scale mongo.