0

I'm wondering how an app like Tinder knows to get all the users in a 50miles radius of my location.

I could get my lat/long and then go through every other users lat/long while calculating the distance and only store the ones that are < 50miles but that seems like a horrible way to do it when you're dealing with a big user base.

Say I have a User Schema:

const userSchema = new Schema(
  {
    username: {
      type: String,
      required: true,
      index: { unique: true },
    },
    {...}
    lat: {
      type: Number,
      default: 0
    },
    long: {
      type: Number,
      default: 0
    },
  }
);

module.exports = mongoose.model("User", userSchema);

And I have a User info mongodb:

{
    "id": "605a652ddg3rdf46b274c8df",
    "username": "johnsmith",
    "lat": "39.554618",
    "long": "-104.911083"
}

And I've got 1000s of other Users just like it. Some with locations within 50miles of johnsmith, others not.

What would be the best/most efficient way to go about getting the users within a certain radius of johnsmith?

I've seen answers specific to Firebase like this but what if I'm using Mongoose/MongoDB?

mitchldtn
  • 423
  • 1
  • 4
  • 18
  • 1
    I'm not a MongoDB man, but when I needed to do the same sort of thing in MySQL I went to the reference for that db's geo/spherical calculations. Seems like MongoDB has similar https://docs.mongodb.com/manual/core/2dsphere/ (mind you, some of the MySQL geo functions were dependent on specific versions, so you'll likely need to consider that as well). On this particular page there's reference for querying "proximity to a point", in metres - https://docs.mongodb.com/manual/tutorial/query-a-2dsphere-index/ – Craig Mar 24 '21 at 03:31
  • Thank you @Craig ! I'm new to this stuff and didn't really know where to look or what it's even called. You've pointed me in the right direction. Appreciate it. – mitchldtn Mar 24 '21 at 03:38

0 Answers0