deno.land / x / mongoose@6.7.5 / examples / geospatial / geoJSONexample.js

geoJSONexample.js
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// import async to make control flow simplier'use strict';
const async = require('async');
// import the rest of the normal stuffconst mongoose = require('../../lib');
require('./geoJSONSchema.js')();
const Location = mongoose.model('Location');
// define some dummy data// note: the type can be Point, LineString, or Polygonconst data = [ { loc: { type: 'Point', coordinates: [-20.0, 5.0] } }, { loc: { type: 'Point', coordinates: [6.0, 10.0] } }, { loc: { type: 'Point', coordinates: [34.0, -50.0] } }, { loc: { type: 'Point', coordinates: [-100.0, 70.0] } }, { loc: { type: 'Point', coordinates: [38.0, 38.0] } }];

mongoose.connect('mongodb://localhost/locations', function(err) { if (err) { throw err; }
Location.on('index', function(err) { if (err) { throw err; } // create all of the dummy locations async.each(data, function(item, cb) { Location.create(item, cb); }, function(err) { if (err) { throw err; } // create the location we want to search for const coords = { type: 'Point', coordinates: [-5, 5] }; // search for it Location.find({ loc: { $near: coords } }).limit(1).exec(function(err, res) { if (err) { throw err; } console.log('Closest to %s is %s', JSON.stringify(coords), res); cleanup(); }); }); });});
function cleanup() { Location.remove(function() { mongoose.disconnect(); });}
mongoose

Version Info

Tagged at
a year ago