Skip to content

Instantly share code, notes, and snippets.

@aaizemberg
Created April 9, 2019 14:48
Show Gist options
  • Save aaizemberg/f479bfc61427f40395955f3bead6f1ea to your computer and use it in GitHub Desktop.
Save aaizemberg/f479bfc61427f40395955f3bead6f1ea to your computer and use it in GitHub Desktop.
mongodb geospatial tutorial
# mongodb geospatial-tutorial
source: https://docs.mongodb.com/manual/tutorial/geospatial-tutorial/
$ wget https://raw.githubusercontent.com/mongodb/docs-assets/geospatial/neighborhoods.json
$ wget https://raw.githubusercontent.com/mongodb/docs-assets/geospatial/restaurants.json
$ mongoimport restaurants.json -c restaurants -d 7dbs
$ mongoimport neighborhoods.json -c neighborhoods -d 7dbs
# si no especificas -d 7dbs, te crea las colecciones en la db 'test'
#
$ mongo
> use 7dbs
> db.restaurants.createIndex({ location: "2dsphere" })
> db.neighborhoods.createIndex({ geometry: "2dsphere" })
> db.restaurants.findOne()
> db.neighborhoods.findOne({ geometry: { $geoIntersects: { $geometry: { type: "Point", coordinates: [ -73.93414657, 40.82302903 ] } } } })
> var a_nh = db.neighborhoods.findOne( { geometry: { $geoIntersects: { $geometry: { type: "Point", coordinates: [ -73.93414657, 40.82302903 ] } } } } )
> db.restaurants.find( { location: { $geoWithin: { $geometry: a_nh.geometry } } } ).count()
# restaurants a 1 km
#
> db.restaurants.find({ location: { $geoWithin: { $centerSphere: [ [ -73.93414657, 40.82302903 ], 1 / 6371 ] } } }).count()
# radio de la tierra, 3963.2 en millas o 6371 en km.
# otra manera de buscar, restaurantes a un km
#
> db.restaurants.find({ location: { $nearSphere: { $geometry: { type: "Point", coordinates: [ -73.93414657, 40.82302903 ] }, $maxDistance: 1000 } } }).count()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment