● MONGODB · GEOSPATIAL · PRODUCTION

Matching Blood Donors by GPS

2dsphere indexes, $near vs $geoWithin, radius expansion, and why we never show you where a donor actually lives.

$geoNear 2dsphere S2 cells Node.js
15 km radius

The Problem

A blood request arrives with GPS coordinates. The system must instantly return nearby, verified, available donors. Fast index lookups won't help if the query scans the wrong data.

Geospatial Indexing

MongoDB's 2dsphere index organizes Earth as a 3D sphere using S2 cell decomposition. The index bounds the search space logarithmically before filtering on blood type, verification status, and availability.

Filtering Pipeline

The query separates concerns: $geoNear finds nearby candidates, $match filters for verified/available, $lookup joins preferences, $sort ranks by distance then rating. Each step works on a smaller set.

Design Tradeoffs

Start with a small radius and expand if needed. Verify upfront (not per-request). Update availability in real-time over Socket.IO. Shard by geography so each request hits one shard.