Skip to content

Instantly share code, notes, and snippets.

@t2k
Created June 19, 2015 19:42
Show Gist options
  • Save t2k/3ec6507a481203fec246 to your computer and use it in GitHub Desktop.
Save t2k/3ec6507a481203fec246 to your computer and use it in GitHub Desktop.
mongoose schema geojson example
mongoose = require('mongoose')
Schema = mongoose.Schema
assert = require('assert')
console.log '\n==========='
console.log ' mongoose version: %s', mongoose.version
console.log '========\n\n'
dbname = 'testing_geojsonPoint3'
mongoose.connect 'localhost', dbname
mongoose.connection.on 'error', ->
console.error 'connection error', arguments
return
# schema
schema = new Schema({
message: String
loc:
type: type: String
coordinates: []
}, capped: 1024)
schema.index loc: '2dsphere'
A = mongoose.model('A', schema)
mongoose.connection.on 'open', ->
A.on 'index', (err) ->
if err
throw err
A.create {
message: 'message'
loc:
type: 'Point'
coordinates: [
-179.0
0.0
]
}, (err) ->
if err
throw err
tail = A.find(loc: $near:
type: 'Point'
coordinates: [
-179.0
0.0
]).tailable().stream()
tail.on 'data', (d) ->
console.log d
return
return
return
return
setTimeout (->
new A(
message: 'close'
loc:
type: 'Point'
coordinates: [
-179.0
0.0
]).save (e, doc) ->
if e
throw e
console.log 'saved'
return
console.log 'timeout'
return
), 2000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment