DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • MongoDB to Couchbase for Developers, Part 1: Architecture
  • MongoDB Basics in 5 Minutes
  • Role of MongoDB In MERN/MEAN Stack
  • An Introduction to MongoDB and its Database Architecture

Trending

  • REST vs. Message Brokers: Choosing the Right Communication
  • Getting Started With Prometheus Workshop: Instrumenting Applications
  • Revolutionizing Software Testing
  • Agile Metrics and KPIs in Action
  1. DZone
  2. Data Engineering
  3. Databases
  4. How Santa Uses MongoDB Part 1: Using Geospatial Indexes to Deliver Presents Around the World

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

Francesca Krihely user avatar by
Francesca Krihely
·
Jan. 08, 15 · Interview
Like (0)
Save
Tweet
Share
2.76K Views

Join the DZone community and get the full member experience.

Join For Free

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:

 

MongoDB Database

Published at DZone with permission of Francesca Krihely, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • MongoDB to Couchbase for Developers, Part 1: Architecture
  • MongoDB Basics in 5 Minutes
  • Role of MongoDB In MERN/MEAN Stack
  • An Introduction to MongoDB and its Database Architecture

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: