Joe Busuttil
unread,Apr 8, 2013, 2:08:00 AM4/8/13Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to mongo...@googlegroups.com
Hi and thanks for reading. I'm quite new to Ruby and Mongomapper so please forgive me if this is obvious.
I'm trying to do a geospatial query, depending on how the user enters their location it'll either be an exact match or distance based. I'd like to also query with other fields.
The Model
class User
include MongoMapper::Document
#other fields removed for brevity
key :location, Array
ensure_index [[:location, '2d']]
key :locationname, String
key :geosearchtype, String
key :searchdistance, Integer, :default => 1
key :active, String, :default => "on"
key :visible, Boolean, :default => false
timestamps!
end
The location is set by a query to google through the geocoder gem
In the mongo client I am able to do:
db.users.find({location: [-31.945475, 115.8566411], active: "on", visible: true})
and this gives me three results
In MongoMapper I'm doing (some lines commented out to make things simpler:
if @user.visible == true
basequery = Hash.new
# basequery["visible"] = true
# basequery["active"] = "on"
if @user[:geosearchtype] == "venue"
basequery["location"] = @user[:location]
elsif @user[:geosearchtype] == "distance"
basequery[:location] = {'$near' => @user[:location], '$maxDistance' => @user[:searchdistance]})
end
@matchedusers = User.where(basequery).all
end
I've only tested with the Exact match (ie venue based) and when I do I get:
geo field only has 1 element
Since my query works in mongo I don't understand what I need to do differently in MongoMapper. Any help would be appreciated.