1

I have already been facing issues in meteor when dealing with reactive sorting of a publication according to various data. Every time, I never found a real answer to what I was trying to achieve and I ended up giving up and just deciding "not to build my app that way", which always makes me really sad.

Now maybe I can phrase this problem simply enough that it could get a constructive and very interesting answer. I think it sounds like it should be possible to be done in a Meteor app, so here it is:

I store the locations of my users in "lat" and "lon" variables in my Accounts.users collection. I want the current user to get a list of, say, the 10 closest users to him or her. How would I do this, considering the seemingly complicated set of operations needed to get that distance from latitude and longitude?

As far as I know, it is pretty much impossible to insert such an equation in a MongoDB query condition. So how do other people achieve this feat?

I would appreciate any insight on this problem, as theoretical as it may be.

Community
  • 1
  • 1
SylvainB
  • 4,765
  • 2
  • 26
  • 39
  • If you use mysql, it can support spatial features and supports various GIS functions. See http://stackoverflow.com/questions/11436396/mysql-spatial-distance-using-point-not-working for an example where someone is computing distance. – user1269942 Oct 13 '14 at 21:02
  • Thank you ! Very interesting, will look into it :) Although, Meteor doesn't allow any other DBMS than MongoDB. :/ I hear there is a way to sort of make it work with MySQL, but I'm not on a hurry to start hacking around. – SylvainB Oct 13 '14 at 21:55
  • 2
    Doesn't the [$near](http://docs.mongodb.org/manual/reference/operator/query/near/) operator do what you want? – David Weldon Oct 13 '14 at 23:57
  • Wow, mongodb has an operator for that?! Thank you, will try this out! – SylvainB Oct 14 '14 at 06:20
  • Don't forget right data type and index. – none Oct 14 '14 at 06:29

1 Answers1

1

Look at my working example localgeo and sources on github

You can find like this:

   var nearMarkers = allMarkers.find({
      location: {
        $near: {
          $geometry: {
            type: "Point",
            coordinates: location
          },
          $maxDistance: radius
        }
      }
    });

Location is this type of MongoDB. And remember: in MongoDB Lng - first, Lat - second, I don't know why. I do it on client side, but you can make publication in the same way, depend on radius.

none
  • 1,699
  • 2
  • 13
  • 18
  • How does the `$near` sorting figure into other sorting modifiers? For instance, if I use `sort` as an option in the `find` method on the server, will it sort first by distance, then by other factors, or the opposite? – ygesher Aug 18 '15 at 11:26
  • I don't have the answer, but in MongoDB docs I found this: http://docs.mongodb.org/manual/reference/operator/query/near/#sort-operation – none Aug 21 '15 at 13:49
  • Yeah, I already looked at that. It doesn't seem to have any indication of priority. – ygesher Aug 24 '15 at 07:48