How Santa Uses MongoDB Part 1: Using Geospatial Indexes to Deliver Presents Around the World

< View all blog posts
Norberto Leite
December 23, 2014
Category: Company

We love watching everyone open their presents on Christmas morning to see what jolly old Santa brought us!

Have you ever wondered how Santa delivers 6 billion presents in one night? How does he collect all those “Dear Santa” letters, read them one by one, make sure that there are no mistakes, no missed presents, and no inconsistent state, so some poor person doesn’t get two daVinci 1.0 3D printers, while their neighbor down the hall gets none? How does Santa know how many bicycles to get? How many dolls? How many fire trucks?

Delivering on Christmas is an incredible task! We have recently discovered Santa uses MongoDB!!! Let’s take a closer look.

Santa Loves Geospatial Indexes!

With over 6 billion gifts to deliver across 24 time zones in one night, it’s an incredible achievement, right? So how does Santa know how to reach everyone in time? And how does he ensure everyone gets their fair share of presents from their wishlist?

Santa keeps a database of all the “dear Santa” letters that reach his inbox (yes Santa knows where you live!) and he has lots of bags grouping the presents per city. In the past, Santa used one magical bag to hold all the presents. Then he received an inspection by the Finnish Work Environment Safety Agency and they recommended spreading the load across several bags, both for health (Santa is not getting any younger) and efficiency reasons (what happens if you lose that single bag?!?).

Besides, Santa has been around the block a few times so he knows that monolithic approaches are a 90’s thing.

Santa maintains a record for each one of us, where he keeps track of where he needs to deliver the presents. He uses MongoDB (obviously!) with geospatial information to understand where to deliver your freshly wrapped gifts. Each record looks like this:

{
    "_id": 123300410230,
    "name": "Norberto Leite",
    "location":{ 
        "geo": {
            "type": "Point",
            "coordinates": [ -8.611720204353333, 41.14341253682037]
                },
        "address" : {
            "country": "PT",
            "city": "Porto",
            "street": "Rua Escura 1",
            "zip": [4000, "Porto"],
        },
    },
}

This type of data structure allows Santa to do lots of different things with his gift distribution algorithm.

For example, find all presents to be delivered in Portugal:

> db.presents.find( { "location.address.country": "PT"  })

But what Santa really wants to know is, which presents need to be delivered once he gets to a certain city? To solve this traveling Santa problem uses MongoDB geospatial capabilities.

Santa asked his Master Elf DBA to create a 2dsphere index so we can easily and efficiently make his deliveries:


db.presents.ensureIndex( {"location.geo": "2dsphere"}  );

Obviously, as any good and Certified MongoDB DBA, to avoid operational overhead in the database, Master Elf DBA decided to run this index creation on secondaries without disrupting Santa’s work. No gift delivery status update will need to wait for the creation of this index to be efficiently accepted!

Santa can then deliver presents to each house based on their distance from the center of the city. To get a rough approximation based on a city’s centroid, Santa can use the {noformat}$near{noformat} operator to get that list of presents to be delivered:


db.presents.find(  {"location.geo": { "$near": { "$geometry": {  "type": "Point", "coordinates": [-8.611720204353333, 41.14341253682037]  } }  } } )

But Santa is a bit of a perfectionist, and to get a more accurate guide to his delivery requirements, Santa uses the Polygon map of the city Porto, in this case {}:


db.presents.find( {"location.geo" :{ "$geoWithin": {  "$geometry":{ "type": "Polygon", "coordinates": [
          [
            [
              -8.688468933105469,
              41.17400251011821
            ],
            [
              -8.642463684082031,
              41.18459702669797
            ],
            [
              -8.601951599121094,
              41.18459702669797
            ],
            [
              -8.582038879394531,
              41.169609159184255
            ],
            [
              -8.578948974609375,
              41.148413563966386
            ],
            [
              -8.594741821289062,
              41.14091592012965
            ],
            [
              -8.604354858398438,
              41.14453557935463
            ],
            [
              -8.614654541015625,
              41.1406573653974
            ],
            [
              -8.635597229003906,
              41.14815503879421
            ],
            [
              -8.667869567871094,
              41.148413563966386
            ],
            [
              -8.677825927734373,
              41.15022321163024
            ],
            [
              -8.688468933105469,
              41.17400251011821
            ]
          ]
        ] }  } }}  )

In the next post we will look at some of the techniques Santa has used to improve the efficiency of his delivery algorithm by moving from a single magical bag, to multiple bags. After all, even Holiday Spirit has a carbon offset, and it isn’t cheap.

If you're interested in learning more about how Santa put together his Christmas plan, download the MongoDB Architecture guide here:

<a class="btn btn-primary href="/lp/whitepaper/architecture-guide" target="_BLANK">ARCHITECTURE GUIDE

About Norberto Leite

Norberto Leite is Technical Evangelist at MongoDB. Norberto has been working for the last 5 years on large scalable and distributable application environments, both as advisor and engineer. Prior to MongoDB Norberto served as BigData Engineer at Telefonica.

comments powered by Disqus