Skip to content

Instantly share code, notes, and snippets.

@jystewart
Last active December 27, 2015 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jystewart/7327162 to your computer and use it in GitHub Desktop.
Save jystewart/7327162 to your computer and use it in GitHub Desktop.
Quick example of how to do a geo query with the moped library and convert the output to JSON
require 'moped'
require 'yajl'
session = Moped::Session.new([ "127.0.0.1:27017" ])
session.use "imminence_development"
query = session.command({
geoNear: "places",
near: [ -2.16933984212885, 52.27229461855149 ],
spherical: false,
query: { service_slug: "register-offices", data_set_version: 2 },
num: 5
})
output = query['results'].collect { |row| row['obj']['distance'] = row['dis']; row['obj'] }
# Use YAJL as it's said to be much faster than the core JSON lib
Yajl::Encoder.encode(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment