Searching for multiple geo locations

700 views
Skip to first unread message

Michael Grohe

unread,
Feb 26, 2015, 1:22:38 AM2/26/15
to mongod...@googlegroups.com
I have documents with two addresses (one pickup and one handover address). Each address has a geolocation. Is it somehow possible to find documents which "fit" to both addresses? I tried a "chained" near-query. But this results in an error (Can't canonicalize query: BadValue Too many geoNear expressions). As I have seen in this group and on SO this doesn't seem to be supported out of the box. Is there any "workaround"?

Will Berkeley

unread,
Feb 26, 2015, 10:45:20 AM2/26/15
to mongod...@googlegroups.com
Can you be more specific? What does "'fit' to both addresses" mean? Can you show us your example query, with sample documents, that tries to do what you want but isn't working?

-Will

guido hornig

unread,
Feb 26, 2015, 10:53:31 AM2/26/15
to mongod...@googlegroups.com
Am 26.02.2015 16:45 schrieb "Will Berkeley" <william....@mongodb.com>:
Can you be more specific? What does "'fit' to both addresses" mean? Can you show us your example query, with sample documents, that tries to do what you want but isn't working?

-Will

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/ede75ea6-63e2-4436-8bc3-51de0f3ae627%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

guido hornig

unread,
Feb 26, 2015, 10:53:46 AM2/26/15
to mongod...@googlegroups.com

Michael Grohe

unread,
Feb 27, 2015, 3:48:48 AM2/27/15
to mongod...@googlegroups.com
Ok. I will explain the use case.
A user has two addresses (one "pickup address" and one "delivery address").
The user wants to find other users which have similar pickup AND delivery addresses (this is why I need two $geoNear).

Example data (simplified):
  • User, pickup address, delivery address:
    • User 1, Germany, USA
    • User 2, France, Spain
    • User 3, Germany, USA
    • User 4, Germany, France
    • User 5, Canada, USA
    • User 6, Germany, USA

If user 1 wants to find other users, user 3 and user 6 have to be shown (because only there both addresses are "near").

In my real project the data is a bit more complex and of course I have real addresses (with latitude and longitude). 

If two $geoNear are not possible, I could go first for the pickup address, then for the delivery address. However I fear a bad performance here because I have millions of addresses.

Need more details? Please let me know.

Maga Napanga

unread,
Apr 17, 2017, 7:50:45 AM4/17/17
to mongodb-user
I know it's an old thread but it might still be useful for someone.

As of MongoDB 3.4.2, the same problem is present when using $geoNear and any other "near" expression which sorts the output from nearest to farthest.

You can, though, use the $geoWithin expression, which does not require an index (although it would be useful of course). It also doesn't limit the collection to have at most one 2d/2dsphere index).  It can be used even in an aggregate with a match stage. In this example, it's looking for points within a 200 m radius from a reference point, each start and end points separately:

db.trips.aggregate(
  { $match: {
    'start.gps.loc': {
      $geoWithin: { $centerSphere: [ [ -4.364399, 36.514921 ], 0.2/6378.1 ] }
    },
    'stop.gps.loc': {
      $geoWithin: { $centerSphere: [ [ -4.456572, 36.534344 ], 0.2/6378.1 ] }
    }
  }},
  { $project: {
      Aloc: '$start.gps.loc.coordinates', Bloc:'$stop.gps.loc.coordinates',
      Atag:'$start.addr.tag', Btag:'$stop.addr.tag',
  }}
)


Hope it helps.

Reply all
Reply to author
Forward
0 new messages