2

I have source and destination fields in my mongodb collection named Flight. Both the fields are geospatially indexed and written in lat-long format. I am using mongomapper to query it from rails controller.

I want to write a query like following.

Result= Flight.where(:source => {'$near' =>  location_src} , :destination => {'$near' => location_dest} )

where location_src and location_dest are gui inputs in lat-long format.

However, when I try to access the Result by writing Result.first or Result.all, it says that Mongo::OperationFailure: can't have 2 special fields .

Can anyone suggest me what could be the workaround?

Kind Regards, Amrish.

Nelson
  • 49,283
  • 8
  • 68
  • 81
Amrish Patel
  • 442
  • 1
  • 6
  • 14

4 Answers4

2

I found a simple workaround which serves my purpose and might be helpful to someone facing the similar scenario.

Though mongodb does not support geospatial query on multiple fields simultaneously, it still supports geospatial indexing on multiple fields simultaneously. So, we would run query on both the fields one after another as follows.

  • start_place : name of starting place
  • start_place_loc : co-ordinates of the starting location (indexed geospatial field)
  • end_place : name of destination place
  • end_place_loc : co-ordinates of the destination location (indexed geospatial field)

Following is the example code.

tmp=Flight.distinct(:start_place,:start_place_loc => {'$near' =>  location_src} )

Result=Flight.where(:start_place_loc => {'$near' =>  location_src} , :start_place => {'$in' => tmp})

Also, one will need to set '$maxDistance' => "value" according to one's requirement.

Amrish Patel
  • 442
  • 1
  • 6
  • 14
1

Currently Mongodb only supports 1 geo index per collection. That's documented, from this page

You may only have 1 geospatial index per collection, for now. While MongoDB may allow to create multiple indexes, this behavior is unsupported. Because MongoDB can only use one index to support a single query, in most cases, having multiple geo indexes will produce undesirable behavior.

RameshVel
  • 64,778
  • 30
  • 169
  • 213
0

@Amrish

currently mongodb can have only one 2d index per collection, However you can have Multi-location Documents.

Address : [ {loc:[Lon,Lat], // or {lon: "somevalue" , lat:"somevalue"} name:"start" },

{loc:[Lon,Lat], // or {lon: "somevalue" , lat:"somevalue"} name:"end" }, ]

And you can have 2d index on Address.loc

This can be helpful with $near and geoNear in searching and can be used for workaround to search Find all "start" point near to given lat-lng pair

Also note : it should be (long,lat) pair Not reverse.

]

Raxit Sheth
  • 2,491
  • 5
  • 17
  • 20
0

looks like SO has zig-zag the example

Shortly, put (lng,lat) pair, with name:start or name:end. Make array of above pair put 2d Index on above array.

This can help you in querying find all start point near to given (lng,lat) pair. This can help you in querying find all end point near to given (lng,lat) pair.

And you should able to use AND/OR etc operation for above two operation

Raxit Sheth
  • 2,491
  • 5
  • 17
  • 20