I need to store a taxi route into MongoDB, however I'm not sure what is the best way to do this.

Since the route is only needed for the taxi order, the first idea I had is to store it in the order, this would make the retrieval quite fast, since I won't need to join the order collection with the route collection. As well as that, the route can be stored in a simplified way for example:

   order_id: 1,
   route: [
        [ 1567767312, 58.542454, 110.15454, 45 ], //timestamp, lat, long, speed
        [ 1567768312, 59.448488, 10.484684, 20 ]


If say the measurement is done every 2 seconds, an average 15 min ride would be 450 points, which is not a lot.

However, I read that MongoDB does not like when documents are very different in sizes and since the route can be different all the time, this might cause issues as the order number grows.

The other obvious approach was to have a separate collection to store each reading a separate document:

    timestamp: "2019-09-06T10:49:38+00:00",
    coordinates: [ 58.45464, 128.45465 ],
    order_number: 1

With indexing, this should not be much slower in terms of fetching or writing than the method above. However, this will occupy way more space and the collection might grow really fast.

Just as clarifications. The route is only used for displaying it on the map when somebody opens the order, no need for geo queries.


The right way to store a taxiroute in a MongoDB is to use a GeoJSON object and a LINESTRING

{ type: "LineString", coordinates: [ [ 40, 5 ], [ 41, 6 ] ] }

Using this method you can make use of Spatial Queries like $geointersects and $geonear, and Spatial Indexes. All of that said, you're probably better off migrating to a database that focuses more on this kind of stuff, like PostgreSQL.

  • Hi, thanks for the answer, as mentioned, queries are not important, my most concern was the different sizes of routes, so if the LineString has 100 points in document and 10000 in another, does that mean the documents might be saved on different areas on the disk? – Combustible Pizza Oct 8 '19 at 6:53
  • @CombustiblePizza Of course, there is nothing about Mongo that makes two discrete documents save in the same part of disk. Rather than having two documents with LineStrings. Why not have one document with one MULTILINESTRING? – Evan Carroll Oct 9 '19 at 4:48
  • Ah sorry, I actually was thinking of MultiLineString from the start (forgot that LineString is a single line). That still may leave documents of different sizes. But, perhaps cutting the routes into chunks (separate documents) of fixed sizes (padded for half empty ones) might be the best approach? – Combustible Pizza Oct 9 '19 at 6:09
  • A MULTILINESTRING would require all of the LINESTRINGs to be in the same document. If they're all in the same document, they won't be saved in different places on the disk. They'll all be represented in the same structure – Evan Carroll Oct 9 '19 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.