Last active
August 29, 2015 14:22
-
-
Save suissa/a696643028fb3b2c6a8b to your computer and use it in GitHub Desktop.
Workshop Geoloc com MongoDb - Exercicios
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var MongoClient = require('mongodb').MongoClient | |
, url = 'mongodb://localhost:27017/geodb' | |
, lugar = { | |
name: "Zona da Creide" | |
, loc: [50, 50] | |
} | |
; | |
MongoClient.connect(url, function(err, db) { | |
if(err) return console.log(err); | |
db.collection('lugares').insert(lugar, function(err, result) { | |
if(err) return console.log(err); | |
return console.log('Lugar ' + lugar.name + ' cadastrado: ', lugar); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var MongoClient = require('mongodb').MongoClient | |
, url = 'mongodb://localhost:27017/geodb' | |
; | |
MongoClient.connect(url, function(err, db) { | |
if(err) return console.dir(err) | |
var collection = db.collection('lugares'); | |
collection.ensureIndex({loc: "2d"}, {min: -1000, max: 1000}, function(err, result) { | |
if(err) return console.dir(err); | |
return console.log(result); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var MongoClient = require('mongodb').MongoClient | |
, url = 'mongodb://localhost:27017/geodb' | |
, lugares = [ | |
{name: "Bar do Ze", loc: [50, 50]} | |
, {name: "Bar da Dirce", loc: [10, 10]} | |
, {name: "C Q Sabe", loc: [45, 45]} | |
] | |
; | |
MongoClient.connect(url, function(err, db) { | |
if(err) return console.dir(err) | |
db.collection('lugares').insert(lugares, function(err, result) { | |
if(err) return console.dir(err) | |
return console.log('Lugares cadastrados: ', lugares); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var MongoClient = require('mongodb').MongoClient | |
, url = 'mongodb://localhost:27017/geodb' | |
, lugares = [ | |
{name: "Bar do Ze", loc: [50, 50]} | |
, {name: "Bar da Dirce", loc: [10, 10]} | |
, {name: "C Q Sabe", loc: [45, 45]} | |
] | |
; | |
MongoClient.connect(url, function(err, db) { | |
if(err) return console.dir(err) | |
db.collection('lugares') | |
.find({loc: {$near: [50,50], $maxDistance: 10}}) | |
.toArray(function(err, docs) { | |
if(err) return console.dir(err) | |
return console.log(docs); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var MongoClient = require('mongodb').MongoClient | |
, url = 'mongodb://localhost:27017/geodb' | |
, lugares = [ | |
{name: "Bar do Ze", loc: [50, 50]} | |
, {name: "Bar da Dirce", loc: [10, 10]} | |
, {name: "C Q Sabe", loc: [45, 45]} | |
] | |
; | |
MongoClient.connect(url, function(err, db) { | |
if(err) return console.dir(err) | |
db.collection('lugares').geoNear(50, 50, {$maxDistance:10}, function(err, docs) { | |
if(err) return console.dir(err) | |
return console.log(docs); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var MongoClient = require('mongodb').MongoClient | |
, url = 'mongodb://localhost:27017/geodb' | |
, lugares = [ | |
{name: "Bar do Ze", loc: [50, 50]} | |
, {name: "Bar da Dirce", loc: [10, 10]} | |
, {name: "C Q Sabe", loc: [45, 45]} | |
] | |
; | |
var box = [[30, 30], [60, 60]]; | |
MongoClient.connect(url, function(err, db) { | |
if(err) return console.dir(err) | |
db.collection('lugares').find({loc: {$within: {$box: box}}}).toArray(function(err, docs) { | |
if(err) return console.dir(err) | |
return console.log(docs); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var MongoClient = require('mongodb').MongoClient | |
, url = 'mongodb://localhost:27017/geodb' | |
, lugares = [ | |
{name: "Bar do Ze", loc: [50, 50]} | |
, {name: "Bar da Dirce", loc: [10, 10]} | |
, {name: "C Q Sabe", loc: [45, 45]} | |
] | |
; | |
var triangle = [[40, 40], [40, 50], [45, 45]]; | |
MongoClient.connect(url, function(err, db) { | |
if(err) return console.dir(err) | |
db.collection('lugares').find({loc: {$within: {$polygon: triangle}}}).toArray(function(err, docs) { | |
if(err) return console.dir(err) | |
return console.log(docs); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment