First brief disclaimer, I might not know all correct terms, and that kind of hinders my googling fu. If you know good examples of this problem, please direct me to those, and I’m happy to dig in to those.

**Problem context:**

I have data dump in JSON, that contains cartesian coordinates of points of interests (POI’s). Simplified structure of one document is like this:

```
{
"name": "Example Origo",
"coords": {
"x": 0,
"y": 0,
"z": 0
}
}
```

Names are unique to POI’s, and coordinates tell their positioning in 3d space in relation to each other.

**Problem I want to solve:**

I want to be able to search & group POIs by distance relative to other POIs in same dataset.

For example, I want to search for distance of 10, and get return list of POIs that have other POIs within that 10 unit distance in coordinates.

I’m really interested if there could be query that can be ran on the spot, and it does calculations every time again. I’m also ok to run search once, and have that output saved to new collection, with some simple data structure of having POI name, and array of POI’s that are within given distance. I could then further process that list.

Bruteforce way to solve this would be iterating all documents in collection, reading coordinates from that one POI and then calculate distances to all other POIs in collection, save matches to some variable in this bruteforce program and then in the end display results. I however have feeling that MongoDB would be able to handle this inside query, and that would be so much more elegant way.

If this would be latitude/longitude, I know there are some tools in mongo specific for those. But quick glance to those didn’t seem to allow using three axis of (x,y,z). And still I would be a bit loss how to start construct query that goes through all POIs, instead of having one reference POI with coordinates, and then trying to find if there are other within range.

And if calculating distance with coordinates isn’t readily in mind, it is calculated like this:

```
ABS ( SQRT ( (x1 - x0)^2 + (y1 - y0)^2 + (z1-z0)^2 ) )
```

Where (x0, y0, z0) is one POI coordinates and (x1, y1, z1) is coordinates of another POI.

Any help and direction how to start constructing this kind of query is appreciated, and even pointers which Mongo features to try and use for it.