-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGeo2DSpherePointDemo.js
62 lines (56 loc) · 1.9 KB
/
Geo2DSpherePointDemo.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
60
61
62
// Basic Geo2DSphere stuff.
//
// helper
var dump_cursor = function(cur) {
while ( cur.hasNext() ) {
printjson( cur.next() );
}
}
// use a 'geo' db and drop any point collection
db = db.getSiblingDB("geo");
db.point.drop();
// add some docs for cities
db.point.insert({name : "Palo Alto", loc : { type : "Point" , coordinates : [-122.143019,37.441883] } });
db.point.insert({name : "Cupertino", loc : { type : "Point" , coordinates : [-122.032182,37.322998] } });
db.point.insert({name : "San Jose", loc : { type : "Point" , coordinates : [-121.894955,37.339386] } });
db.point.insert({name : "San Francisco", loc : { type : "Point" , coordinates : [-122.419415,37.77493] } });
db.point.insert({name : "Los Angeles", loc : { type : "Point" , coordinates : [-118.243685,34.052234] } });
db.point.insert({name : "Washington, DC", loc : { type : "Point" , coordinates : [-77.036366,38.895112] } });
// index on 'loc'
db.point.ensureIndex({"loc":"2dsphere"});
// do some queries
var closeToCupertino = db.point.find( { "loc" : {
$near : {
$geometry : {
type : "Point" ,
coordinates : [-122.143019,37.441883]
},
$maxDistance : 20000
}
}
});
print("Places within 20000 'points' from Cupertino");
dump_cursor( closeToCupertino );
print("#Close to Apple HQ = " + closeToCupertino.count());
var eastCoast = db.point.find( { "loc" : {
$geoWithin : {
$geometry : {
type : "Polygon",
coordinates: [[
[ -71, 60 ],
[ -71, 15 ],
[ -80, 15 ],
[ -80, 60 ],
[ -71, 60 ]
]],
crs: {
type: "name",
properties: { name: "urn:x-mongodb:crs:strictwinding:EPSG:4326" }
}
}
}
}
});
print("East coast cities");
dump_cursor( eastCoast );
print("#East coast cities= " + eastCoast.count());